2016-04-29 17 views
-1

如何制作如下图所示的条形图?如何用ggplot2制作这样的条形图

Fig.1

我试过,但只能得到这一个:

Fig.2

这里是我的代码:

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")) 

和下面的数据显示:

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)) 

注意,“O”型是指“输出”和“我”的意思是“输入”

+1

获得帮助和工作代码的最佳方式是发布最少的输入数据,并在此处预期结果内联(而不是外部链接)。 – Gopala

+0

为什么你想复制那个怪物? – hrbrmstr

+0

@hrbrmstr,我只想知道是否ggplot2可以处理那种图片或不。 – Frank

回答

2

这本来是做dput()我问的评论更有帮助,但我取代了print()输出,对于那些想要贡献的人来说。

有(至少)两种方式(比下面的方法)接近该图表。这一个依赖于新兴geom_,让你有水平条W/O 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 

enter image description here

这个新geom_barh()是在一个包,是不是在CRAN(尚未),所以如果这是一个问题,至少有其他两种方式,正如我所说的。

另一种方式为您提供了完全冲洗的条形,并且可以做到这一点,只需要geom_bar(),但它需要一些数据争用和轴文本标签的争夺。您必须手工制作哪些刻面标签可以在这里免费获得(如果您需要输入/输出标签)。

如果你需要分开的传说(和一些额外的调整来获得酒吧刷新),那么最直接的方法是做出两个独立的情节,编辑grob页边距&使用grid.arrange()。如果您在绘图标题中使用此方法,还可以获得对齐的输入/输出标签。

这两个都是“工作”。

如果您需要花样填充,其他人将不得不帮助您。这是可行但乏味的。

+0

太棒了!感谢您花时间回复我的问题。新的geom_barh()似乎很棒。 – Frank

+0

@ hrbrmstr,另一个基本问题:我对'aes(group =)'中的参数'group'有点困惑。如果ggplot由不同的组处理'geom_',为什么在这种情况下geom_bar(stat =“identity”,aes(group = Type,fill = variable))没有提供预期的输出?为我的英语道歉。希望你能明白我在问什么。 – Frank

+0

它与ggplot2如何处理(或者在这种情况下不处理)+和 - 与'position =“stack”'有关。它只是定义了> = 0的值,并且不会自动执行所需的交替布置(我认为这会回答您的问题,但我可能会误解)。 – hrbrmstr