【问题标题】:plot() in R - how to shade an area between two vertical lines?R中的plot() - 如何在两条垂直线之间遮蔽区域?
【发布时间】:2021-08-14 15:03:15
【问题描述】:

如何在图中用两条红色垂直线标记的区域着色? (阴影区域不能被曲线限制)

plot(1980:2019,y,type="l")
abline(v=1990,col="red")
abline(v=2001,col="red")

提前致谢。

【问题讨论】:

  • 我觉得这种事情用ggplot做起来要容易得多。
  • 感谢您的回答。你能解释一下它是如何与ggplot一起工作的吗?那太好了..
  • 好的,你能分享一些你的数据吗?它使帮助更容易
  • 您可以将一些您使用的内容放在帖子中,甚至更好的是,还可以提供所需输出的示例
  • 当然。 x

标签: r plot colors


【解决方案1】:

如图所示使用rect。 alpha 值越低,透明度越高。

y <- 1980:2019
plot(y, y)

rect(xleft = 1999, xright = 2001, ybottom = par("usr")[3], ytop = par("usr")[4], 
  border = NA, col = adjustcolor("blue", alpha = 0.3))

【讨论】:

    【解决方案2】:

    我会使用 ggplot 来做这个:

        library(ggplot2)
        df = cbind(1980:2019,runif(40,0,1))
        df=as.data.frame(df)
        ggplot() + 
          geom_rect(aes(xmin=1990, xmax=2001,ymin=-Inf,ymax=Inf), fill='red', alpha= 0.3)+
          geom_line(data=df,aes(x=V1,y = V2), color = "darkred")+
          theme_classic()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      相关资源
      最近更新 更多