Reputation: 21
I am using geom_dotplot and want to differentiate by color which points are in one group versus another. I have successfully done this by adding "fill=group" to the aes() in geom_dotplot().
Here is some sample code that reproduces the error:
set.seed(124)
df <- data.frame(Group = rep(c("control","treatment"),20), Response = sample(1:10,40, replace = T), Recovered = rep(c("no","no","no","no","yes"),4))
ggplot() + geom_dotplot(data = df, aes(x = Group, y = Response, fill = Recovered),binaxis = "y", stackdir = "center", alpha = 0.3) + coord_flip()
However, now the package no longer stacks the points in one group alongside the other, but instead overlaps them, hiding some of the data.
I can set alpha = 0.5 to see where this overlap occurs, but I would much rather have it plot all the points alongside each other and simply color some of the points. Does anyone know how to do this?
I know I can set position_dodge to a small amount, but would prefer not to as that messes up axis interpretation.
Edit: output of dput(df) is:
structure(list(Group = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), .Label = c("control", "treatment"), class = "factor"), Response = c(1L,
5L, 6L, 4L, 3L, 3L, 6L, 5L, 10L, 3L, 8L, 9L, 8L, 9L, 5L, 1L,
6L, 8L, 9L, 1L, 7L, 7L, 1L, 5L, 4L, 3L, 9L, 3L, 9L, 4L, 9L, 4L,
5L, 9L, 2L, 10L, 2L, 10L, 2L, 4L), Recovered = structure(c(1L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 2L), class = "factor", .Label = c("no",
"yes"))), .Names = c("Group", "Response", "Recovered"), row.names = c(NA,
-40L), class = "data.frame")
Upvotes: 1
Views: 1665
Reputation: 93761
You could add stackgroups=TRUE
. Does that give you the effect you were looking for?
ggplot() + geom_dotplot(data = df,
aes(x = Group, y = Response, fill = Recovered),
binaxis = "y", stackdir = "center", alpha = 1,
stackgroups=TRUE) + coord_flip()
Upvotes: 2
Reputation: 7654
Does jitter
help?
ggplot() + geom_dotplot(data = df, aes(x = Group, y = Response, fill = Recovered),
binaxis = "y", stackdir = "center", alpha = 0.3, position = "jitter") + coord_flip()
Upvotes: 0