ggplot2条形图的多个子组 [英] ggplot2 multiple sub groups of a bar chart
问题描述
我正在尝试制作具有多个因素分组的条形图。我试图创建的一个例子,按品种和灌溉处理分类:
$ b facet_wrap()$ c产生多个图表$ c>,但我想为同一类型的数据为多年的类似数据生成多个图表。我在这个例子中使用的数据的一个例子:
Year Trt品种基因产量SE
2010-2011 Irr Variety .2 1 6807 647
2010-2011 Irr Variety.2 2 5901 761
2010-2011 Irr Variety.1 1 6330 731
2010-2011 Irr Variety.1 2 5090 421
2010-2011干品种.2 1 3953 643
2010-2011干品种.2 2 3438 683
2010-2011干品种1 1 3815 605
2010-2011干品种1 2 3326 584
有没有办法在ggplot2中创建多个分组?我搜索了相当长的一段时间,还没有看到类似上面示例图的例子。
感谢您的任何帮助!
这可能是一个开始。
dodge < - position_dodge(width = 0.9)
ggplot(df,aes(x =交互作用(Variety,Trt),y = yield,fill = factor(geno)))+
geom_bar(stat =identity,position = position_dodge())+
geom_errorbar(aes(ymax = yield + SE,ymin = yield-SE),position = dodge,width = 0.2)
更新:x axis
我添加了:
coord_cartesian
,设置y轴的限制,主要是下限避免轴的默认扩展。
annotate
,添加所需的标签。我对 x
位置进行了硬编码,我在这个相当简单的例子中找到了合适的位置。
theme_classic
,删除灰色背景和网格。
主题
,增加较低的绘图边距以便为两行标签留出空间,移除默认标签。
上一组代码:由于该文本将添加到x轴下方,它会在绘图区域外消失,并且我们需要删除裁剪。
library(grid)
g1 < - ggplot(data = df,aes (x = interaction(Variety,Trt),y = yield,fill = factor(geno)))+
geom_bar(stat =identity,position = position_dodge())+
geom_errorbar(aes ymax = c(0,7500))+
注解(text,x = y)= yield + SE,ymin = yield-SE),position = dodge,width = 0.2)+
1:4,y = - 400,
label = rep(c(Variety 1,Variety 2),2))+
annotate(text,c(1.5,3.5) ,y = - 800,label = c(Irrigated,Dry))+
theme_classic()+
theme(plot.margin = unit(c(1,1,4,1) ,lines),
axis.title.x = element_blank(),
axis.text.x = element_blank())
删除x轴标签的裁剪
g2 < - ggplot_gtable(ggplot_build(g1))
g2 $ layout $ clip [g2 $ layout $ name ==panel]< - off
grid.draw(g2 )
I am trying to produce a bar graph that has multiple groupings of factors. An example from excel of what I am attempting to create, subgrouped by Variety and Irrigation treatment:
I know I could produce multiple graphs using facet_wrap()
, but I would like to produce multiple graphs for this same type of data for multiple years of similar data. An example of the data I used in this example:
Year Trt Variety geno yield SE
2010-2011 Irr Variety.2 1 6807 647
2010-2011 Irr Variety.2 2 5901 761
2010-2011 Irr Variety.1 1 6330 731
2010-2011 Irr Variety.1 2 5090 421
2010-2011 Dry Variety.2 1 3953 643
2010-2011 Dry Variety.2 2 3438 683
2010-2011 Dry Variety.1 1 3815 605
2010-2011 Dry Variety.1 2 3326 584
Is there a way to create multiple groupings in ggplot2? I have searched for quite some time and have yet to see an example of something like the example graph above.
Thanks for any help you may have!
This may be a start.
dodge <- position_dodge(width = 0.9)
ggplot(df, aes(x = interaction(Variety, Trt), y = yield, fill = factor(geno))) +
geom_bar(stat = "identity", position = position_dodge()) +
geom_errorbar(aes(ymax = yield + SE, ymin = yield - SE), position = dodge, width = 0.2)
Update: labelling of x axis
I have added:
coord_cartesian
, to set limits of y axis, mainly the lower limit to avoid the default expansion of the axis.
annotate
, to add the desired labels. I have hard-coded the x
positions, which I find OK in this fairly simple example.
theme_classic
, to remove the gray background and the grid.
theme
, increase lower plot margin to have room for the two-row label, remove default labels.
Last set of code: Because the text is added below the x-axis, it 'disappears' outside the plot area, and we need to remove the 'clipping'. That's it!
library(grid)
g1 <- ggplot(data = df, aes(x = interaction(Variety, Trt), y = yield, fill = factor(geno))) +
geom_bar(stat = "identity", position = position_dodge()) +
geom_errorbar(aes(ymax = yield + SE, ymin = yield - SE), position = dodge, width = 0.2) +
coord_cartesian(ylim = c(0, 7500)) +
annotate("text", x = 1:4, y = - 400,
label = rep(c("Variety 1", "Variety 2"), 2)) +
annotate("text", c(1.5, 3.5), y = - 800, label = c("Irrigated", "Dry")) +
theme_classic() +
theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
axis.title.x = element_blank(),
axis.text.x = element_blank())
# remove clipping of x axis labels
g2 <- ggplot_gtable(ggplot_build(g1))
g2$layout$clip[g2$layout$name == "panel"] <- "off"
grid.draw(g2)
这篇关于ggplot2条形图的多个子组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!