-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18 - Themes.Rmd
221 lines (193 loc) · 5.77 KB
/
18 - Themes.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
---
title: "ggplot2 Elegant Graphics"
author: "Brandon Foltz"
date: "2023-03-16"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(ggthemes)
```
```{r}
base <- ggplot(mpg, aes(cty, hwy, color = factor(cyl))) +
geom_jitter() +
geom_abline(color = "grey50", linewidth = 2)
base
```
```{r}
labelled <- base +
labs(
x = "City mileage per gallon",
y = "Highway mileage per gallon",
color = "Cylinders",
title = "Highway and city mileage are highly correlated") +
scale_color_brewer(type = "seq", palette = "Spectral"
)
labelled
```
```{r}
styled <- labelled +
theme_bw() +
theme(
plot.title = element_text(face = "bold", size = 12),
legend.background = element_rect(fill = "white", linewidth = 4, color = "white"),
legend.justification = c(0,1),
legend.position = c(0,1),
axis.ticks = element_line(color = "grey70", size - 0.2),
panel.grid.major = element_line(color = "grey70", size = 0.2),
panel.grid.minor = element_blank()
)
styled
```
```{r}
df <- data.frame(x = 1:3, y = 1:3)
base <- ggplot(df, aes(x, y)) + geom_point()
base + theme_grey() + ggtitle("theme_grey()")
base + theme_bw() + ggtitle("theme_bw()")
base + theme_linedraw() + ggtitle("theme_linedraw()")
base + theme_light() + ggtitle("theme_light()")
base + theme_dark() + ggtitle("theme_dark()")
base + theme_minimal() + ggtitle("theme_minimal()")
base + theme_classic() + ggtitle("theme_classic()")
base + theme_void() + ggtitle("theme_void()")
```
GGthemes
```{r}
library(ggthemes)
base + theme_tufte() + ggtitle("theme_tufte()")
base + theme_solarized() + ggtitle("theme_solarized()")
base + theme_excel() + ggtitle("theme_excel()")
```
Modifying Theme Components
```{r}
base_t <- base + labs(title = "This is a ggplot") + xlab(NULL) + ylab(NULL)
base_t + theme(plot.title = element_text(size = 16))
base_t + theme(plot.title = element_text(face = "bold", color = "red"))
base_t + theme(plot.title = element_text(hjust = 1))
```
```{r}
base_t + theme(plot.title = element_text(margin = margin()))
base_t + theme(plot.title = element_text(margin = margin(t = 10, b = 10)))
base_t + theme(axis.title.y = element_text(margin = margin(r = 10)))
```
```{r}
base + theme(panel.grid.major = element_line(color = "black"))
base + theme(panel.grid.major = element_line(size = 2))
base + theme(panel.grid.major = element_line(linetype = "dotted"))
```
```{r}
base + theme(plot.background = element_rect(fill = "grey80", color = NA))
base + theme(plot.background = element_rect(color = "red", size = 2))
base + theme(panel.background = element_rect(fill = "linen"))
```
```{r}
base
last_plot() + theme(panel.grid.minor = element_blank())
last_plot() + theme(panel.grid.major = element_blank())
```
```{r}
last_plot() + theme(panel.background = element_blank())
last_plot() + theme(
axis.title.x = element_blank(),
axis.title.y = element_blank()
)
last_plot() + theme(axis.line = element_line(color = "grey50"))
```
```{r}
old_theme <- theme_update(
plot.background = element_rect(fill = "lightblue3", colour = NA),
panel.background = element_rect(fill = "lightblue", colour = NA),
axis.text = element_text(colour = "linen"),
axis.title = element_text(colour = "linen")
)
base
theme_set(old_theme)
base
```
Theme Elements
When exporting plots to use in other systems, you might want to make the background transparent with fill = NA.
```{r}
base + theme(plot.background = element_rect(color = "grey50", size = 2))
base + theme(
plot.background = element_rect(color = "grey", size = 2),
plot.margin = margin(2,2,2,2)
)
base + theme(plot.background = element_rect(fill = "lightblue"))
```
```{r}
df <- data.frame(x = 1:3, y = 1:3)
base <- ggplot(df, aes(x, y)) + geom_point()
# Accentuate the axes
base + theme(axis.line = element_line(colour = "grey50", size = 1))
# Style both x and y axis labels
base + theme(axis.text = element_text(color = "blue", size = 12))
# Useful for long labels
base + theme(axis.text.x = element_text(angle = -90, vjust = 0.5))
```
```{r}
df <- data.frame(
x = c("label", "a long label", "an even longer label"),
y = 1:3
)
base <- ggplot(df, aes(x, y)) + geom_point()
base
base +
theme(axis.text.x = element_text(angle = -30, vjust = 1, hjust = 0)) +
xlab(NULL) +
ylab(NULL)
```
```{r}
df <- data.frame(x = 1:4, y = 1:4, z = rep(c("a", "b"), each = 2))
base <- ggplot(df, aes(x, y, colour = z)) + geom_point()
base + theme(
legend.background = element_rect(
fill = "lemonchiffon",
colour = "grey50",
size = 1
)
)
base + theme(
legend.key = element_rect(color = "grey50"),
legend.key.width = unit(0.9, "cm"),
legend.key.height = unit(0.75, "cm")
)
base + theme(
legend.text = element_text(size = 15),
legend.title = element_text(size = 15, face = "bold")
)
```
```{r}
base <- ggplot(df, aes(x, y)) + geom_point()
# Modify background
base + theme(panel.background = element_rect(fill = "lightblue"))
# Tweak major grid lines
base + theme(
panel.grid.major = element_line(color = "gray60", size = 0.8)
)
# Just in one direction
base + theme(
panel.grid.major.x = element_line(color = "gray60", size = 0.8)
)
```
```{r}
base2 <- base + theme(plot.background = element_rect(colour = "grey50"))
# Wide screen
base2 + theme(aspect.ratio = 9 / 16)
# Long and skiny
base2 + theme(aspect.ratio = 2 / 1)
# Square
base2 + theme(aspect.ratio = 1)
```
```{r}
df <- data.frame(x = 1:4, y = 1:4, z = c("a", "a", "b", "b"))
base_f <- ggplot(df, aes(x, y)) + geom_point() + facet_wrap(~z)
base_f
base_f + theme(panel.spacing = unit(0.5, "in"))
base_f + theme(
strip.background = element_rect(fill = "grey20", color = "grey80", size = 1),
strip.text = element_text(colour = "white")
)
#> Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.
```