我想使用 ggplot 创建一个带有断开线的图表
# Sample data
x <- seq(1:100)
y <- c(rnorm(25, mean = 1, sd = 1), rnorm(25, mean = 2, sd = 1), rnorm(25, mean = 3, sd = 1), rnorm(25, mean = 4, sd = 1))
z <- rep(1:4, each = 25)
tempdf <- data.frame(cbind(x,y,z))
使用代码
ggplot(data=tempdf, aes(x = x)) +
geom_line(aes(y = y), color = "blue") +
geom_line(aes(y = z), color = "red", size = 1)
我得到这个输出
但是我想创建类似的东西(注意红线在步骤中没有连接)
我尝试使用geom_segment但找不到正确的方法
ggplot(data=tempdf, aes(x = x)) +
geom_line(aes(y = y), color = "blue") +
geom_segment(x = x, y = z, xend = x+25, yend = z)



