- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 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 = c(10, 15)) +
geom_text(data = data2,
aes(x = x, y = y, label = count),
color = "#ffffff")
有没有办法以更优雅的方式实现这一点(即不需要第二个数据框)?我知道您可以使用 ..n..
访问 geom_count 中的计数,但如果我尝试在 geom_text
中访问它,这是行不通的。
最佳答案
你期待这样吗:
ggplot(data %>%
group_by(id) %>%
summarise(x = mean(x), y = mean(y), count = n()),
aes(x = x, y = y)) + geom_point(aes(size = count)) +
scale_size_continuous(range = c(10, 15)) +
geom_text(aes(label = count),
color = "#ffffff")
更新:如果必须使用 geom_count
,则可以使用以下方法实现预期输出:
p <- ggplot(data = data, aes(x = x, y = y)) +
geom_count() + scale_size_continuous(range = c(10, 15))
p + geom_text(data = ggplot_build(p)$data[[1]],
aes(x, y, label = n), color = "#ffffff")
关于r - 将计数作为标签添加到 geom_count 中的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46851450/
我使用 geom_count 将重叠点可视化为大小组,但我还想将实际计数作为标签添加到标绘点,如下所示: 然而,为了实现这一点,我必须创建一个包含计数的新数据框,并在 geom_text 中使用这些数
我想使用 ggplot2 中的 geom_count 图,但是值的范围太小,图例中断成为出现次数的浮点,例如1 1.5 2 2.5 3 这是一个测试用例: test = mtcars[1:6,] gg
我想根据 ggplot2 的 geom_count 绘制的点的计数来为其着色。 这是我到目前为止所拥有的: ggplot(test3, aes(eleStart, eleLength)) + geo
我想将 geom_line 的粗细设置为沿该路径的数据的比例,与 geom_count 设置点大小的方式相同在这一点上重叠的数据比例,或者找到一个允许我这样做的函数。 如果我可以将其作为计数而不是比例
我是一名优秀的程序员,十分优秀!