{"id":13711904,"url":"https://github.com/coolbutuseless/ggreverse","last_synced_at":"2025-06-13T07:07:00.387Z","repository":{"id":95359599,"uuid":"183582634","full_name":"coolbutuseless/ggreverse","owner":"coolbutuseless","description":"Reverse a ggplot object back into code","archived":false,"fork":false,"pushed_at":"2019-05-31T21:27:31.000Z","size":57,"stargazers_count":67,"open_issues_count":3,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-18T01:11:15.719Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coolbutuseless.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-04-26T07:45:35.000Z","updated_at":"2024-11-20T16:48:36.000Z","dependencies_parsed_at":"2023-07-04T22:32:15.680Z","dependency_job_id":null,"html_url":"https://github.com/coolbutuseless/ggreverse","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Fggreverse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Fggreverse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Fggreverse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolbutuseless%2Fggreverse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coolbutuseless","download_url":"https://codeload.github.com/coolbutuseless/ggreverse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244799239,"owners_count":20512213,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T23:01:12.832Z","updated_at":"2025-03-21T12:30:58.540Z","avatar_url":"https://github.com/coolbutuseless.png","language":"R","funding_links":[],"categories":["Plot layers","ggplot"],"sub_categories":["Miscellaneous"],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nsuppressPackageStartupMessages({\n  library(ggplot2)\n})\n\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\",\n  fig.height = 2\n)\n```\n\n# ggreverse\n\n\u003c!-- badges: start --\u003e\n![](https://img.shields.io/badge/Status-alpha-orange.svg)\n![](https://img.shields.io/badge/Version-0.1.1-blue.svg)\n\u003c!-- badges: end --\u003e\n\n`ggreverse` takes a ggplot object and returns the code to create that plot.\n\nThis package is written as a learning exercise to help me figure out \nthe internal structure of a ggplot object.\n\n\n\n## Releases\n\n* `0.1.0` - initial release\n* `0.1.1` - improved theme handling\n\n## Installation\n\nYou can install from [GitHub](https://github.com/coolbutuseless/) with:\n\n``` r\n# install.packages(\"remotes\")\nremotes::install_github(\"coolbutuseless/ggreverse\")\n```\n\n\n\n## Example `ggreverse::convert_to_code()`\n\n1. Create a ggplot\n2. Convert the ggplot back into code using `ggreverse`\n3. Convert the code back into a plot\n\n```{r}\nlibrary(ggreverse)\n\nplot_df \u003c- mtcars\n\n# Create a ggplot2 plot object\np \u003c- ggplot(plot_df) +\n  geom_point(aes(mpg, wt, colour = cyl), size = 3) +\n  labs(title = \"hello\") +\n  theme_bw() + \n  theme(legend.position = 'none') + \n  coord_equal()\n```\n\n```{r echo = FALSE}\np\n```\n\n\n```{r echo=TRUE, eval=FALSE}\n# Convert the plot object back into code\nplot_code \u003c- ggreverse::convert_to_code(p)\nprint(plot_code)\n```\n\n\n```{r echo=FALSE, eval=TRUE}\nplot_code \u003c- convert_to_code(p)\nstyler::style_text(\n  gsub(\"[+]\", \"+\\n\", plot_code)\n)\n```\n\n\n```{r}\n# Parse the plot code back into a plot - which should match the original plot\neval(parse(text = plot_code))\n```\n\n\n\n\n## Technical Notes\n\n* the `data` arguments to `ggplot()` and `geom()` are evaluated at call time. There is\n  no easy way to recover the name of the data argument.\n    * `ggreverse` tries to match the actual data in the ggplot object against a named\n      object in the plotting environment. Otherwise it uses a generic data name\n* aesthethic mappings are evaluated at call time, so tidyeval and `aes_string()` mappings\n  are supported, but `ggreverse` will only include the final variable name mapping.\n* Layers are currently extracted as `geom_x(stat = 'y')` rather than `stat_y(geom='x')`.  \n  I'm not sure if there are any cases where these aren't equivalent.\n\n## ToDo\n\n* Extracting `facet` and `scales` information.\n* Complete themes which are customisations of built-in themes could be \n  more compact if nested diffs where done between themes, rather than \n  just a `shallow_diff()`\n* Lots of other stuff :)\n\n\n## SessionInfo\n\nDeveloped against:\n\n* R 3.5.3\n* ggplot2 v3.1.1\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoolbutuseless%2Fggreverse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoolbutuseless%2Fggreverse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoolbutuseless%2Fggreverse/lists"}