R 中的 plot() - 如何遮蔽两条垂直线之间的区域?
plot() in R - how to shade an area between two vertical lines?
我如何遮蔽图中标有两条红色垂直线的区域? (阴影区域不得受曲线限制)
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()
我如何遮蔽图中标有两条红色垂直线的区域? (阴影区域不得受曲线限制)
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()