制作成对的倒置直方图

时间:2012-10-11 19:36:17

标签: r ggplot2

我想在一组不同的测试中为两组制作成对的点图直方图,其中两组在y轴上以相反的方向显示。使用这个简单的数据集

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

我可以制作像这样的分面点图

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

faceted dotplot example

但我希望控制点指向下而不是向上。使用this question and answer,我可以制作一个看起来或多或少与我想要的直方图版本

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 但现在面子已经消失了。我知道我可以使用grid.arrange手动进行刻面,但这很费力(我的实际数据集有很多测试,而不仅仅是2),是否有更优雅的解决方案?

两个后续问题:

  1. geom_histogram正在给我一个警告,上面写着“当ymin!= 0时堆叠不明确”。有谁知道它是如何“没有明确定义”的?换句话说,这是我应该关注的事情吗?
  2. 我更喜欢使用dotplot而不是直方图,但反转似乎不适用于dotplot。这是为什么?有什么想法让它起作用吗?
  3. 提前致谢!

1 个答案:

答案 0 :(得分:3)

仔细阅读geom_dotplot会带来好处:

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

我不知道我头顶的stackdir论点。我不得不查一查!