我正在尝试使用ggplot vline和hline在线图上显示截距,但希望这些线在图上的截断点处停止。这可能在ggplot中还是有其他解决方案
library(ggplot2)
pshare <- data.frame()
for (i in 1:365) {
pshare <- rbind(pshare,c(i, pbirthday(i,365,coincident=3)))
}
names(pshare) <- c("number","probability")
x25 <- qbirthday(prob = 0.25, classes = 365, coincident = 3) #61
x50 <- qbirthday(prob = 0.50, classes = 365, coincident = 3)
x75 <- qbirthday(prob = 0.75, classes = 365, coincident = 3)
p <- qplot(number,probability,data=subset(pshare,probability<0.99))
p <- p + geom_vline(xintercept = c(x25,x50,x75))
p <- p + geom_hline(yintercept = c(0.25,0.5,0.75))
p
因此,举例来说,我希望0.25/61行在绘图上相遇时结束
TIA
最佳答案
通过@joran将评论扩展为答案和示例geom_vline
绘制整个情节。这就是它的目的。 geom_segment
仅在特定端点之间绘制。它有助于制作一个带有用于画线的相关信息的数据框。
probs <- c(0.25, 0.50, 0.75)
marks <- data.frame(probability = probs,
number = sapply(probs, qbirthday, classes=365, coincident=3))
这样,使线条仅到达交叉点就容易了。
qplot(number,probability,data=subset(pshare,probability<0.99)) +
geom_segment(data=marks, aes(xend=-Inf, yend=probability)) +
geom_segment(data=marks, aes(xend=number, yend=-Inf))
关于r - 有没有办法限制ggplot2中的vline长度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9085104/