ggscatter in R

This work is developed using part of the code from RUFORUM, specifically from 1 hr 24 minutes 55 seconds time-stamp. While watching the training on YouTube, I realized there could be more possibilities in using ggscatter. Good job by Makerere University staff and collaborators.

library(ggpubr)
ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris')
## `geom_smooth()` using formula 'y ~ x'

Faceting the plot

The plot can also be faceted by Species

ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris', facet.by = 'Species')
## `geom_smooth()` using formula 'y ~ x'

Adding ellipse

It is also possible to easily add ellipses around points:

ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris', facet.by = 'Species', ellipse = TRUE)
## `geom_smooth()` using formula 'y ~ x'

Alter alpha of points in ellipses

It is possible to change the transparency of the allipse in revealing points encapsulated by the ellipses.

ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris', facet.by = 'Species', ellipse = TRUE, ellipse.alpha = 0.5)
## `geom_smooth()` using formula 'y ~ x'

Mean point

Further, it is possible to add mean points on the plots. One can thus easily tell the mean point of the data.

ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris', facet.by = 'Species', ellipse = TRUE, ellipse.alpha = 0.5, mean.point = TRUE)
## `geom_smooth()` using formula 'y ~ x'

Star plot

Can also generate spokes from the mean to the data points, that is, star plot.

ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris', facet.by = 'Species', ellipse = TRUE, ellipse.alpha = 0.5, mean.point = TRUE, star.plot = TRUE)
## `geom_smooth()` using formula 'y ~ x'

Themes

Can use any of the many themes from ggplot2 package.

library(ggplot2)
ggscatter(data = iris, x = 'Sepal.Length', y = 'Petal.Length', add = 'reg.line', conf.int = TRUE, cor.coef = TRUE, cor.method = 'pearson', xlab = 'Sepal Length (cm)', ylab = 'Petal Length (cm)', color = 'purple', size = 3, rug = TRUE, title = 'Relationship between Sepal and Petal lengths (cm) of iris', facet.by = 'Species', ellipse = TRUE, ellipse.alpha = 0.3, mean.point = TRUE, star.plot = TRUE, ggtheme = theme_dark())
## `geom_smooth()` using formula 'y ~ x'

Possibilities

There are many more possibilities in ggscatter that one can use to design plot to their liking.

Best of luck coding in R!

References

Allaire, JJ, Yihui Xie, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, Hadley Wickham, Joe Cheng, Winston Chang, and Richard Iannone. 2021. Rmarkdown: Dynamic Documents for r. https://CRAN.R-project.org/package=rmarkdown.
Henry, Lionel, and Hadley Wickham. 2020. Purrr: Functional Programming Tools. https://CRAN.R-project.org/package=purrr.
Kassambara, Alboukadel. 2020. Ggpubr: Ggplot2 Based Publication Ready Plots. https://rpkgs.datanovia.com/ggpubr/.
Müller, Kirill, and Hadley Wickham. 2021. Tibble: Simple Data Frames. https://CRAN.R-project.org/package=tibble.
R Core Team. 2021. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
———. 2019. Stringr: Simple, Consistent Wrappers for Common String Operations. https://CRAN.R-project.org/package=stringr.
———. 2021a. Forcats: Tools for Working with Categorical Variables (Factors). https://CRAN.R-project.org/package=forcats.
———. 2021b. Tidyverse: Easily Install and Load the Tidyverse. https://CRAN.R-project.org/package=tidyverse.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey Dunnington. 2021. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.
Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2021. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.
Wickham, Hadley, and Maximilian Girlich. 2022. Tidyr: Tidy Messy Data. https://CRAN.R-project.org/package=tidyr.
Wickham, Hadley, Jim Hester, and Jennifer Bryan. 2022. Readr: Read Rectangular Text Data. https://CRAN.R-project.org/package=readr.
Xie, Yihui. 2014. “Knitr: A Comprehensive Tool for Reproducible Research in R.” In Implementing Reproducible Computational Research, edited by Victoria Stodden, Friedrich Leisch, and Roger D. Peng. Chapman; Hall/CRC. http://www.crcpress.com/product/isbn/9781466561595.
———. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. https://yihui.org/knitr/.
———. 2021. Knitr: A General-Purpose Package for Dynamic Report Generation in r. https://yihui.org/knitr/.
Xie, Yihui, J. J. Allaire, and Garrett Grolemund. 2018. R Markdown: The Definitive Guide. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown.
Xie, Yihui, Christophe Dervieux, and Emily Riederer. 2020. R Markdown Cookbook. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown-cookbook.