Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

geom_dotplot with vertical stacking and colors

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading