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