Highlighting specific ranges on a Graph in R

3.8k Views Asked by At
library(season)
plot(CVD$yrmon, CVD$cvd, type = 'o',pch = 19,ylab = 'Number of CVD deaths per month',xlab = 'Time')

if i wanted to highlight a region of the graph based on x values say from 1994-1998 how do i do this?

Any thought would be appreciated Thanks.

2

There are 2 best solutions below

1
On BEST ANSWER

Or you could place a rectangle on the region of interest:

rect(xleft=1994,xright = 1998,ybottom=range(CVD$cvd)[1],ytop=range(CVD$cvd)[2], density=10, col = "blue")

Maybe something like this

1
On

You could just color the points in that range another color,

plot(CVD$yrmon, CVD$cvd, type = 'o',pch = 19,ylab = 'Number of CVD deaths per month',xlab = 'Time')
points(cvd ~ yrmon, type="o", pch=19, col="red", 
       data=CVD[CVD$yrmon >= 1994 & CVD$yrmon <= 1998,])

enter image description here