[英]Adding horizontal lines in the plot of a step function
我附上圖形 image.jpg,我想在其中繪制 x<3 的線 y=0 和 x>=8 的線 y=1,即結果將是 image2.jpg。
這些是 image.jpg 的說明。
df <- data.frame(x=Asignaturas, y=solF)
df$xend <- c(df$x[2:nrow(df)],NA)
df$yend <- df$y
p <- (ggplot(df, aes(x=x, y=y, xend=xend, yend=yend)) +
geom_vline(aes(xintercept=x), linetype=2,color="grey") +
geom_point() + # Solid points to left
geom_point(aes(x=xend, y=y), shape=1) + # Open points to right
geom_segment() + # Horizontal line
geom_text(aes(label = paste0(solF,''),vjust = -0.5), color = "black") +
ylab("Función de distribucción") +
xlab("Asignaturas"))
p
有誰知道該怎么做?
謝謝
使用基礎 R 的選項。
plot(df$x, df$y, xlim=c(2.5, max(df$x) + .5), ylim=c(0, 1.075), pch=19)
points(df$x + 1, df$y, pch=1)
segments(df$x, df$y, df$x + 1)
text(df$x, df$y + .05, df$y)
sapply(0:1, \(x) lines(2:3 + x*6, rep(x, 2), col='red'))
要不就
plot(df$x, df$y, type='s', xlim=c(2.5, max(df$x) + .5), ylim=c(0, 1.075))
text(df$x, df$y + .05, df$y)
sapply(0:1, \(x) lines(2:3 + x*6, rep(x, 2), col='red'))
數據:
df <- structure(list(y = c(0, 0.14, 0.31, 0.48, 0.67, 0.82, 1), x = c(2,
3, 4, 5, 6, 7, 8)), class = "data.frame", row.names = c(NA, -7L
))
如果您想堅持使用 ggplot2, geom_segment()
將提供您正在尋找的內容。 制作參數的 data.frame,然后添加geom_segment()
。
extraLines <- data.frame(x = c(-Inf, max(df$x)), xend = c(min(df$x), Inf), y = c(2, max(df$y)), yend = c(2, max(df$y)))
p +
geom_segment(data = extraLines, aes(x = x, xend = xend, y = y, yend = yend), colour = "red") +
geom_point(data = filter(extraLines, x > 0), aes(x = x, y=y), colour = "red") +
geom_point(data = filter(extraLines, x < max(df$x)), aes(x=xend, y=y), shape = 1, colour = "red")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.