ggplot2 is a system for declaratively creating graphics, based on The Grammar of Graphics.
You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details.
It is in cran, so it is easy:
Remember than in your script the install.packages()
command should allways be commented and at the beginning of the
script.
There is an official cheatsheet here: https://rstudio.github.io/cheatsheets/html/data-visualization.html
All ggplot2 plots begin with a call to ggplot(),
supplying default data and aesthethic mappings, specified by
aes(). You then add layers, scales, coords and facets with
+. To save a plot to disk, use
ggsave().
For plotting a ggplot2 plot it is necessary to have the data into a dataframe in long format.
Producing a plot with ggplot2, we must give three things:
Let’s make our first ggplot.
### Plot relationship between sepal length and width ----------
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point()The call to ggplot() and aes() sets up the
basics of the data and variables forthe data frame.
aes() defines the “aesthetics”, which is how columns of
the data frame map to graphical attributes such as x and y position,
color, size, etc. Arguments to aes() may refer to columns
of the data frame directly.
We then literally add layers of graphics (“geoms”) to this.
Further aesthetics can be used. Any aesthetic can be either numeric or categorical, an appropriate scale will be used.
### Plot relationship between sepal length and width
### related to Species and Petal.Length -----------------------
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width,
color = Species, size = Petal.Length)) +
geom_point()We can also use different geometries.
### Plot relationship between sepal length and width
### related to Species ----------------------------
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width,
color = Species)) +
geom_smooth() +
geom_point()## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
### boxplot of sepal width for each species ----------------------
g <- ggplot(iris, aes(x = Species, y = Sepal.Width,
fill = Species))
g + geom_boxplot()Besides data, aesthetics and geometries you can add different layers of information. For example, the previous plot can be draw with a different theme.
### boxplot of sepal width for each species --------------------
g +
geom_boxplot() +
geom_jitter() +
theme_bw()To save a particular plot from an R script you can use the command ggsave().
But for Quarto or RMarkdown documents it is better if in the first
setup R chunk you include a fig.path parameter with the
direction where to save all the plots from the document.
The best way to see more features is to look for them in google when needed. Some interesting ones might be:
The ggplot2 reference web, with all the posibilities. Please, click on the different posibilities to get to the help page with examples. https://ggplot2.tidyverse.org/reference/
The R Graph Gallery is a very complete gallery of basic graphs. Each one of them is explained and with code examples. https://www.r-graph-gallery.com/
Statistical tools for high-throughput data analysis (STHDA). A web with good introduccions to different graph types in R. http://www.sthda.com/english/wiki/data-visualization
Other packages for particular plots. https://exts.ggplot2.tidyverse.org/gallery/
The book “R for Data Science” chapters for:
The web “From Data to Viz” https://www.data-to-viz.com/ is a very good place to find the right graph for each type of data. Do not miss the Story link for each data type. It has also a collection of caveats to have in mind to avoid the most common mistakes when choosing the right graph for representign your data https://www.data-to-viz.com/caveats.html.
In a new Rmarkdown document, copy and answer the following questions:
trees data usingGirth as xHeight as yVolume as size and colorgeom_point()geom_line()geom_smooth()geom_smooth(method = "lm")¿Which is the best combination to represent the data?
Cite as: Alfonso Garmendia (2024) R for life sciences. Chapter 6, ggplot: A new way of thinking about graphics. http://personales.upv.es/algarsal/R-tutorials/06_Tutorial-6_R-ggplot.html
Available also in other formats (pdf, docx, …): https://drive.google.com/drive/folders/19w914WCg8BVTVBE_zpgShmg2vpjguV1e?usp=sharing.
Other simmilar tutorials: https://garmendia.blogs.upv.es/r-lecture-notes/
Originals are in bitbucket repository: https://bitbucket.org/alfonsogar/tea_daa_tutorials.
Document written in Rmarkdown, using Rstudio.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.