#r #function #ggplot2 #plot #line
Вопрос:
Я прилагаю графический, image.jpg, в котором я хочу нарисовать линию y=0 для xlt;3 и линию y=1 для xgt;=8, т. Е. Результат будет image2.jpg.
Вот инструкции для image.jpg.
df lt;- data.frame(x=Asignaturas, y=solF) df$xend lt;- c(df$x[2:nrow(df)],NA) df$yend lt;- df$y p lt;- (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
Кто-нибудь знает, как это сделать?
Спасибо
Ответ №1:
Опция с использованием базы 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 lt;- 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 ))
Ответ №2:
Если вы хотите придерживаться ggplot2, geom_segment()
он даст то, что вы ищете. Создайте данные.фрейм параметров, а затем добавьте geom_segment()
.
extraLines lt;- 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 gt; 0), aes(x = x, y=y), colour = "red") geom_point(data = filter(extraLines, x lt; max(df$x)), aes(x=xend, y=y), shape = 1, colour = "red")