-1

I'm using R and GGplot2 and I want to make a graph in which there are boxes (from geom_boxplot) and points (from geom_dotplot) one next to the other, not above one another.
The picture shows what I've managed to do for now, which is pretty much what I'd like to have except that, instead of the pink boxes, I want just the points from the 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())

This is a subset of the data that I'm using:

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")
Mabri
  • 119
  • 11

2 Answers2

0

The solution was just filtering the data within ggplot.
This is the code of the graph:

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())
Mabri
  • 119
  • 11
0

Not a real answer but a suggestion: I tend to prefer geom_jitter(size = 0.5, width = 0.2) to overlay dots with boxplots, and use the outlier.size = NULL argument in geom_boxplot not to duplicate dots.

r_mvl
  • 109
  • 5