英文:
How to preform geom_text_repel if you don't have y value?
问题
我有这样的代码:
feature1 %>%
ggplot(aes(x = value, color = str_wrap(name, 20))) +
geom_line(stat = "density", bw = 0.5, size = 1) +
labs(x = "score", color = "Features")
我想要添加 geom_text_repel
。但是它不起作用,因为它需要 x
、y
和 label
,而我的 aes()
中没有 y
,y
是使用密度函数计算的。我该怎么办?
英文:
I have this kind of code:
feature1 %>%
ggplot(aes(x = value, color = str_wrap(name, 20))) +
geom_line(stat = "density", bw = 0.5, size = 1) +
labs(x = "score", color = "Features")
And I want to add geom_text_repel
. But it doesn't work because it requires x, y and label, while I don't have y in my aes(), y is counted using density function. What can I do with that?
答案1
得分: 1
一种方法:
library(ggplot2)
set.seed(123)
data.frame(x = rnorm(1000)) |
ggplot(aes(x = x)) +
geom_line(stat = 'density', bw = .5) +
geom_text_repel(aes(label = round(after_stat(density), 2)),
stat = 'density', bw = .5,
n = 10 ## 十个密度标签
)
[![各点处的标签密度][1]][1]
[1]: https://i.stack.imgur.com/kxclE.png
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论