0

I am trying to plot a ggplot with 3 explanatory variables. I cannot align the points with their respective box plots. Can somebody help me solve this problem?

This is the code i have tried, all the points are aligned except for the two middle boxplots, where the points are not at the right position.

ggplot(fakedata, aes(x = Temperature,
                     y = Fitness,
                     fill = Fungi_species,
                     form = Collembola_species)) + 
  geom_boxplot(size = 0.2, color = "black", 
               position = position_dodge2(0.75)) +
  stat_summary(fun = mean, geom = "point",
               shape = 16, size = 2,
               color = 'black',
               position = position_dodge2(0.75)) +
  geom_point(aes(color = Collembola_species, 
                 shape = Collembola_species),
             position = position_dodge(0.75), 
             alpha = 0.7, size = 2) + 
  scale_fill_manual(values = c("forestgreen", "grey")) +
  scale_color_manual(values = c("red", "cyan")) +
  theme_bw() +
  ylab("Fitness")+ 
  xlab("Temperature") +
  ylim(0, 7) 

enter image description here

Thanks a lot for your help!

Data :

fakedata <- structure(list(Fitness = c(5, 5.5, 4.5, 5.25, 4.75, 5, 5.5, 4.5, 
5.25, 4.75, 2, 2.5, 1.5, 2.25, 1.75, 2, 2.5, 1.5, 2.25, 1.75, 
6, 5.75, 6.5, 5.5, 6.25, 4, 4.5, 3.5, 4.25, 3.75, 3, 3.5, 2.5, 
3.23, 2.75, 1, 1.5, 0.5, 1.25, 0.75), Collembola_Species = c("Armata", 
"Armata", "Armata", "Armata", "Armata", "Tricompata", "Tricompata", 
"Tricompata", "Tricompata", "Tricompata", "Armata", "Armata", 
"Armata", "Armata", "Armata", "Tricompata", "Tricompata", "Tricompata", 
"Tricompata", "Tricompata", "Armata", "Armata", "Armata", "Armata", 
"Armata", "Tricompata", "Tricompata", "Tricompata", "Tricompata", 
"Tricompata", "Armata", "Armata", "Armata", "Armata", "Armata", 
"Tricompata", "Tricompata", "Tricompata", "Tricompata", "Tricompata"
), Fungi_Species = c("Asperellus", "Asperellus", "Asperellus", 
"Asperellus", "Asperellus", "Asperellus", "Asperellus", "Asperellus", 
"Asperellus", "Asperellus", "Hamatus", "Hamatus", "Hamatus", 
"Hamatus", "Hamatus", "Hamatus", "Hamatus", "Hamatus", "Hamatus", 
"Hamatus", "Asperellus", "Asperellus", "Asperellus", "Asperellus", 
"Asperellus", "Asperellus", "Asperellus", "Asperellus", "Asperellus", 
"Asperellus", "Hamatus", "Hamatus", "Hamatus", "Hamatus", "Hamatus", 
"Hamatus", "Hamatus", "Hamatus", "Hamatus", "Hamatus"), Temperature = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("15", "25"), class = "factor"), 
    Collembola_species = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L), .Label = c("Armata", "Tricompata"), class = "factor"), 
    Fungi_species = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("Asperellus", "Hamatus"), class = "factor"), 
    moustache_species = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L), .Label = c("Armata", "Tricompata"), class = "factor"), 
    papardelli_species = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L), .Label = c("Asperellus", "Hamatus"), class = "factor"), 
    papardeli_species = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L), .Label = c("Asperellus", "Hamatus"), class = "factor")), row.names = c(NA, 
-40L), class = c("tbl_df", "tbl", "data.frame"))
Vincent Guillemot
  • 3,394
  • 14
  • 21
  • You will be most likely to get your question answered if you follow the [helpful advice](https://stackoverflow.com/help/how-to-ask) about asking questions and create a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – DaveArmstrong May 03 '22 at 13:02
  • Please share your data using `dput(df)`? – Quinten May 03 '22 at 13:05
  • (1) You should check that the levels of your two factors match (they seem to be encoding the same information) and (2) Do you mean "shape = moustache_species" instead of "form = moustache_species"? – Vincent Guillemot May 03 '22 at 13:08
  • I tried to share the data. – Léa Prasin May 03 '22 at 13:13
  • @VincentGuillemot I tried to change the "form" to "shape" but it doesn't solve the problem – Léa Prasin May 03 '22 at 13:15

1 Answers1

0

I think you can solve your problem by factorising the aesthetic parameters:

ggplot(fakedata, aes(x = Temperature,
                     y = Fitness, 
                     color = Fungi_species, 
                     fill = Fungi_species, 
                     shape = Collembola_species)) + 
  geom_boxplot(size = 0.2, color = "black", 
               position = position_dodge2(0.75)) +
  stat_summary(geom = "pointrange", size = 1,
               position = position_dodge2(0.75)) +
  geom_point(position = position_dodge(0.75), 
             alpha = 0.7, size = 2) + 
  scale_fill_manual(values = c("forestgreen", "grey")) +
  scale_color_manual(values = c("red", "cyan")) +
  theme_bw() +
  ylab("Fitness")+ 
  xlab("Temperature") +
  ylim(0, 7) 

I am not sure why you wanted to add a "stat_summary" with the mean, since the median is already informative enough (I think) and is represented in the boxplot. I replaced it with a "pointrange", to show an alternative to boxplots...

Resulting boxplots

Vincent Guillemot
  • 3,394
  • 14
  • 21
  • Thank you all very much for your help ! We found a way by adding position = Fungi_species inside the aes() of the points ! – Léa Prasin May 03 '22 at 13:39