suppressPackageStartupMessages({ library(ggplot2) }) knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%", fig.height = 2 )
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.
0.1.0
- initial release0.1.1
- improved theme handlingYou can install from GitHub with:
# install.packages("remotes") remotes::install_github("coolbutuseless/ggreverse")
ggreverse::convert_to_code()
ggreverse
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()
p
# Convert the plot object back into code plot_code <- ggreverse::convert_to_code(p) print(plot_code)
plot_code <- convert_to_code(p) styler::style_text( gsub("[+]", "+\n", plot_code) )
# Parse the plot code back into a plot - which should match the original plot eval(parse(text = plot_code))
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 nameaes_string()
mappings
are supported, but ggreverse
will only include the final variable name mapping.geom_x(stat = 'y')
rather than stat_y(geom='x')
.facet
and scales
information.shallow_diff()
Developed against:
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.