【问题标题】:如何绘制一条与ggplot2绘图线相交的水平线和垂直线?
【发布时间】:2022-01-21 23:28:33
【问题描述】:

我做了一个用线连接点的图。在这个图中,我想画一条从 x 轴上的点到与绘图线相交的垂直线。在相交处,我想画一条到 Y 轴的水平线。我已经搜索了几个网站,论坛和教程,但我仍然无法做到。有什么帮助吗?

library(ggplot2)

X <- 1:5
Y <- c(2, 6, 4, 7, 12)

DF <- data.frame(X, Y)

ggplot(data = DF,
       aes(x = X,
           y = Y)) +
  geom_point() +
  geom_line() +
  geom_vline(xintercept = 4.5,
             linetype = 2)

目前的结果:

期望结果示例:

【问题讨论】:

  • 使用 geom_segment geom_segment(aes(x=X,xend=X,y=0,yend=Y),linetype=2)+ geom_segment(aes(x=0,xend=X,y=Y,yend=Y),linetype=2)

标签: r ggplot2 line geom-hline geom-vline


【解决方案1】:

@Eric 已经发表评论,但@akrun 在link 中也有类似的回答,通过将apply 函数应用于@akrun 的函数,您还可以绘制DF 的所有点,像这样会为你工作:

library(ggplot2)
X <- 1:5
Y <- c(2, 6, 4, 7, 12)
DF <- data.frame(X, Y)

draw_guides<- function(x, y) {
  list(geom_segment(aes(x = -Inf, xend = x, y = y, yend = y), linetype = "dashed"),
       geom_segment(aes(x = x, xend = x, y = y, yend = -Inf), linetype = "dashed"))
}

ggplot(data = DF,
       aes(x = X,
           y = Y)) +
  geom_point() +
  geom_line() +
  apply(DF, 1, function(y) draw_guides(y[1], y[2]))

【讨论】:

    【解决方案2】:

    正如@Eric 在他的评论geom_segment 中已经提到的那样,这是实现您想要的结果的方法。此外,您必须手动计算y 值,其中段应该切割geom_line,这可以使用approx 来实现。快速方法可能如下所示:

    library(ggplot2)
    
    X <- 1:5
    Y <- c(2, 6, 4, 7, 12)
    
    DF <- data.frame(X, Y)
    
    # vertical line
    vsegment <- function(x, X, Y) {
      geom_segment(aes(x = x, xend = x, y = -Inf, yend = approx(X, Y, x)$y),
                   linetype = 2)  
    }
    # horizontal line
    hsegment <- function(x, X, Y) {
      geom_segment(aes(x = -Inf, xend = x, y = approx(X, Y, x)$y, yend = approx(X, Y, x)$y),
                   linetype = 2)  
    }
    ggplot(data = DF,
           aes(x = X,
               y = Y)) +
      geom_point() +
      geom_line() +
      vsegment(4.5, X, Y) +
      hsegment(4.5, X, Y)
    

    【讨论】:

      【解决方案3】:

      您也可以为此使用 geom_path()

      X <- 1:5
      Y <- c(2, 6, 4, 7, 12)
      
      DF <- data.frame(X, Y)
                
      ggplot(data = DF, aes(x = X, y = Y)) +
        geom_point() +
        geom_line() +
        geom_path(data = data.frame(x = c(-Inf, 4.5, 4.5), y = c(approx(X, Y, 4.5)$y, approx(X, Y, 4.5)$y, -Inf)), aes(x, y), color = "red", linetype = 2)
      

      如果您希望它更灵活地进行更多拦截,您可以使用此功能。 注意... 部分,因此您可以传递geom_path 参数,如颜色、线型、大小等。它支持基于x 值或基于y 值的截距。

      my_intercept <- function(x, y, ...) {
        if (!missing(x)) dt <- data.frame(x = c(-Inf, x, x), y = c(approx(X, Y, x)$y, approx(X, Y, x)$y, -Inf))
        if (!missing(y)) dt <- data.frame(x = c(-Inf, approx(Y, X, y)$y, approx(Y, X, y)$y), y = c(y, y, -Inf))
        geom_path(data = dt, aes(x, y), ...)
      }
      
      ggplot(data = DF, aes(x = X, y = Y)) +
        geom_point() +
        geom_line() +
        my_intercept(x = 4.5, color = "blue", linetype = 2) +
        my_intercept(y = 5, color = "red", linetype = 4)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-23
        • 2015-04-04
        • 1970-01-01
        • 2018-01-29
        • 1970-01-01
        • 1970-01-01
        • 2014-05-04
        • 1970-01-01
        相关资源
        最近更新 更多