Preventing overplotting for specific data points in ggplot2 scatterplot.

huangapple go评论64阅读模式
英文:

Preventing overplotting for specific data points in ggplot2 scatterplot

问题

我正在尝试找到一种方法,可以防止在我的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

理想情况下没有重叠点的散点图示意

我尝试过使用position_jitter,但为了防止重叠,我不得不将所有点都抖动得无法识别它们的相对位置。我还尝试了position_dodge,但是由于某些点需要在x和y轴上移动以防止重叠,我基本上需要手动操作原始数据集中的100个数据点。我认为类似于geom_text_repelgeom_label_repel以最小的方式移动标签,以确保它们不重叠的方法会更理想!附注:这是我在Stack Overflow上的第一篇帖子,对任何错误表示歉意!

英文:

I am trying find a way to prevent points in my ggplot2 scatterplot from overlapping whilst ideally not changing the position of the non-overlapping points (in this example, not moving points with the ids 1, 4, or 5). I would like to maintain the relative position of each of the points, but they can be moved slightly in the x and y as necessary.

library(ggplot2)

df &lt;- 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 &lt;- ggplot(df, aes(x = x, y = y)) +
  geom_point(aes(size = size), alpha = 0.8, shape = 21, color = &quot;black&quot;) +
  geom_text(aes(label = id, size = 0.4*size), color = &quot;black&quot;) +
  scale_size_area(max_size = 8) +
  xlim(1, 10) +
  ylim(1, 10) +
  theme_bw()

plot

Idea of ideal Scatterplot without overlapping points

I have tried position_jitter but to prevent overlap, I had to jitter all of the points so much that their relative positions were unrecognizable. I have also attempted position_dodge, however, as some points need to be moved in the x and some in the y to prevent overlap, I'd basically be doing this manually for 100 data points in my original dataset. I think something similar to the way geom_text_repel and geom_label_repel moves labels the minimum amount so they don't overlap would be ideal! Side note: this is my first post to stack overflow, apologies for any errors!!

答案1

得分: 1

R的基本绘图函数也可以使用抖动函数,这可能适合您的需求。否则,我建议通过添加以下代码来改变ggplot中点的透明度:

geom_point(alpha = 0.1)

此外,您还可以尝试改变点的大小:

geom_point(size=1.5)

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

英文:

The base plot function of R can utilize a jitter function as well which may suit your needs. Otherwise, I would recommend changing the transparency of your points in ggplot by adding

geom_point(alpha = 0.1)

Additionally, you could try changing the size of the points,

geom_point(size=1.5)

You could also try changing the color or shape of the points, but you would need another variable to determine what colors or shapes the points would be.

huangapple
  • 本文由 发表于 2023年3月10日 00:39:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定