-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathREADME.Rmd
125 lines (85 loc) · 2.69 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
suppressPackageStartupMessages({
library(ggplot2)
})
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
fig.height = 2
)
```
# ggreverse
<!-- badges: start -->


<!-- badges: end -->
`ggreverse` takes a ggplot object and returns the code to create that plot.
This package is written as a learning exercise to help me figure out
the internal structure of a ggplot object.
## Releases
* `0.1.0` - initial release
* `0.1.1` - improved theme handling
## Installation
You can install from [GitHub](https://github.com/coolbutuseless/) with:
``` r
# install.packages("remotes")
remotes::install_github("coolbutuseless/ggreverse")
```
## Example `ggreverse::convert_to_code()`
1. Create a ggplot
2. Convert the ggplot back into code using `ggreverse`
3. Convert the code back into a plot
```{r}
library(ggreverse)
plot_df <- mtcars
# Create a ggplot2 plot object
p <- ggplot(plot_df) +
geom_point(aes(mpg, wt, colour = cyl), size = 3) +
labs(title = "hello") +
theme_bw() +
theme(legend.position = 'none') +
coord_equal()
```
```{r echo = FALSE}
p
```
```{r echo=TRUE, eval=FALSE}
# Convert the plot object back into code
plot_code <- ggreverse::convert_to_code(p)
print(plot_code)
```
```{r echo=FALSE, eval=TRUE}
plot_code <- convert_to_code(p)
styler::style_text(
gsub("[+]", "+\n", plot_code)
)
```
```{r}
# Parse the plot code back into a plot - which should match the original plot
eval(parse(text = plot_code))
```
## Technical Notes
* the `data` arguments to `ggplot()` and `geom()` are evaluated at call time. There is
no easy way to recover the name of the data argument.
* `ggreverse` tries to match the actual data in the ggplot object against a named
object in the plotting environment. Otherwise it uses a generic data name
* aesthethic mappings are evaluated at call time, so tidyeval and `aes_string()` mappings
are supported, but `ggreverse` will only include the final variable name mapping.
* Layers are currently extracted as `geom_x(stat = 'y')` rather than `stat_y(geom='x')`.
I'm not sure if there are any cases where these aren't equivalent.
## ToDo
* Extracting `facet` and `scales` information.
* Complete themes which are customisations of built-in themes could be
more compact if nested diffs where done between themes, rather than
just a `shallow_diff()`
* Lots of other stuff :)
## SessionInfo
Developed against:
* R 3.5.3
* ggplot2 v3.1.1