-
Notifications
You must be signed in to change notification settings - Fork 7
/
README.Rmd
213 lines (172 loc) · 6.84 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
warning = FALSE,
message = FALSE
)
```
# camcorder <img src='man/figures/logo.png' align="right" height="200" />
<!-- badges: start -->
[![Codecov test coverage](https://codecov.io/gh/thebioengineer/camcorder/branch/main/graph/badge.svg)](https://app.codecov.io/gh/thebioengineer/camcorder?branch=main)
[![R-CMD-check](https://github.com/thebioengineer/camcorder/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/thebioengineer/camcorder/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
`{camcorder}` is an an R package to track and automatically save graphics generated with `{ggplot2}` that are created across one or multiple sessions with the eventual goal of creating a GIF showing all the plots saved sequentially during the design process.
After installation, the package enables you to:
- save a ggplot automatically every time you run `ggplot()` in any format with given specifications
- generate a GIF that showcases every step of the design process using those image files
- inspect the ggplot output directly with your specifications in the RStudio IDE—you'll get what you see[^f1]
[^f1]: In case you are saving to PDF, the file will automatically open in your default PDF viewer.
## Installation
Currently {camcorder} is only available on GitHub, and can be installed using the following command.
```{r install, eval=FALSE}
# install.packages("camcorder")
remotes::install_github("thebioengineer/camcorder")
```
## Goal End Product
The idea of tracking your plots as part of your development process and generating a making-of movie was popularized by two contributors to this project: Georgios Karamanis and Cédric Scherer. They have wowed the R community weekly with their "how its made" videos. Below are a few examples of the goal end products.
<table>
<tr>
<td>
<p><i><a href = 'https://twitter.com/cedscherer/status/1281653392859820032'>TidyTuesday 2020/28</a><br>by Cédric Scherer</i></p>
<img src = "man/figures/cscherer_coffee_ratings.gif" height = "350">
</td>
<td>
<p><i><a href = 'https://mobile.twitter.com/geokaramanis/status/1248147973206413312'>TidyTuesday 2020/15</a><br>by Georgios Karamanis</i></p>
<img src = "man/figures/gkaramanis_tour_de_france.gif" height = "350">
</td>
</tr>
</table>
## How To
To get started, load {camcorder} and initialize recording using the `gg_record()` function.
This function has several options, such as where to save the recordings, device to use to save the recordings, and the height/width of the image to create. By default it will save to a temporary directory so recordings will go away once the R session is closed.
```{r setup}
library(ggplot2)
library(camcorder)
gg_record(
dir = file.path(tempdir(), "recording100"), # where to save the recording
device = "png", # device to use to save images
width = 4, # width of saved image
height = 6, # height of saved image
units = "in", # units for width and height
dpi = 300 # dpi to use when saving image
)
```
Once the recorder is initialized, any ggplot that is made and printed will be automatically (or *automagically*[^f2]) recorded.
[^f2]: A previous typo but actually it fits quite well.
```{r plots-plots-plots, results = "hide"}
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point()
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(shape = as.factor(gear)))
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(color = as.factor(gear)))
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(color = as.factor(gear))) +
geom_path()
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(color = disp)) +
geom_smooth()
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_smooth() +
geom_point(aes(color = disp))
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_smooth() +
geom_point(aes(color = disp)) +
scale_color_viridis_c() +
theme_light()
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_smooth() +
geom_point(aes(color = disp)) +
scale_color_viridis_c() +
theme_light() +
labs(
title = "MPG vs Horse Power!",
subtitle = "Power and economy, the classic compromise!"
)
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_smooth() +
geom_point(aes(color = disp)) +
scale_color_viridis_c() +
theme_light(base_family = "Roboto Mono") +
labs(
title = "MPG vs Horse Power!",
subtitle = "Power and economy, the classic compromise!"
)
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_smooth() +
geom_point(aes(color = disp)) +
scale_color_viridis_c() +
theme_light(base_family = "Roboto Mono") +
labs(
title = "MPG vs Horse Power!",
subtitle = "Power and economy, the classic compromise!",
x = "Efficiency (Miles/Gallon)",
y = "Power (Horsepower)",
color = "Displacement\n(Cubic Inch)"
)
```
If at any point, that you want to save your plots in a different format than what the recorder was initialized with this can be done through the `gg_resize_film()` function. This will set the size and dpi of all plots going forward.
```{r resize}
gg_resize_film(
height = 4,
width = 6,
units = "in",
dpi = 350
)
```
```{r plots-plots-plots-2, results = "hide"}
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_smooth() +
geom_point(aes(color = disp)) +
scale_color_viridis_c() +
theme_light(base_family = "Roboto Mono") +
labs(
title = "MPG vs Horse Power!",
subtitle = "Power and economy, the classic compromise!",
x = "Efficiency (Miles/Gallon)",
y = "Power (Horsepower)",
color = "Displacement\n(Cubic Inch)"
) +
theme(
plot.title.position = "plot",
plot.title = element_text(face = "bold")
)
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_smooth() +
geom_point(aes(color = disp)) +
scale_color_viridis_c() +
theme_light(base_family = "Roboto Mono") +
labs(
title = "MPG vs Horse Power!",
subtitle = "Power and economy, the classic compromise!",
x = "Efficiency (Miles/Gallon)",
y = "Power (Horsepower)",
color = "Displacement\n(Cubic Inch)"
) +
theme(
plot.title.position = "plot",
plot.title = element_text(face = "bold"),
panel.background = element_rect(colour = "turquoise", fill = "turquoise")
)
```
Finally, to generate the final GIF, use the `gg_playback()` function.
The user can define:
- where the final GIF gets saved by setting the `name` argument,
- duration of the first and last images with `first_image_duration` or `last_image_duration`
- delay between frames in seconds with `frame_duration`
```{r create-gif, eval=FALSE}
gg_playback(
name = file.path(tempdir(), "recording", "vignette_gif.gif"),
first_image_duration = 5,
last_image_duration = 15,
frame_duration = .4,
image_resize = 800
)
```
Once rendering is complete, a GIF is opened in your viewer.
![](man/figures/vignette_gif.gif)