【问题标题】:Visualizing two or more data points where they overlap (ggplot R)可视化两个或多个重叠的数据点(ggplot R)
【发布时间】:2018-06-05 22:06:37
【问题描述】:

我有一个带有颜色编码数据点的散点图。当两个或多个数据点重叠时,仅显示一种颜色(以图例中的第一个颜色为准)。这些数据点中的每一个都代表一个项目,我需要显示哪些项目落在规模上的每个点上。我正在使用 R (v.3.3.1)。对于我如何证明散点图上的每个点都有多个项目,有人会有什么建议吗? 提前致谢。

pdf('pedplot.pdf', height = 6, width = 10)
p3 <- ggplot(data=e4, aes(x=e4$domain, y=e4$ped)) + geom_point(aes(color = 
    e4$Database_acronym), size = 3, shape = 17) + 
    labs(x = "Domains", y = "Proportion of Elements per Domain", color = "Data 
    Sources") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) 
p3 dev.off();

【问题讨论】:

  • aes 中重述数据框名称是不必要且不可取的。所以代码应该是:ggplot(data=e4, aes(x=domain, y=ped)) + geom_point(aes(color = Database_acronym), size = 3, shape = 17)
  • 您的问题因缺少可重现的示例而被标记为“离题”。下面的几个答案都有可重复的例子。我鼓励您将其中一个复制到您的问题中。

标签: r ggplot2 scatter-plot


【解决方案1】:

除了here, 提到的抖动之外,您还可以考虑使这些点部分透明:

linecolors <- c("#714C02", "#01587A", "#024E37")
fillcolors <- c("#9D6C06", "#077DAA", "#026D4E")

# partially transparent points by setting `alpha = 0.5`
ggplot(mpg, aes(displ, cty, colour = drv, fill = drv)) +
  geom_point(position=position_jitter(h=0.1, w=0.1),
             shape = 21, alpha = 0.5, size = 3) +
  scale_color_manual(values=linecolors) +
  scale_fill_manual(values=fillcolors) +
  theme_bw()

【讨论】:

    【解决方案2】:

    您可以抖动点,这意味着添加一些噪音以消除重叠(可能是最常用的选项)。另一种选择是使用选择的不同标记形状(加上小尺寸调整),以便标记在绘制时可见。如果您只有两种或三种不同的标记类型,这将起作用。第三种选择是改变每种颜色的尺寸,同样仅适用于可能有两种或三种颜色/尺寸的情况,尽管尺寸差异可能会令人困惑。如果您可以有多个具有相同坐标的相同颜色的点,那么只有抖动(在上述三个选项中)会使这一点变得明显。无论如何,以下是每种方法的示例:

    dat = data.frame(x=1:5, y=rep(1:5,3), group=rep(LETTERS[1:3],each=5))
    theme_set(theme_bw())
    
    # Jitter
    set.seed(3)
    ggplot(dat, aes(x,y, colour=group)) +
      geom_point(size=3, position=position_jitter(h=0.15,w=0.15))
    
    # Vary the marker size
    ggplot(dat, aes(x,y, colour=group,size=group)) +
      geom_point() +
      scale_color_manual(values=c("red","blue","orange")) +
      scale_size_manual(values=c(5,3,1))
    
    # Vary the marker shape (plus a small size adjustment)
    ggplot(dat, aes(x,y, colour=group, size=group, shape=group)) +
      geom_point(stroke=1.5) +
      scale_colour_manual(values=(c("black", "green", "orange"))) +
      scale_shape_manual(values=c(19,17,4)) +
      scale_size_manual(values=c(4,3,3))
    

    【讨论】:

      【解决方案3】:

      如何使用不同的形状和填充?

      ggplot(mpg, aes(displ, cty,  fill = drv, shape = drv)) +
           geom_point(position=position_jitter(h=0.1, w=0.1), alpha = 0.5, size = 3) +
           scale_fill_manual(values=c("red","blue","orange")) +
           scale_shape_manual(values= c(23, 24, 25)) +
           theme_bw()
      

      【讨论】:

        【解决方案4】:

        试试geom_point(aes(color = e4$Database_acronym), position = "jitter", size = 3, shape = 17)

        这会为您的散点图添加一点随机变化,从而防止过度绘制。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-15
          • 2018-06-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-14
          • 1970-01-01
          相关资源
          最近更新 更多