簡體   English   中英

R 中的 plot() - 如何對兩條垂直線之間的區域進行着色?

[英]plot() in R - how to shade an area between two vertical lines?

如何遮蔽 plot 中標有兩條紅色豎線的區域? (陰影區域不得受曲線限制)

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

提前致謝。

在此處輸入圖像描述

如圖所示使用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))

截屏

我會使用 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()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM