[英]How to make a bar plot like this with ggplot2
How to make a bar-plot like the picture below? 如何制作如下图的条形图?
I've tried, but only can get this one: 我已经尝试过了,但是只能做到这一点:
Here is my code: 这是我的代码:
ggplot(N.Balance, aes(x = factor(Period), y = value)) +
geom_bar(stat = "identity", aes( group = Type, fill = variable), osition = "stack", width = 0.6) +
facet_wrap(~ Type,ncol = 1) +
coord_flip() +
scale_fill_grey() +
theme_bw(base_size = 30, base_family = "serif") +
labs(y = expression(paste("kg", " ", "N", " ", ha^{-1}))) +
theme(legend.key.height = unit(0.5, "in"))
And the data is shown below: 数据如下所示:
structure(list(Period = c("2007R", "2007/2008W", "2008R", "2008/2009W",
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W",
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W",
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W",
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W",
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W",
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W",
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W",
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W",
"2009R", "2009/2010W", "2007R", "2007/2008W", "2008R", "2008/2009W",
"2009R", "2009/2010W"),
variable = c("Denitrification", "Denitrification", "Denitrification", "Denitrification",
"Denitrification", "Denitrification", "Runoff", "Runoff", "Runoff", "Runoff", "Runoff",
"Runoff", "Leaching", "Leaching", "Leaching", "Leaching", "Leaching", "Leaching", "NH3Vol",
"NH3Vol", "NH3Vol", "NH3Vol", "NH3Vol", "NH3Vol", "Harvest",
"Harvest", "Harvest", "Harvest", "Harvest", "Harvest", "Fertilizer",
"Fertilizer", "Fertilizer", "Fertilizer", "Fertilizer", "Fertilizer",
"Fix", "Fix", "Fix", "Fix", "Fix", "Fix", "Irrigation", "Irrigation",
"Irrigation", "Irrigation", "Irrigation", "Irrigation", "Seeds",
"Seeds", "Seeds", "Seeds", "Seeds", "Seeds", "Deposition", "Deposition",
"Deposition", "Deposition", "Deposition", "Deposition"),
value = c(-89.4, -34.4, -61.5, -82.5, -87.2, -34.7, -21.8, -33.4, -2.65, -42.8,
-19.2, -58.7, -8.22, -1.44, -9.76, -4.76, -4.97, -19, -71.6,
-50.8, -97.1, -10.9, -60.6, -19.6, -187, -116, -167, -96, -177,
-127, 300, 200, 300, 200, 300, 200, 45, 15, 45, 15, 45, 15, 12.5,
0, 11.6, 0, 11.3, 0, 0.9, 3, 0.9, 3, 0.9, 3, 8.41, 13.74, 4.01,
13.34, 16.31, 9.81), Type = c("O", "O", "O", "O", "O", "O", "O",
"O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O",
"O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "I", "I", "I",
"I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I",
"I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I")),
.Names = c("Period", "variable", "value", "Type"), class = "data.frame",
row.names = c(NA, -60L))
Note that the type "O" means "Output" and "I" means "Input" 注意,类型“ O”表示“输出”,“ I”表示“输入”
It would have been more helpful to do the dput()
I asked for in the comment, but I replaced the print()
output with that for others who may want to contribute. 进行我在注释中要求的
dput()
可能会更有帮助,但我将print()
输出替换为其他可能想要贡献的人。
There are (at a minimum) two other ways (than the one below) to get close to that chart. 至少有两种其他方法(除了下面的一种)可以接近该图表。 This one relies on an emerging new
geom_
that lets you have horizontal bars w/o coord_flip()
- which means you can play with facet scales: 这依赖于一种新兴的
geom_
,它使您可以使用不带coord_flip()
-这意味着您可以使用刻面比例尺:
library(ggplot2)
library(ggstance) # devtools::install_github("lionel-/ggstance")
library(dplyr)
N.Balance <- read.csv("~/Data/so.csv", stringsAsFactors=FALSE)
N.Balance$Period_f <- factor(N.Balance$Period)
N.Balance$Type_f <- factor(N.Balance$Type,
levels=c("O", "I"),
labels=c("Output", "Input"))
gg <- ggplot(N.Balance, aes(x=value, y=Period_f))
gg <- gg + geom_barh(stat = "identity",
aes(group = Type, fill = variable),
position = "stack", width = 0.6)
gg <- gg + geom_text(data=data.frame(Period=unique(N.Balance$Period)),
aes(x=5, y=Period, label=Period),
color="white", hjust=0, size=3)
gg <- gg + scale_x_continuous(expand=c(0,-0.001))
gg <- gg + scale_fill_grey(name="")
gg <- gg + facet_wrap(~ Type_f, ncol=2, scales="free_x")
gg <- gg + guides(fill=guide_legend(keywidth = 2, keyheight = 1))
gg <- gg + labs(y=NULL, x = expression(paste("kg", " ", "N", " ", ha^{-1})))
gg <- gg + theme_bw()
gg <- gg + theme(legend.key.height = unit(0.5, "in"))
gg <- gg + theme(panel.background=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(panel.margin=margin(l=0, r=0))
gg <- gg + theme(legend.position="top")
gg <- gg + theme(panel.grid=element_blank())
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme(axis.text.y=element_blank())
gg <- gg + theme(axis.ticks.y=element_blank())
gg <- gg + theme(legend.key=element_blank())
gg <- gg + theme(legend.text=element_text(size=8))
gg <- gg + theme()
gg
This new geom_barh()
is in a package that is not on CRAN (yet) so if that's an issue, there are—as I said—at least two other ways. 这个新的
geom_barh()
位于一个不在CRAN上的程序包中(因此),因此,如果有问题,至少有两种方法可以解决。
One other way gives you completely flush bars, and it's possible to do that w/just geom_bar()
but it takes some data wrangling and axis text label wrangling. 另一种方法是为您提供完全刷新的条,并且可以仅通过
geom_bar()
来完成,但需要一些数据处理和轴文本标签处理。 You have to hand craft what facet labeling gets you for free here (if you need the Input/Output labels). 您必须在这里手工制作什么方面的标签可免费获得您(如果需要输入/输出标签)。
If you need the legends separated (and some additional tweaking to get the bars flush), then most straighforward way is to make two separate plots, edit grob margins & use grid.arrange()
. 如果需要将图例分开(以及进行一些其他调整以使条形平齐),那么最直接的方法是制作两个单独的图,编辑
grid.arrange()
边距并使用grid.arrange()
。 This method can also get you aligned Input/Output labels if you do it in the plot title. 如果在图标题中执行此方法,则还可以使输入/输出标签对齐。
Both of those are "work". 两者都是“工作”。
If you need pattern fills, someone else will have to help you. 如果您需要图案填充,其他人将不得不帮助您。 That's doable but tedious.
那是可行的,但很乏味。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.