如何将另一组数据点添加到我的散点图中?

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

我有一个玩具数据集

df

df <- data.frame(
  title = c("Whispers in the Dark", "The Silent Shadows", "Echoes of the Forgotten", "The Vanishing Path", "Haunted Reflections"),
  downloads = c(84902, 78267, 81463, 102784, 112948),
  age_in_days = c(191, 184, 177, 170, 163),
  completion = c(59, 57, 43, 65, 71)
)

使用这些数据,我用以下代码绘制了

age_in_days
downloads
的关系:

library(ggplot2)

ggplot(df, aes(x = age_in_days, y = downloads)) +
  geom_point() +
  geom_text_repel(aes(label = title), size = 3, box.padding = 0.35, point.padding = 0.3) +
  theme_minimal()
}

给出输出:

plot (downloads against age in days)

我想将

completion
数据添加到绘图中,以便在绘图上的每个剧集标题旁边,您还可以看到完成数据(可能在括号中或不同的颜色)。所以情节是一样的,但标签上写着:“闹鬼的倒影(71)”或类似的。

如何在现有绘图上可视化这些数据?

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

使用

paste0
你可以:

library(ggplot2)
library(ggrepel)

ggplot(df, aes(x = age_in_days, y = downloads)) +
  geom_point() +
  geom_text_repel(
    aes(label = paste0(title, " (", completion, ")")),
    size = 3, box.padding = 0.35, point.padding = 0.3
  ) +
  theme_minimal()

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