gpt4 book ai didi

R ggplot2 : Labelling a horizontal line on the y axis with a numeric value

转载 作者:行者123 更新时间:2023-12-03 09:41:29 28 4
gpt4 key购买 nike

我在 ggplot 中有一条水平线,我想在 y 轴上标记它的值 (7.1)。

library(ggplot2)
df <- data.frame(y=c(1:10),x=c(1:10))
h <- 7.1
plot1 <- ggplot(df, aes(x=x,y=y)) + geom_point()
plot2 <- plot1+ geom_hline(aes(yintercept=h))

感谢您的帮助。

最佳答案

目前尚不清楚您是否希望 7.1 成为 y 轴的一部分,或者您只是想要一种标记线条的方法。假设前者,您可以使用 scale_y_continuous()定义自己的休息时间。像这样的事情可能会做你想做的(最有可能需要一些摆弄):

plot1+ geom_hline(aes(yintercept=h)) + 
scale_y_continuous(breaks = sort(c(seq(min(df$y), max(df$y), length.out=5), h)))

enter image description here

假设是后者,这可能更像是您想要的:
plot1 + geom_hline(aes(yintercept=h)) +
geom_text(aes(0,h,label = h, vjust = -1))

enter image description here

关于R ggplot2 : Labelling a horizontal line on the y axis with a numeric value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12876501/

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