I attach graphic, image.jpg, in which I want to draw the line y=0 for x<3 and the line y=1 for x>=8, i.e. the result would be image2.jpg.
These are the instructions for 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
Does anyone know how to do it?
Thanks
CodePudding user response:
An option using base 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'))
Or just
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'))
Data:
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
))
CodePudding user response:
If you want to stick with ggplot2, the geom_segment()
will give what you're looking for. Make a data.frame of the parameters and then add the 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")