gpt4 book ai didi

r - 有没有办法限制 ggplot2 中的 vline 长度

转载 作者:行者123 更新时间:2023-12-04 02:50:47 25 4
gpt4 key购买 nike

我正在尝试使用 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))

enter image description here

关于r - 有没有办法限制 ggplot2 中的 vline 长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9085104/

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