【问题标题】:Add vertical lines connecting point to a horizontal line in a plot in R在R中的绘图中添加垂直线连接点到水平线
【发布时间】:2021-07-02 08:20:06
【问题描述】:

我正在绘制 R 中的一些自相关值:

  plot(y=lag[2:N],x=1:(N-1), xlab="lag",ylab="Autocorrelation",ylim=c(-1,1), pch=16,col="red")
  abline(h=0, col="black")
  abline(h=up, col="blue")
  abline(h=low, col="blue")

这是我的代码,这是我在 R 中得到的

但是,我想要如下图所示的内容,其中我用红线将点连接到 0 处的水平线。

知道怎么做吗?

【问题讨论】:

    标签: r ggplot2 plot line autocorrelation


    【解决方案1】:

    如果使用 ggplot 应该这样做:

    plot_df = data.frame(x = 1:20, y = rnorm(20))
    
    ggplot(plot_df, aes(x, y, ymax = y, ymin = 0)) +
        geom_pointrange(color = "red") +
        geom_hline(yintercept = min(plot_df$y), color = "blue") +
        geom_hline(yintercept = max(plot_df$y), color = "blue")
    

    【讨论】:

      【解决方案2】:

      可以用type = "h"添加垂直线,然后分别添加点

      plot(y=lag[2:N],x=1:(N-1), xlab="lag",ylab="Autocorrelation",ylim=c(-1,1), col="black", type = "h") 
      points(y=lag[2:N],x=1:(N-1), xlab="lag", ylim=c(-1,1), pch=16,col="red", type = "p")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-23
        • 1970-01-01
        • 1970-01-01
        • 2020-08-23
        • 2021-03-07
        • 2021-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多