我在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))
谢谢你的帮助.
我在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)))
假设是后者,这可能更符合你的要求:
plot1 + geom_hline(aes(yintercept=h)) +
geom_text(aes(0,h,label = h, vjust = -1))