R ggplot2:用数值标记y轴上的水平线 [英] R ggplot2: Labelling a horizontal line on the y axis with a numeric value
本文介绍了R ggplot2:用数值标记y轴上的水平线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在ggplot中有一条水平线,我想在y轴上标记它的值(7.1)。
库(ggplot2)
/ pre>
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)))
假设后者,这可能更符合您的要求:
plot1 + geom_hline(aes(yintercept = h))+
geom_text(aes(0,h,label = h,vjust = -1))
I have a horizontal line in a ggplot and I would like to label it's value (7.1) on the y axis.
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))
Thank you for your help.
解决方案
It's not clear if you want 7.1 to be part of the y-axis, or if you just want a way to label the line. Assuming the former, you can use scale_y_continuous()
to define your own breaks. Something like this may do what you want (will need some fiddling most likely):
plot1+ geom_hline(aes(yintercept=h)) +
scale_y_continuous(breaks = sort(c(seq(min(df$y), max(df$y), length.out=5), h)))
Assuming the latter, this is probably more what you want:
plot1 + geom_hline(aes(yintercept=h)) +
geom_text(aes(0,h,label = h, vjust = -1))
这篇关于R ggplot2:用数值标记y轴上的水平线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文