gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:19:25 26 4
gpt4 key购买 nike

我想在垂直线的顶部添加文本,我添加到 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com