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
```

Facets

Let’s use the plot stored in the variable p and combine it with a facet command.
Compare the plot below to the plot above.
Points corresponding to different levels of exercise are shown in separate plot panels.
Note, that a formula of a form y_var ~ x_var is used to specify which variables should define panels in the vertical (y_var) and horizontal (x_var) directions.
. is used when there should be no panels in a given direction.

p + facet_grid( . ~ exercise )

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()

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 © 2023 Biomedical Data Sciences (BDS) | LUMC