1

I've looked at How do I use color in a geom_dotplot? , but I don't understand how it applies to my current use case.

For this example:

dfc <- structure(list(g = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("1", "2"), class = "factor"), r = c(9, 2, 
2, 10, 7, 5.5, 5.5, 8, 4, 2)), class = "data.frame", row.names = c(NA, 
-10L))

I would like to get the points from both groups stacked appropriately, with appropriate colors. If I specify group=1 I get the right stacking but no colors:

library(ggplot2)
ggplot(dfc, aes(color=g, fill=g)) + geom_dotplot(aes(x=r, group = 1), binwidth = 0.2)

enter image description here

If I don't specify group=1 I get the colors, but the points are stacked:

ggplot(dfc, aes(color=g, fill=g)) + geom_dotplot(aes(x=r), binwidth = 0.2)

enter image description here

I can get close with ggstance::position_dodgev(), but not quite:

ggplot(dfc, aes(color=g, fill=g)) + geom_dotplot(position=ggstance::position_dodgev(height=0.025), aes(x=r), binwidth = 0.2)

enter image description here

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453

2 Answers2

2

I think this might be what you're looking for. Let me know if this wasn't the answer you're looking for

ggplot(dfc, aes(x=r, y=g, color=g, fill=g)) + 
  geom_dotplot(stackgroups = TRUE, binwidth = 0.25, method = "histodot") +
  scale_x_continuous(breaks = seq(from = 2, to = 10, by = 2))

enter image description here

neuron
  • 1,949
  • 1
  • 15
  • 30
1

Following the linked to post in the question and after some trial and error, I came up with this.

library(ggplot2)

ggplot(dfc, aes(x=r)) + 
  geom_dotplot(
    aes(color = g, fill = g), 
    stackgroups = TRUE,
    binpositions = "all", 
    binwidth = 0.2
  )

enter image description here

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66