r - 如何在ggplot中向geom_vline添加文本

标签 r ggplot2 dplyr

我想在垂直线的顶部添加文本,我添加到 ggplot 中的密度图;诸如“目标长度”之类的东西;似乎在任何地方都不是解决方案。

ggplot(iris, aes(x = Sepal.Length,  fill = Species)) + 
  geom_density(alpha = 0.5) +
  geom_vline(xintercept = 6, linetype="dashed", 
             color = "black", size=1, )

我更喜欢在 geom_label 中有一个可能的框,但 geom_text 的一些变化也足够了。

最佳答案

使用带有 geom="label"的注释?如果只有一个标签,那就更直接了,不会弄乱图例:

g = ggplot(iris, aes(x = Sepal.Length,  fill = Species)) + 
  geom_density(alpha = 0.5) +
  geom_vline(xintercept = 6, linetype="dashed", 
             color = "black", size=1, )

g+annotate(x=6,y=+Inf,label="Target Length",vjust=2,geom="label")

enter image description here

关于r - 如何在ggplot中向geom_vline添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61701985/

相关文章:

r - 在 R 中对命名列表进行排序

R:将列表中每个 < 1 的元素乘以 10,直到该元素 > 1

r data.table lapply 或 for 循环创建变量或生成列

r - 检查连续日期内的两个值是否相同

multithreading - R system()进程始终使用相同的CPU,而不是多线程/多核

从长到宽 reshape 数据 - 了解 reshape 参数

r - 将日志符号放入 ggplot2 文本中

r - 在ggplot2条形图中获取分组条形的中点值

r - 如何使用 geom_bar (ggplot2) 垂直居中标签

r - 按(行)对的组合过滤