我使用 geom_count 将重叠点可视化为大小组,但我还想将实际计数作为标签添加到绘制的点上,如下所示:
然而,为了实现这一目标,我必须创建一个包含计数的新数据框,并在 geom_text 中使用这些数据,如下所示:
#Creating two data frames
data <- data.frame(x = c(2, 2, 2, 2, 3, 3, 3, 3, 3, 4),
y = c(1, 2, 2, 2, 2, 2, 3, 3, 3, 3),
id = c("a", "b", "b", "b", "c",
"c", "d", "d", "d", "e"))
data2 <- data %>%
group_by(id) %>%
summarise(x = mean(x), y = mean(y), count = n())
# Creating the plot
ggplot(data = data, aes(x = x, y = y)) +
geom_count() +
scale_size_continuous(range …Run Code Online (Sandbox Code Playgroud)