-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotting_examples.qmd
More file actions
198 lines (172 loc) · 4.31 KB
/
plotting_examples.qmd
File metadata and controls
198 lines (172 loc) · 4.31 KB
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
---
title: "Plotting Example"
subtitle: "Demonstrates latex math and plotting"
author: "AUTHORS"
date: "`r format(Sys.Date(), '%d %b %Y')`"
# universal output options
number-sections: true
toc: true
toc-depth: 2
fig-width: 8
fig-height: 6
df-print: tibble
embed-resources: true
# format specific output options
format:
# html file
html:
code-tools: true
code-fold: show
toc-float: true
# knitr global chunk options
knitr:
opts_chunk:
fig.path: "plots/"
fig.keep: "all"
dev: ['png', 'pdf']
dev.args:
pdf:
encoding: 'WinAnsi'
useDingbats: false
# rstudio editor options
editor: source
editor_options:
chunk_output_type: console
---
```{r}
#| label: install
#| eval: false
# run this block once in RStudio to install all packages required for this example
install.packages("tidyverse") # tidyverse data and plotting tools
install.packages("latex2exp") # rendering latex equations in plots
```
```{r}
#| label: setup
#| message: false
# load libraries
library(tidyverse)
library(latex2exp)
# source all relevant scripting files
source(file.path("scripts", "plotting_functions.R"))
source(file.path("scripts", "table_functions.R"))
```
# $\LaTeX$ Math
This is just markdown that can include latex math.
$$
\begin{align}
\dot{x} & = \sigma(y-x) \\
\dot{y} & = \rho x - y - xz \\
\dot{z} & = -\beta z + xy
\end{align}
$$
# Simple Plot
```{r}
#| label: fig-simple-plot
#| fig-width: 8
#| fig-height: 6
# generate a simple plot with ggplot2
p1 <- mpg |>
ggplot() +
aes(x = hwy, y = cty, color = drv) +
geom_point()
p1
```
# Polished Plots
## Latex labels
```{r}
#| label: fig-polished-plot-w-latex-labels
#| warning: false
#| fig-width: 6
#| fig-height: 6
# generate a more polished plot
p2 <- mpg |>
# upgrade the color scale to include some latex symbols
mutate(
latex_drv =
# turn into ordered variable (factor) that keeps the order
as_factor(drv) |>
# introduce latex substitutions
fct_recode(
"$\\left(\\frac{\\textbf{rear drv}}{\\beta^2}\\right)$" = "r",
"$\\int_{\\infinity}\\sqrt[2]_{\\textbf{front drv}}$" = "f"),
cyl_info = paste("cyl:", cyl) |> as_factor()
) |>
# start the plot
ggplot() +
aes(x = hwy, y = cty, color = latex_drv) +
geom_point() +
# add latex to color, facet and axis labels
scale_color_discrete(labels = label_latex()) +
facet_wrap(~latex_drv, labeller = label_latex(), ncol = 1) +
labs(x = TeX("my x axis $\\delta^{42}X$ [\U2030]")) +
# use theme_figure from the plotting_functions.R
theme_figure(text_size = 16)
p2
```
```{r}
#| label: fig-large-plot-w-latex-labels
#| warning: false
#| fig-width: 8
#| fig-height: 10
# or with multi facets
p2 + facet_wrap(~latex_drv + cyl_info, labeller = label_latex(multi_line = FALSE))
```
## Regression labels
```{r}
#| label: fig-polished-plot-w-regression-labels
#| warning: false
#| fig-width: 6
#| fig-height: 8
p3 <- p2 +
# add regression
geom_smooth(method = lm, formula = y ~ x, linetype = 2) +
# add regression label to previous plot
geom_text(
data = function(df) {
df |>
group_by(latex_drv) |>
generate_regression_fit_label(
formula = cty ~ hwy,
func = lm,
include_r2 = TRUE
) |>
filter(!has_error)
},
mapping = aes(color = NULL, label = expr_label, x = -Inf, y = Inf),
hjust = -0.1, vjust = 1.3, parse = TRUE, size = 4, show.legend = FALSE
)
p3
```
## Log axis formatting
```{r}
#| label: fig-polished-plot-w-formatte-log-axis
#| warning: false
#| fig-width: 8
#| fig-height: 4
diamonds |>
slice_sample(n = 1000) |>
ggplot() +
aes(x = carat, y = price, color = cut) +
geom_point() +
scale_y_log10(label = label_scientific_log(), guide = "axis_logticks") +
theme_figure(text_size = 16)
```
## Y axis break
```{r}
#| label: fig-polished-plot-w-y-axis-break
#| warning: false
#| fig-width: 8
#| fig-height: 4
p <- diamonds |>
slice_sample(n = 1000) |>
ggplot() +
aes(x = carat, y = price, color = cut) +
geom_point() +
theme_figure(text_size = 16, legend = FALSE)
plot_with_y_gap(
pb = p, pb_ylim = c(0, 1100), pb_breaks = c(0, 250, 500, 750, 1000),
pt = p, pt_ylim = c(9000, 16000), pt_breaks = c(10000, 12500, 15000),
pb_gap_marker_offset = 0.02, pt_gap_marker_offset = 0.03,
pb_gap_marker_width = 0.25, pt_gap_marker_width = 0.2
)
```