-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
173 lines (132 loc) · 6.68 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
---
output:
md_document:
variant: markdown_github
html_preview: false
---
[![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/ggghost)](https://cran.r-project.org/package=ggghost)
[![packageversion](https://img.shields.io/badge/Package%20version-0.2.1-orange.svg?style=flat-square)](commits/master)
[![Last-changedate](https://img.shields.io/badge/last%20change-`r gsub('-', '--', Sys.Date())`-yellowgreen.svg)](/commits/master)
[![Linux/Mac Travis Build Status](https://img.shields.io/travis/jonocarroll/ggghost/master.svg?label=Mac%20OSX%20%26%20Linux)](https://travis-ci.org/jonocarroll/ggghost)
[![AppVeyor Build Status](https://img.shields.io/appveyor/ci/jonocarroll/ggghost/master.svg?label=Windows)](https://ci.appveyor.com/project/jonocarroll/ggghost)
[![codecov](https://codecov.io/gh/jonocarroll/ggghost/branch/master/graph/badge.svg)](https://codecov.io/gh/jonocarroll/ggghost)
[![Downloads](http://cranlogs.r-pkg.org/badges/ggghost)](http://www.r-pkg.org/pkg/ggghost)
[![GitHub forks](https://img.shields.io/github/forks/jonocarroll/ggghost.svg)](https://github.com/jonocarroll/ggghost/network)
[![GitHub stars](https://img.shields.io/github/stars/jonocarroll/ggghost.svg)](https://github.com/jonocarroll/ggghost/stargazers)
[![Twitter](https://img.shields.io/twitter/url/https/github.com/jonocarroll/ggghost.svg?style=social)](https://twitter.com/intent/tweet?text=Wow:&url=%5Bobject%20Object%5D)
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
message = FALSE,
warning = FALSE,
comment = "#>",
fig.path = "README_supp/README-"
)
```
# :ghost: _Oh, no! I think I saw a ... g-g-ghost_
![](https://github.com/jonocarroll/ggghost/raw/master/README_supp/scooby.gif)
Capture the spirit of your `ggplot2` calls.
## Motivation
`ggplot2::ggplot()` stores the information needed to build the graph as a `grob`, but that's what the **computer** needs to know about in order to build the graph. As humans, we're more interested in what commands were issued in order to build the graph. For good reproducibility, the calls need to be applied to the relevant data. While this is somewhat available by deconstructing the `grob`, it's not the simplest approach.
Here is one option that solves that problem.
`ggghost` stores the data used in a `ggplot()` call, and collects `ggplot2` commands (usually separated by `+`) as they are applied, in effect lazily collecting the calls. Once the object is requested, the `print` method combines the individual calls back into the total plotting command and executes it. This is where the call would usually be discarded. Instead, a "ghost" of the commands lingers in the object for further investigation, subsetting, adding to, or subtracting from.
## Installation
You can install `ggghost` from CRAN with:
```{r, eval=FALSE}
install.packages("ggghost")
```
or the development version from github with:
```{r, eval=FALSE}
# install.packages("devtools")
devtools::install_github("jonocarroll/ggghost")
```
## Usage
use `%g<%` to initiate storage of the `ggplot2` calls then add to the call with each logical call on a new line (@hrbrmstr style)
```{r}
tmpdata <- data.frame(x = 1:100, y = rnorm(100))
head(tmpdata)
```
```{r, results='hide'}
library(ggplot2)
library(ggghost)
z %g<% ggplot(tmpdata, aes(x, y))
z <- z + geom_point(col = "steelblue")
z <- z + theme_bw()
z <- z + labs(title = "My cool ggplot")
z <- z + labs(x = "x axis", y = "y axis")
z <- z + geom_smooth()
```
This invisibly stores the `ggplot2` calls in a list which can be reviewed either with the list of calls
```{r}
summary(z)
```
or the concatenated call
```{r}
summary(z, combine = TRUE)
```
The plot can be generated using a `print` method
```{r}
z
```
which re-evaluates the list of calls and applies them to the saved data, meaning that the plot remains reproducible even if the data source is changed/destroyed.
The call list can be subset, removing parts of the call
```{r}
subset(z, c(1,2,6))
```
Plot features can be removed by name, a task that would otherwise have involved re-generating the entire plot
```{r}
z2 <- z + geom_line(col = "coral")
z2 - geom_point()
```
Calls are removed based on matching to the regex `\\(.*$` (from the first bracket to the end of the call), so arguments are irrelevant.
The object still generates all the `grob` info, it's just stored as calls rather than a completed image.
```{r, fig.show='hide'}
str(print(z))
#> [... truncated ...]
```
Since the `grob` info is still produced, normal `ggplot2` operators can be applied *after* the `print` statement, such as replacing the data
```{r}
xvals <- seq(0,2*pi,0.1)
tmpdata_new <- data.frame(x = xvals, y = sin(xvals))
print(z - geom_smooth()) %+% tmpdata_new
```
`ggplot2` calls still work as normal if you want to avoid storing the calls.
```{r}
ggplot(tmpdata) + geom_point(aes(x,y), col = "red")
```
Since the object is a list, we can stepwise show the process of building up the plot as a (re-)animation
```{r}
lazarus(z, "mycoolplot.gif")
```
A supplementary data object (e.g. for use in a `geom_*` or `scale_*` call) can be added to the `ggghost` object
```{r}
myColors <- c("alpha" = "red", "beta" = "blue", "gamma" = "green")
supp_data(z) <- myColors
```
These will be recovered along with the primary data.
For full reproducibility, the entire structure can be saved to an object for re-loading at a later point. This may not have made much sense for a `ggplot2` object, but now both the original data and the calls to generate the plot are saved. Should the environment that generated the plot be destroyed, all is not lost.
```{r}
saveRDS(z, file = "README_supp/mycoolplot.rds")
rm(z)
rm(tmpdata)
rm(myColors)
exists("z")
exists("tmpdata")
exists("myColors")
```
Reading the `ggghost` object back to the session, both the relevant data and plot-generating calls can be re-executed.
```{r}
z <- readRDS("README_supp/mycoolplot.rds")
str(z)
recover_data(z, supp = TRUE)
head(tmpdata)
myColors
z
```
We now have a proper reproducible graphic.
## Caveats
* The data _must_ be used as an argument in the `ggplot2` call, not piped in to it. Pipelines such as `z %g<% tmpdata %>% ggplot()` won't work... yet.
* ~~Only one original data set will be stored; the one in the original `ggplot(data = x)` call. If you require supplementary data for some `geom` then you need manage storage/consistency of that.~~ (fixed)
* For removing `labs` calls, an argument _must_ be present. It doesn't need to be the actual one (all will be removed) but it must evaluate in scope. `TRUE` will do fine.