【问题标题】:How to make grouping tighter on ggplot2 geom_jitter?如何使 ggplot2 geom_jitter 上的分组更紧密?
【发布时间】:2015-09-02 17:53:16
【问题描述】:

尝试将我在 reddit 上发现的政治调查代码重新用于更小的样本量。

我正在使用 geom_jitter 创建散点图。这是我的代码:

ggplot(sae, aes(Alignment, Abortion))+
geom_jitter(aes(color = "green"), size = 4, alpha = 0.6)+
labs("Alignment", "Stance on Abortion")

这是它给出的图表:

如何使围绕“Pro-choice”或“Pro-life”线的分组更紧密?我相信这个当前的图表会让很多人混淆哪些观察结果是有利于选择或有利于生活。

帮助解决颜色问题的额外功劳。

【问题讨论】:

    标签: r graphics ggplot2 scatter-plot


    【解决方案1】:

    你有一个更大的问题。 x 轴按字母顺序排列,这非常令人困惑,可能不是您想要的。此外,您可能需要同时指定width(x 方向抖动)和height(y 方向抖动)。

    您可以使用例如,修复排序,

    sae$Alignment <- factor(sae$Alignment, levels=unique(sae$Alignment))
    

    如下所示。

    # make up some data - you have this already
    set.seed(1)     # for reproducible example
    sae <- data.frame(Alignment=rep(c("Left","Left Leaning","Center","Right Leaning","Right"),each=5),
                      Abortion =sample(c("Pro Choice","Pro Life","Other"),25, replace=TRUE))
    
    # you start here...
    library(ggplot2)
    sae$Alignment <- factor(sae$Alignment, levels=unique(sae$Alignment))
    ggplot(sae, aes(Alignment, Abortion))+
      geom_point(color = "green", size = 4, alpha = 0.6, position=position_jitter(width=0.1, height=0.1))+
      labs("Alignment", "Stance on Abortion")
    

    另外,IMO,你可以做得更好,即。颜色:

    sae$Orientation <- with(sae,ifelse(grepl("Left",Alignment),"Progressive",
                                       ifelse(grepl("Right",Alignment),"Conservative","Neutral")))
    ggplot(sae, aes(x=Alignment, y=Abortion, color=Orientation))+ 
      geom_point(size = 4, alpha = 0.6, position=position_jitter(width=0.1, height=0.1))+
      labs("Alignment", "Stance on Abortion")
    

    【讨论】:

      【解决方案2】:

      您可以在position = position_jitter()中设置width参数来控制点的松紧程度。

      ggplot(sae, aes(Alignment, Abortion)) +
          geom_jitter(color = "green", size = 4, alpha = 0.6, position = position_jitter(width = .2)) +
          labs("Alignment", "Stance on Abortion")
      

      如果您使用的是 ggplot2 的最新开发版本 (1.0.1.9003),则可以改为 geom_jitter(width = .2, ...)

      如果它仍然太宽,请将width 减小到更小的值(反之亦然)。另请注意,为了更改点的颜色,我删除了 aes() 周围的 color = "green"

      【讨论】:

      • 直接在对geom_jitter(...) 的调用中指定width=... 不起作用(无论如何对我来说......)。您需要执行类似geom_jitter(..., position=position_jitter(width=...)) 的操作。请参阅the documentation 中的示例。
      • @jlhoward Aha:这个changed in one of the more recent versions of ggplot2。看起来像1.0.0 release。编辑答案,谢谢!
      • 我明白您所说的修复是什么意思,但我刚刚在 CRAN (1.0.1) 上下载了最新版本,但它仍然无法正常工作。这是您正在使用的开发版本吗?
      • @jlhoward 你是对的:我的错。我正在使用 ggplot2 1.0.1.9003- 感谢您的关注
      【解决方案3】:

      最近才遇到这个问题,上面的答案都没有给出最佳解决方案。

      我在 library(geom_beeswarm) 中使用 geom_beeswarm 找到了一个优雅的答案,并认为我会在这里发布。

      使用 mpg 使用 geom_jitter 进行复制相当混乱:

      data(mpg)
      ggplot(mpg, aes(x=cyl, y=hwy, group=factor(cyl))) +
          geom_boxplot() +
          geom_jitter(position = position_jitter(height = .2, width = .2))
      

      而 geom_beeswarm 使抖动点集中且更清晰:

      library(geom_beeswarm)
      data(mpg)
      ggplot(mpg, aes(x=cyl, y=hwy, group=factor(cyl))) +
          geom_boxplot() +
          geom_beeswarm()
      
      

      【讨论】:

        猜你喜欢
        • 2020-01-06
        • 2022-01-25
        • 2011-10-16
        • 1970-01-01
        • 1970-01-01
        • 2018-02-20
        • 2022-12-15
        • 1970-01-01
        • 2019-10-30
        相关资源
        最近更新 更多