【问题标题】:How to generate the same plot with "jitter", and how to jitter selected points (not all points)?如何使用“抖动”生成相同的图,以及如何抖动选定点(不是所有点)?
【发布时间】:2015-09-11 16:07:57
【问题描述】:

我想做的是:

a)ggplot 代码产生的情节在每次运行时都相同 [set.seed kind of notion?] 和

b) 的文本标签仅针对具有相同 y 轴值的标签进行抖动 - 不理会其他文本标签。这似乎是某种基于点因子值的条件抖动。

这是一些数据:

dput(df)
structure(list(Firm = c("a verylongname", "b verylongname", "c verylongname", 
"d verylongname", "e verylongname", "f verylongname", "g verylongname", 
"h verylongname", "i verylongname", "j verylongname"), Sum = c(74, 
77, 79, 82, 85, 85, 88, 90, 90, 92)), .Names = c("Firm", "Sum"
), row.names = c(NA, 10L), class = "data.frame")

这是使用 df 的ggplot 代码:

ggplot(df, aes(x = reorder(Firm, Sum, mean), y = Sum)) +
  geom_text(aes(label = Firm), size = 3, show.guides = FALSE, position = position_jitter(height = .9)) +
  theme(axis.text.x = element_blank()) +
  scale_x_discrete(expand = c(-1.1, 0)) +   # to show the lower left name fully
  labs(x = "", y = "", title = "")

请注意,该图的一个版本仍然与 h 和 i 重叠——每次我运行上述代码时,文本标签的位置都会发生变化。

顺便说一句,这个问题conditional jitter 稍微移动了 x 轴上的离散值,但我想(仅)移动 y 轴上的重叠点。

【问题讨论】:

标签: r ggplot2 jitter


【解决方案1】:

一种选择是添加一列来标记重叠点,然后分别绘制它们。更好的选择可能是直接移动重叠点的 y 值,以便我们直接控制它们的位置。我在下面显示了这两个选项。

选项 1(抖动):首先,添加一列来标记重叠。在这种情况下,因为点几乎落在一条线上,所以如果它们的 y 值太接近,我们可以将任何点标记为重叠。如果检查 x 值是否接近也很重要,您可以包含更复杂的条件。

df$overlap = lapply(1:nrow(df), function(i) {
  if(min(abs(df[i, "Sum"] - df$Sum[-i])) <= 1) "Overlap" else "Ignore"
})

在情节中,我将抖动的点涂成红色,以便很容易判断哪些点受到了影响。

# Add set.seed() here to make jitter reproducible
ggplot(df, aes(x = reorder(Firm, Sum, mean))) +
  geom_text(data=df[df$overlap=="Overlap",], 
            aes(label = Firm, y = Sum), size = 3,  
            position = position_jitter(width=0, height = 1), colour="red") +
  geom_text(data=df[df$overlap=="Ignore",], 
            aes(label = Firm, y = Sum), size = 3) +
  theme(axis.text.x = element_blank()) +
  scale_x_discrete(expand = c(-1.1, 0)) +   # to show the lower left name fully
  labs(x = "", y = "", title = "")

选项 2(直接放置):另一个选项是直接控制标签的移动量,而不是采用 jitter 碰巧给我们的任何东西。在这种情况下,我们知道我们想要移动具有相同 y 值的每一对点。如果我们需要担心 x 和 y 值、同一重叠中的两个以上点和/或我们需要移动接近但不完全相同的值,则需要更复杂的逻辑。

library(dplyr)

# Create a new column that shifts pairs of points with the same y-value by +/- 0.25
df = df %>% group_by(Sum) %>%
  mutate(SumNoOverlap = if(n()>1) Sum + c(-0.25,0.25) else Sum)

ggplot(df, aes(x = reorder(Firm, Sum, mean), y = SumNoOverlap)) +
  geom_text(aes(label = Firm), size = 3) +
  theme(axis.text.x = element_blank()) +
  scale_x_discrete(expand = c(-1.1, 0)) +   # to show the lower left name fully
  labs(x = "", y = "", title = "")

注意:要使抖动可重现,请在抖动的绘图代码之前添加set.seed(153)(或您想要的任何种子值)。

【讨论】:

  • 仅供参考 - set.seed(123) 不会使抖动在我手中使用 ggplot2 2.2.1 重现。
猜你喜欢
  • 1970-01-01
  • 2013-10-06
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
  • 1970-01-01
相关资源
最近更新 更多