代码之家  ›  专栏  ›  技术社区  ›  89_Simple

绘制曲线上的自定义垂直线

  •  2
  • 89_Simple  · 技术社区  · 7 年前

    我希望垂直线在相交后不要超过曲线

    # example data:
    x <- 1:50
    dat <- data.frame(x = x, y = 1 - exp(-x/43)^4)
    
    ggplot(dat, aes(x = x, y = y)) + geom_line() + geom_vline(xintercept = c(10, 20, 30), lty = "dashed")
    

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  3
  •   zx8754    7 年前

    使用 取而代之的是:

    ggplot(dat, aes(x = x, y = y)) + 
      geom_line() + 
      geom_segment(aes(x = x, xend = x, y = min(dat$y), yend = y),
                   data = dat[ dat$x %in% c(10, 20, 30), ],
                   lty = "dashed")
    

    enter image description here