For problems with the code, please open an issue on GitHub here.

Contact details: GitHub / seethedatablog / rpubs / LinkedIn / Twitter / Instagram

The Rmarkdown file with the R code used to generate this html report is stored on GitHub here.

1 Summary

In this post I build two graph examples where my colleagues desired some shading effects in order to emphasize certain data. For this task, I used geom_rect() and geom_polygon() from the ggplot2 package. While geom_rect() draws rectangles (as its name suggests), with geom_polygon() one can draw rectangles and more, allowing for more flexibility. geom_polygon() is a bit more complex to use.

2 The R environment

2.1 Load packages

2.2 R Session

## R version 4.0.0 (2020-04-24)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18362)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] dplyr_0.8.5   ggplot2_3.3.0
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.4.6     knitr_1.28       magrittr_1.5     tidyselect_1.0.0
##  [5] munsell_0.5.0    colorspace_1.4-1 R6_2.4.1         rlang_0.4.6     
##  [9] stringr_1.4.0    tools_4.0.0      grid_4.0.0       gtable_0.3.0    
## [13] xfun_0.13        withr_2.2.0      htmltools_0.4.0  ellipsis_0.3.1  
## [17] assertthat_0.2.1 yaml_2.2.1       digest_0.6.25    tibble_3.0.1    
## [21] lifecycle_0.2.0  crayon_1.3.4     purrr_0.3.4      vctrs_0.2.4     
## [25] glue_1.4.0       evaluate_0.14    rmarkdown_2.1    stringi_1.4.6   
## [29] compiler_4.0.0   pillar_1.4.4     scales_1.1.0     pkgconfig_2.0.3

2.3 Package versions

If you want to install a snapshot of the packages as they existed on CRAN at the date of the creation of this document, then can use the checkpoint package. The code below should guarantee the same versions of the packages as they existed on CRAN at the specified point in time.

To be sure that things work correctly and you do not install unnecessary extra packages, use RStudio, creating a project .Rproj file for this tutorial and save it in a separate clean folder. Here you will store either the RMarkdown file that generated this report or an R file with the code that you copy and paste from this html report. Then execute the line of code from below:

The checkpoint functionality consists on scanning for package names in the scripts and text files of your project folder and its subfolder. It scans all R code (.R, .Rmd, and .Rpres files) for library() and require() statements. Then creates a local library into which it installs a copy of the packages required in the project as they existed on CRAN at the specified snapshot date. See details with ?checkpoint after you load library(checkpoint), or here

Warning - Installing older versions of packages in the checkpoint local library, may take up some hundreds of MG.

3 Set ggplot2 theme

This is more convenient, because once set here, is valid for any generated ggplot in this report. Feel free to adapt and change your theme preferences.

4 geom_rect

I’ll use the mtcars dataset for the next examples. Assume we want to produce a scatterplot of mpg explained by hp, two variables in the data frame mtcars. Then we want to draw a horizontal line that would split the cloud of points into two parts, with the intend to shade differently the area above and under the line. This line can be some sort of threshold in your data.

I think the most important aspect here is to realize that geom_rect uses the locations of the four corners (xmin, xmax, ymin and ymax) to draw rectagles.

Here is how you could write the code:

5 geom_polygon

With geom_polygon we can shade more complex areas than just rectangles :)

Let’s assume you fit some model to your data and then want to shade some area under or above the fitting line. You build the model, make the predictions and use these values for plotting.

Fit the model and build a data frame with coordinates for the trend line.

This is the scatterplot with the trendline corresponding to the model we just computed above.

What is important here is that we need to add new coordinates to the data corresponding to the trendline that we just constructed above.

This is the scatterplot with the trendline and shaded areas above and below.

You can further save the graph as png file with ggsave():