-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgraphics-ggplot-extras.Rmd
112 lines (80 loc) · 3.15 KB
/
graphics-ggplot-extras.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
---
title: 'ggplot extras'
---
```{r, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse=TRUE, cache=TRUE, message=F)
library(tidyverse)
library(pander)
library(knitr)
```
## Tricks with ggplot {- #ggplot-details}
### More ways to facet a plot {- #facetting-plots}
Facets are ways to repeat a plot for each level of another variable. `ggplot`
has two ways of defining and displaying facets:
- As a list of plots, using `facet_wrap`.
- As a grid or matrix of plots, using `facet_grid()`.
Examples of both are shown below, using the following plot as a starting point:
```{r}
base.plot <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
base.plot
```
### `facet_wrap` {-}
If we want one facet we just type the tilde (`~`) symbol and then the name of
the variable. This is like typing the right hand side of a
[formula for a regression model](formulae):
```{r}
base.plot + facet_wrap(~cyl)
```
If we want two facets we extend the formula, using the `+` sign:
```{r}
base.plot + facet_wrap(~cyl+am)
```
Note, the order of variables in the formula makes a difference:
```{r}
base.plot + facet_wrap(~am+cyl)
```
### `facet_grid` {-}
With one variable `facet_grid` produces similar output. Note the `.` (period) on
the left hand side of the formula now to make explicit we only have one
variable, and we want it on the x axis:
```{r}
base.plot + facet_grid(.~cyl)
```
We can flip the facets around by putting the `cyl` variable on the left hand
side of the `~`:
```{r}
base.plot + facet_grid(cyl~.)
```
And `facet_grid` can also create facets for two or more variables:
```{r}
base.plot + facet_grid(am~cyl)
```
Here the labelling and the arrangement of plots is perhaps nicer because it is
clearer that plots for `cyl` are arrange left to right, and for `am` they are
top to bottom.
### Combining separate plots in a grid {- #combining-plots}
Note that combining separate plots in a grid is different from
[facetting](#facetting-plots), and it may be you want that instead.
If you really want to combine several plots, the `gridExtra` and `cowplot`
packages can be helpful. This is the code from the
[example in the graphics section](#layered-graphics), which may be a useful
starting point:
```{r}
comparison <- ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot() + ggtitle("Comparison")
relationships <- ggplot(mtcars, aes(wt, mpg, color=factor(gear))) + geom_point() + ggtitle("Relationship")
distributions <- ggplot(mtcars, aes(mpg, color=factor(gear))) + geom_density() + ggtitle("Distribution")
composition <- ggplot(mtcars, aes(factor(cyl), fill = factor(gear))) + geom_bar() + ggtitle("Composition")
mm <- theme(plot.margin=unit(rep(1.5,4), "line"))
gridExtra::grid.arrange(relationships+mm, distributions+mm, comparison+mm, composition+mm, ncol=2)
```
## Exporting for print {- #exporting-graphics}
To export ggplot graphics you can use the `ggsave()` function:
```{r, eval=F}
ggplot(mtcars, aes(wt, mpg)) + geom_point()
ggsave(filename = "myplot.pdf")
```
See the
[ggplot docs on exporting](http://ggplot2.tidyverse.org/reference/ggsave.html)
or page
[323 of the R Graphics Cookbook](https://ase.tufts.edu/bugs/guide/assets/R%20Graphics%20Cookbook.pdf)
for lots more detail.