Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlapping points when using fill aesthetic in ggplot2 geom_dotplot in R

Tags:

r

ggplot2

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

minimal_example_dotplot

like image 602
lgbi Avatar asked Jul 20 '16 09:07

lgbi


1 Answers

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

enter image description here

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)

enter image description here

like image 63
agenis Avatar answered Oct 22 '22 12:10

agenis