【问题标题】:Avoid plot overlay using geom_point in ggplot2避免在 ggplot2 中使用 geom_point 进行绘图叠加
【发布时间】:2015-11-16 17:06:40
【问题描述】:

在 ggplot2 中,geom_point 默认在当前绘图上绘图。例如,在调用 geom_boxplot 后调用 geom_point 会导致在箱线图上绘制点:

ggplot(iris, aes(x = "All", y = Sepal.Length)) +
  geom_boxplot() +
  geom_point(aes(color=Species), position = "jitter") 

有没有办法将点分别绘制到侧面,而不是在箱线图上?

在我的特殊情况下,我想这样做是因为这些点使绘图变得模糊(即使具有透明度等),这与此处的示例数据集无关。

【问题讨论】:

    标签: r ggplot2 data-visualization


    【解决方案1】:

    您可以通过为箱线图和点提供单独的 x 值来分别绘制它们:

    ggplot(iris, aes(y = Sepal.Length)) +
      geom_boxplot(aes(x="Boxplot")) +
      geom_point(aes(x="Points", color=Species), 
                 position = position_jitter(width=0.15, height=0)) 
    

    另一种选择是按物种使用箱线图:

    ggplot(iris, aes(y = Sepal.Length)) +
      geom_boxplot(aes(x="All Data"), width=0.5) +
      geom_boxplot(aes(x="By Species", colour=Species), width=0.5,
                   position=position_dodge(width=0.6)) 
    

    这两个图是这样的:

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 2015-11-23
      • 1970-01-01
      相关资源
      最近更新 更多