gpt4 book ai didi

r - 制作成对的、倒置的直方图

转载 作者:行者123 更新时间:2023-12-04 10:32:04 26 4
gpt4 key购买 nike

我想在一组不同的测试中为两组制作成对的点图直方图,两组在 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 个),有没有更优雅的解决方案?

两个后续问题:
  • geom_histogram给我一个警告,说“当 ymin != 0 时堆叠没有很好地定义”。有谁知道它是如何“定义不明确”的?换句话说,这是我应该关心的事情吗?
  • 我更喜欢使用点图而不是直方图,但反转似乎不适用于点图。这是为什么?任何想法如何让它工作?

  • 提前致谢!

    最佳答案

    细读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争论从我的头顶。我必须查一下!

    关于r - 制作成对的、倒置的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12847196/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com