Overlap points when using fill sketch in ggplot2 geom_dotplot in R

When I use aes(fill=...)factors to indicate levels of factors geom_dotplot, the points of different levels of factors overlap with each other. Especially with large datasets, this becomes troublesome.

Below I have included a minimal example and a figure in which I first create a dataset without color code levels, and then add fillto indicate factor levels, which leads to overlapping points with each other. How can i avoid this?

I know a similar question here ; however, the answers do not fix this problem.

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

minimal_example_dotplot

+4
source share
1 answer

- stackgroups = T binpositions = "all" , x.

ggplot(x, aes(x = x, y = y, fill=a)) + 
  geom_dotplot(binaxis = "y", 
               stackdir = "centerwhole", 
               method="dotdensity",
               stackgroups = T,
               binpositions="all")

enter image description here

, , : grid.arrange (. grid_arrange_shared_legend)

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)

enter image description here

+3

Source: https://habr.com/ru/post/1648479/


All Articles