ggplot2 facets allow to split a plot into panels depending on one (or more) categorical variables.
ggplot2 themes configure plot graphical settings: colors, fonts, …

➡️Go to RStudio Cheatsheets/Data Visualization Cheatsheet/Panel Scales to check commands for facets and themes.

Storing plot in a variable

ggplot is a function and it returns an object (of class ggplot) representing a plot.
This object can be stored in a variable.
Notice, that no plot is shown by the following code:

p <- ggplot( pulse ) +
  aes( x = weight, y = height, color = exercise, shape = gender ) +
  geom_point( size = 3, alpha = 0.8 )

Only once the variable is printed the plot is shown.

p

Saving plots

Such a plot object may also be saved to a file with the ggsave function.
In Help you may find how to specify width, height and dpi resolution of the image.
Multiple file formats are supported and by default they are detrmined from the filename.
For example, to save plot p in PNG format:

ggsave( "my_plot.png", plot = p )

Plot size in R Markdown

It is possible to control the dimensions of the plots in your R Markdown report.
Try to add additional options to the first line of a chunk which produces a plot.
Follow this example (the dimensions fig.width and fig.height are specified in inches and dpi sets resolution in pixels per inch):

```{r fig.width=3,fig.height=2,dpi=75}
p
```

Themes

ggplot2 allows detailed configuration of plots, far beyond the scope of this course.
We advise to use google search with phrases like “ggplot2 rotate axis labels”.

Let’s again use the plot stored in the variable p and combine it with general

themes.
Try each of the following lines and observe the effects:

p + theme_minimal()
p + theme_dark()
p + theme_bw()

Plot Types

Generally speaking, ggplot2 geoms specify plot types.
Each geom produces a plot layer and multiple layers can be combined.
Here we demonstrate several frequently used geoms.
Try to regenerate the plots in your R Markdown document.

➡️Go to RStudio Cheatsheets/Data Visualization Cheatsheet/Panel Scales to see numerous geoms provided by the library.

➡️Go to The R Graph Gallery to see how R (often with ggplot2 library) can be used for data visualisation.

Histograms

Let’s start with the histogram of the pulse2 variable from the pulse data:

ggplot( pulse ) +
  aes( x = pulse2 ) +
  geom_histogram( color = "black", fill = "gray" )
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Warning: Removed 1 rows containing non-finite values (stat_bin).

Note, that there were two groups of subjects: one did run, the other did not.
Try to add color to split histogram bars to make groups visible:

ggplot( pulse ) +
  aes( x = pulse2, fill = ran ) +
  geom_histogram( color = "black" )
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Warning: Removed 1 rows containing non-finite values (stat_bin).

The above histogram has the groups stacked.
To visualize each group separately, try to add position argument as below (remember to add alpha; otherwise some bars might get hidden):

ggplot( pulse ) +
  aes( x = pulse2, fill = ran ) +
  geom_histogram( color = "black", position = "identity", alpha = 0.6 )
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Warning: Removed 1 rows containing non-finite values (stat_bin).

An another possible value of the position argument:

ggplot( pulse ) +
  aes( x = pulse2, fill = ran ) +
  geom_histogram( color = "black", position = "dodge" )
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Warning: Removed 1 rows containing non-finite values (stat_bin).



Copyright © 2022 Biomedical Data Sciences (BDS) | LUMC