我想知道如何防止下面ggplot2图表上的ablines继续在x轴上低于0:
fixef.c <- list(intercept0 = 1.72, slope0 = 0.04, intercept1 = 0.038, slope1 = -0.016)
xy <- as.data.frame(rbind("White/Early Dropout" = c(intercept = fixef.c[[1]], slope = fixef.c[[2]]),
"White/Late Dropout" = c(intercept = fixef.c[[1]] + fixef.c[[3]]*3, slope = fixef.c[[2]]),
"Black/Early Dropout" = c(fixef.c[[1]], slope = fixef.c[[2]] + fixef.c[[4]]),
"Black/Late Dropout" = c(fixef.c[[1]] + fixef.c[[3]]*3, slope = fixef.c[[4]] + fixef.c[[2]])))
xy$design <- rownames(xy)
ggplot() +
xlim(0, 12) +
ylim(1.6, 2.4) +
geom_abline(data = xy, aes(intercept = intercept, slope = slope, linetype = design, color = design)) +
scale_linetype_manual(values = c("White/Early Dropout" = "solid", "White/Late Dropout" = "dashed", "Black/Early Dropout" = "solid", "Black/Late Dropout" = "dotdash")) +
scale_color_manual(values = c("White/Early Dropout" = "black", "White/Late Dropout" = "black", "Black/Early Dropout" = "grey", "Black/Late Dropout" = "grey"))
答案 0 :(得分:3)
我认为你可以,geom_segment
可以很轻松地完成这项任务:
library(dplyr)
xy <- xy %>% mutate(x=0, y=intercept, xend=12, yend=intercept+12*slope)
ggplot() +
xlim(0, 12) +
ylim(1.6, 2.4) +
geom_segment(data = xy, aes(x=x, xend=xend, y=y, yend=yend, linetype = design, color = design)) +
scale_linetype_manual(values = c("White/Early Dropout" = "solid", "White/Late Dropout" = "dashed", "Black/Early Dropout" = "solid", "Black/Late Dropout" = "dotdash")) +
scale_color_manual(values = c("White/Early Dropout" = "black", "White/Late Dropout" = "black", "Black/Early Dropout" = "grey", "Black/Late Dropout" = "grey"))