Make paired, inverted histograms

I would like to make paired dotplot histograms for two groups in many different tests with two groups shown in opposite directions along the y axis. Using this simple dataset

dat <- data.frame(score = rnorm(100), group = rep(c("Control", "Experimental"), 50), test = rep(LETTERS[1:2], each=50)) 

I can make faceted dots like this

 ggplot(dat, aes(score, fill=group)) + facet_wrap(~ test) + geom_dotplot(binwidth = 1, dotsize = 1) 

faceted dotplot example

but I want the control points to point down, not up. Using this question and answer , I can make a version of the histogram that looks more or less similar to what I want

 ggplot() + geom_histogram(data=subset(dat, group=="Experimental"), aes(score, fill="Experimental", y= ..count..)) + geom_histogram(data=subset(dat, group=="Control"), aes(score, fill="Control", y= -..count..)) + scale_fill_hue("Group") 

paired, inverted histograms but now the cut has disappeared. I know that I could do the cut manually using grid.arrange , but it would be time consuming (my actual dataset had a lot of tests, not just 2), is there a more elegant solution?

Two subsequent questions:

  • geom_histogram gives me a warning saying that "Stacking is not defined when ymin! = 0". Does anyone know how this is "undefined"? In other words, is this what I should worry about?
  • I would rather use dotplot instead of a histogram, but the inversion does not seem to work for dotplot. Why is this? Any ideas how to make it work?

Thanks in advance!

+4
source share
1 answer

A careful reading of geom_dotplot will pay dividends:

 ggplot() + facet_wrap(~test) + geom_dotplot(data=subset(dat, group=="Experimental"), aes(score, fill="Experimental")) + geom_dotplot(data=subset(dat, group=="Control"), aes(score, fill="Control"),stackdir = "down") + scale_fill_hue("Group") 

enter image description here

I did not know about the stackdir argument from the top of my head. I had to check it out!

+3
source

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


All Articles