Shade part of an R plot

18,163

Solution 1

Just so that you're left with more than just a comment, here's 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). This can also be useful for this sort of thing. Try moving the rect line to the end, and notice that the results still look ok, because the fill color is partially transparent.

Solution 2

I've found this answer to be pretty great for shading background parts of R.

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 starting values for the shaded rectangle, and following arguments c(3,10) are where the shading ends. This creates a shaded region from 1-3 and 7-10.

Share:
18,163
Kevin
Author by

Kevin

Kevin is a lifelong learner and continues to advance his knowledge deeper in biology and also in programming, computer networking, and design. Kevin is a Postdoctoral Research Fellow in the Department of Integrative Biology and Pharmacology at the University of Texas Medical School at Houston in the Levental Lab. His work has contributed to the understanding of the dynamic nature of lipids and proteins in cellular membranes and how dynamic changes in sections of the cell membrane regulate cellular activity and contribute to disease states. Prior to joining the Levental Lab, Kevin received his Ph.D. at the University of Houston (UH) in Biology. His research focused on genome evolution, population genetics of copy number variation, and eukaryotic parasitism. His work involved bench laboratory work as well as programming computer simulations and advanced statistical analysis of large data sets. He was involved with many student activities at UH, including acting treasurer and sports coordinator for the BioScience Graduate Society, founding member and treasurer of Students for Personal Safety, and mentoring undergraduate students in independent research. Kevin also worked at UH as a teacher in microbiology for science major students. Kevin received his Bachelor’s in Science from the University of Houston – Downtown, where he started his research experience under Lisa Morano, Ph.D. and Jeff Flosi, Ph.D. He made the Dean’s list and was awarded a research scholarship for his work on testing the effectiveness of agriculture disease assays. During his undergraduate education, Kevin supported himself through school by working at the law firm Vinson & Elkins.

Updated on November 01, 2022

Comments

  • Kevin
    Kevin over 1 year

    I am trying to make a plot in R that has a portion of the plot grey to emphasize this area. Unlike other examples, I don't want to color an area under a plot, but instead color an area on a plot starting at one area and going to the end of the graph. When I try to use rect() or polygon() it obscures the plots 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 plot 4 lines on the graph. How do I draw a grey box in the background from the 2nd line to the end of the plot?