可视化两个或多个重叠的数据点(ggplot R)

问题描述 投票:4回答:3

我有一个散点图,它有颜色编码的数据点。当两个或多个数据点重叠时,仅显示其中一种颜色(以图例中的第一个为准)。这些数据点中的每一个都代表一个项目,我需要显示哪些项目落在比例尺上的每个点上。我正在使用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();
r ggplot2 scatter-plot
3个回答
7
投票

除了提到的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()

enter image description here


5
投票

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

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))

enter image description here


0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.