Here is a dataset that resembles my own :
set.seed(1)
CRD2 <- data.frame(Response = c(rnorm(10, 5, 1), rnorm(10, 4, 1), rnorm(10, 3, 1)),
Treatment = c(rep("A", 10), rep("B", 10), rep("C", 10)),
Labels = rep(1:5, each = 2))
Using ggplot2 I made the following plot with the code below:
CRD2 %>%
ggplot(aes(x = Treatment, y = Response, fill = Treatment)) +
geom_boxplot() +
geom_dotplot(binaxis = "y", stackdir = "center") +
geom_text(aes(label = Labels), size = 3, fontface = "bold")
My expected output is to produce a plot in which, for every level of 'Treatment', all the observations that have the same 'Response' value are put side by side but aligned to the center of the boxplot. In addition to that, I want the values of the 'Labels' variable of my dataset to be inside of each of the dots.
So far, the geom_dotplot() function is the one that gives me all the observations with the same 'Response' value in the possition I want. However, I can't make the text to be inside the dot. I have no need to stick to geom_dotplot or geom_text so whatever suggestion is welcomed :)
What I've tried so far : In addition to the code to produce the plot above, I also tried using geom_point and geom_text with the same jitter, as is shown in the following code:
set.seed(1)
CRD2 %>%
ggplot(aes(x = Treatment, y = Response, fill = Treatment)) +
geom_boxplot() +
geom_point(aes(color = Treatment), position = position_jitter(width = 0.5, height = 0), size = 3) +
geom_text(aes(label = Labels), size = 3, fontface = "bold", position = position_jitter(width = 0.5, height = 0))
However, it didn't produce the plot I'm expecting to generate since the dots are not aligned to the center and there is no way to make the label to be inside the dots even if I use the same width for the jitter of both, geom_text and geom_point.
EDIT : I changed the title from "add labels inside dots aligned to the center of each boxplot" to "Add geom_text into geom_dotplot 's dots" because this is closer to what I wanted originally.





