When I use aes(fill=...)
to indicate factor levels in a geom_dotplot
, points of different factor levels overlap each other. Especially with large datasets, this becomes troublesome.
Below I have included a minimal example and figure, in which I first plot a dataset without colouring factor levels, and then I add fill
to indicate factor levels, which leads to points overlapping each other. How can I avoid this?
I am aware of a similar question here; however, the answers given do not resolve this issue.
library("ggplot2")
n <- 200
x <- data.frame(x = sample(x = letters[1:3], size = n, replace = TRUE),
y = rnorm(n = n, mean = 0, sd = 1),
a = sample(x = letters[4:5], size = n, replace = TRUE))
p1 <- ggplot(x, aes(x = x, y = y))
p1 <- p1 + geom_dotplot(binaxis = "y", stackdir = "center")
p2 <- ggplot(x, aes(x = x, y = y, fill = a))
p2 <- p2 + geom_dotplot(binaxis = "y", stackdir = "center")
Somehow this combination of arguments, with stackgroups=T combined with binpositions="all" gives a nice result but only centered for the central level of x variable.
ggplot(x, aes(x = x, y = y, fill=a)) +
geom_dotplot(binaxis = "y",
stackdir = "centerwhole",
method="dotdensity",
stackgroups = T,
binpositions="all")
A little bit more complicated construction could yield a result similar to what you want: it uses grid.arrange and a special function to share a common legend (see here for the code of the grid_arrange_shared_legend
function)
for (i in 1:3){
assign(paste0("g", i), ggplot(x %>% filter(x==levels(x$x)[i]), aes(x = x, y = y, fill=a)) + coord_cartesian(ylim=c(-3.5, 3.5))+
geom_dotplot(binaxis = "y", stackdir = "center", method="dotdensity", stackgroups = T, binpositions="all"))
}
grid_arrange_shared_legend(g1, g2, g3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With