GGplot箱线图和点线图并排

时间:2018-11-05 09:30:59

标签: r ggplot2 graphics boxplot

我正在使用R和GGplot2,我想制作一个图形,其中的框(从geom_boxplot开始)和点(从geom_dotplot开始)彼此相邻,而不是彼此相邻。
该图显示了我目前想要完成的工作,除了没有粉红色的框,我只想从geom_dotplot

中获得要点,这几乎是我想要的。

enter image description here

df2%>%ggplot(aes(factor(Organ), value,fill=Crop)) + 
  geom_boxplot() + 
  #geom_dotplot(binaxis='y',stackdir = 'center')
  scale_y_log10("BCF") + facet_wrap(~Csoil)+theme(axis.title.x=element_blank())

这是我正在使用的数据的子集:

df2=structure(list(variable = structure(c(8L, 2L, 6L, 14L, 5L, 14L, 
3L, 8L, 5L, 6L, 2L, 13L, 5L, 7L, 13L, 3L, 8L, 10L, 10L, 2L, 2L, 
8L, 6L, 9L, 10L, 14L, 14L, 1L, 4L, 13L, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA), .Label = c("BCF leaf (CsoilMax) - Ryegrass", 
"BCF root (CsoilMax) - Ryegrass", "BCF root (CsoilMed) - Ryegrass", 
"BCF stem (CsoilMax) - Ryegrass", "BCF stem (CsoilMed) - Ryegrass", 
"BCF leaf (CsoilMed) - Ryegrass", "BCF fruit (CsoilMax) - Maize", 
"BCF fruit (CsoilMed) - Maize", "BCF leaf (CsoilMax) - Maize", 
"BCF leaf (CsoilMed) - Maize", "BCF root (CsoilMax) - Maize", 
"BCF root (CsoilMed) - Maize", "BCF stem (CsoilMax) - Maize", 
"BCF stem (CsoilMed) - Maize"), class = "factor"), value = c(0.209606259766772, 
0.506401960143269, 0.000660265316109507, 6.51930210129075, 0.0207635735841085, 
0.765736181143394, 1.40601301731962, 0.00743553520916094, 0.040587560806454, 
1.60441689044071, 0.522063333823462, 0.540632385379595, 0.0497467701708571, 
0.0573435549206478, 0.0109218792177719, 0.608263306408034, 0.911628697794879, 
0.0874473327218576, 0.105726788757446, 0.267232334133824, 1.3297503892306, 
0.292440363142525, 0.037969380254565, 0.000157551659778798, 0.237462116816578, 
0.0447547790805731, 1.29271738284098, 0.000673871813931306, 0.00388673588576815, 
0.0223297222656565, 6.82, 11.53, 5.39, 5, 5, 0.005, 107.984, 
26.987, 0.005, 0.005, 132.28), Crop = c("Maize", "Ryegrass", 
"Ryegrass", "Maize", "Ryegrass", "Maize", "Ryegrass", "Maize", 
"Ryegrass", "Ryegrass", "Ryegrass", "Maize", "Ryegrass", "Maize", 
"Maize", "Ryegrass", "Maize", "Maize", "Maize", "Ryegrass", "Ryegrass", 
"Maize", "Ryegrass", "Maize", "Maize", "Maize", "Maize", "Ryegrass", 
"Ryegrass", "Maize", "Literature", "Literature", "Literature", 
"Literature", "Literature", "Literature", "Literature", "Literature", 
"Literature", "Literature", "Literature"), Csoil = c("Median soil C", 
"Maximum soil C", "Median soil C", "Median soil C", "Median soil C", 
"Median soil C", "Median soil C", "Median soil C", "Median soil C", 
"Median soil C", "Maximum soil C", "Maximum soil C", "Median soil C", 
"Maximum soil C", "Maximum soil C", "Median soil C", "Median soil C", 
"Median soil C", "Median soil C", "Maximum soil C", "Maximum soil C", 
"Median soil C", "Median soil C", "Maximum soil C", "Median soil C", 
"Median soil C", "Median soil C", "Maximum soil C", "Maximum soil C", 
"Maximum soil C", "Median soil C", "Median soil C", "Median soil C", 
"Median soil C", "Median soil C", "Median soil C", "Median soil C", 
"Median soil C", "Median soil C", "Median soil C", "Median soil C"
), Organ = c("Fruits", "Roots", "Leaves", "Stem", "Stem", "Stem", 
"Roots", "Fruits", "Stem", "Leaves", "Roots", "Stem", "Stem", 
"Fruits", "Stem", "Roots", "Fruits", "Leaves", "Leaves", "Roots", 
"Roots", "Fruits", "Leaves", "Leaves", "Leaves", "Stem", "Stem", 
"Leaves", "Stem", "Stem", "Leaves lit.", "Leaves lit.", "Roots lit.", 
"Roots lit.", "Roots lit.", "Fruits lit.", "Fruits lit.", "Fruits lit.", 
"Fruits lit.", "Fruits lit.", "Fruits lit.")), .Names = c("variable", 
"value", "Crop", "Csoil", "Organ"), row.names = c(NA, -41L), class = "data.frame")

2 个答案:

答案 0 :(得分:0)

解决方案只是过滤ggplot中的数据。
这是图形的代码:

df2%>%ggplot(aes(factor(Organ), value,fill=Crop)) + 
  geom_boxplot(data=.%>%filter(Crop!="Literature")) + 
  geom_dotplot(data=.%>%filter(Crop=="Literature"),binaxis='y',stackdir = 'center',dotsize=0.35,fill="black") +
  scale_y_log10("BCF") + facet_wrap(~Csoil)+theme(axis.title.x=element_blank())

答案 1 :(得分:0)

不是一个真正的答案,而是一个建议:我倾向于使用geom_jitter(size = 0.5, width = 0.2)用箱形图覆盖点,并在outlier.size = NULL中使用geom_boxplot参数不重复点。