如何执行`geom_text_repel`如果没有y值?

huangapple go评论132阅读模式
英文:

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。但是它不起作用,因为它需要 xylabel,而我的 aes() 中没有 yy 是使用密度函数计算的。我该怎么办?

英文:

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
英文:

one approach:

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 ## ten density labels
                    )

如何执行`geom_text_repel`如果没有y值?

huangapple
  • 本文由 发表于 2023年7月3日 21:56:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定