shading area between two lines in r

后端 未结 2 807
傲寒
傲寒 2020-12-17 15:50

For example if i need to shade two area in plot

x<-rep(1:10)
plot(x,type=\"h\")

I need as an example shade the area from 1 to 3 and from

相关标签:
2条回答
  • 2020-12-17 16:20

    If I understand you correctly, you can get what you want by using the little-known panel.first= argument to plot.default():

    plot(x,type="h", 
         panel.first = {
             usr <- par('usr')
             rect(c(1,7), usr[3], c(3,10), usr[4], col='green', border=NA)
         })
    

    Or, to avoid any mucking around with par('usr') values, just do:

    plot(x, type="h", 
         panel.first = rect(c(1,7), -1e6, c(3,10), 1e6, col='green', border=NA))
    

    enter image description here

    0 讨论(0)
  • 2020-12-17 16:28

    See ?polygon:

    polygon( x = c(1,1,3,3,1), y=c( usr[1], x[1], x[3], usr[1], usr[1]) ,col="red")
    
    > polygon( x = c(1,1,3,3,1), y=c( usr[1], x[1], x[3], usr[1], usr[1]) ,col="red")
    > polygon( x = c(7,7,10,10,7), y=c( usr[2], x[7], x[10], usr[1], usr[1]) ,col="red")
    

    I chose to close the polygon but seem I remember you can get it to self-close.

    enter image description here

    0 讨论(0)
提交回复
热议问题