防止 ggplot2 散点图中特定数据点的过度绘制

问题描述 投票:0回答:1

我正在尝试找到一种方法来防止我的

ggplot2
散点图中的点重叠,同时理想情况下不改变非重叠点的位置(在本例中,不移动 ID 为 1、4 或 5 的点)。我想保持每个点的相对位置,但可以根据需要在 x 和 y 中稍微移动它们。

library(ggplot2)

df <- data.frame(
  id = 1:9,
  x = c(1, 2, 2.1, 4, 5, 8.7, 9.1, 9, 8.9),
  y = c(10, 9, 9, 7, 6, 2.4, 2, 2.1, 2),
  size = rep(1:4, length.out = 9)
)

plot <- ggplot(df, aes(x = x, y = y)) +
  geom_point(aes(size = size), alpha = 0.8, shape = 21, color = "black") +
  geom_text(aes(label = id, size = 0.4*size), color = "black") +
  scale_size_area(max_size = 8) +
  xlim(1, 10) +
  ylim(1, 10) +
  theme_bw()

plot

Idea of ideal Scatterplot without overlapping points

我试过

position_jitter
但是为了防止重叠,我不得不抖动所有的点以至于它们的相对位置无法识别。我也尝试过
position_dodge
,但是,由于需要在 x 中移动一些点,在 y 中移动一些点以防止重叠,我基本上是为原始数据集中的 100 个数据点手动执行此操作。我认为类似于
geom_text_repel
geom_label_repel
将标签移动到最小量以使它们不重叠的方式是理想的!旁注:这是我关于堆栈溢出的第一篇文章,对任何错误表示歉意!

r ggplot2 plot graphics scatter-plot
1个回答
0
投票

R的base plot函数也可以使用jitter函数,这可能适合你的需要。否则,我建议通过添加 geom_point(alpha = 0.1)

来更改 ggplot 中点的透明度

此外,您可以尝试更改点的大小,geom_point(size=1.5)

或者您可以尝试更改点的颜色或形状,但您需要另一个变量来确定点的颜色或形状。

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