Shadow of R

I am trying to make a plot in R, which has part of the gray plot to emphasize this area. Unlike other examples, I do not want to color the area for the plot, but instead I color the area in the area starting from one area and going to the end of the graph. When I try to use rect () or polygon (), it hides the graphics that I want to emphasize.

For example:

x_mean <- c(1, 2, 3, 4) y_mean <- c(1, 1, 1, 1) y_max <- c(4, 4, 4, 4) y_min <- c(-4, -4, -4, -4) x_shade <- c(2, 3, 4) y_max_shade <- c(4, 4, 4) y_min_shade <- c(-4, -4, -4) plot(x=rep(x_mean, 3), y=c(y_mean, y_max, y_min), bty='n', type="n" ) arrows(x0=x_mean, y0=y_min, x1=x_mean, y1=y_max, length=0) points( x=x_mean, y=y_mean, pch=16) 

This will build 4 lines on the chart. How to draw a gray field in the background from the second line to the end of the graph?

+7
source share
2 answers

Just so that you have not only a comment, but also a possible solution:

 plot(x=rep(x_mean, 3), y=c(y_mean, y_max, y_min), bty='n', type="n" ) rect(2,-4,4,4,col = rgb(0.5,0.5,0.5,1/4)) arrows(x0=x_mean, y0=y_min, x1=x_mean, y1=y_max, length=0) points( x=x_mean, y=y_mean, pch=16) 

enter image description here

Note that I also demonstrated how to use alpha blending in the color specification (using rgb ). It can also be useful for these kinds of things. Try moving the rect line to the end and note that the results still look fine, because the fill color is partially transparent.

+8
source

I found this answer to be beautiful for shading the background parts of R.

In some context:

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

The first two arguments c(1,7) are the initial values ​​for the hatched rectangle, and the next arguments c(3,10) are where the hatch ends. This creates a shaded area from 1-3 to 7-10.

+1
source

All Articles