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

标签 r ggplot2

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

相关文章:

r - 徒手画ggplot。如何改进和/或格式化 plotly?

r - 无法复制此 ggplot2 图

R - 将许多 1D 格式的时间序列转换为 3D 数组,每个时间序列标记有两个标签

R - XTS : Get the first dates and values for each month from a daily time series with missing rows

html - R:使用 rvest 抓取动态链接

r - 如何在函数 ggplot 中添加子图?

r - 在 Shiny 的应用程序中缓存基本 ggplot 并允许动态修改图层(与 ggplot 等效的leafletProxy)

r - 如何比较 R 中多个数据集中同一位置的每个值

r - 函数内的 Missing() 替代方案

r - ggplot2的散点图按日期着色