geom_dotplot fill aesthetic changes data grouping

Viewed 59

Adding a fill variable to geom_dotplot introduces an unwanted grouping to the geom. Here's the behavior when you don't add a fill variable:

library(tidyverse)

mtcars %>%
 ggplot(
   aes(
     factor(vs), mpg
   )
 ) +
 geom_dotplot(
   stackdir = "center", 
   binaxis = "y", 
   fill = "white"
 ) +
 coord_flip()

returns

no fill dotplot

I just want to color some dots, for attention purposes, but I don't want the dots to change location. If I try to affect the fill as follows:

mtcars %>%
  rownames_to_column %>% 
  mutate(
    merc = rowname %>%
      str_detect("Merc ") %>%
      as.numeric %>%
      factor
  ) %>% 
  ggplot(
    aes(
      factor(vs), mpg, fill = merc
    )
  ) +
  geom_dotplot(
    stackdir = "center", 
    binaxis = "y", 
  ) +
  coord_flip() +
  theme(
    legend.position = "none"
  )

returns

enter image description here

I want the dots to stay where they are in the first plot, but introduce a fill aesthetic. Is this possible in geom_dotplot?

1 Answers

Two of the geom_dotplot options might be useful here:

binpositions

When method is "dotdensity", "bygroup" (default) determines positions of the bins for each group separately. "all" determines positions of the bins with all the data taken together; this is used for aligning dot stacks across multiple groups.

stackgroups

should dots be stacked across groups? This has the effect that position = "stack" should have, but can't (because this geom has some odd properties).

... + geom_dotplot(alpha = 0.6,   # for testing to show overplotting - none here
                   binpositions = "all", 
                   stackgroups = TRUE,  # to avoid overplotting
                   stackdir = "center", 
                   binaxis = "y", 
      ) 

enter image description here

Related