How to see the code of a stored plot (ggplot)
Asked Answered
C

2

5

I want to know whether it is possible to see the code of a plot that is stored in a variable. For example, given the following plot:

library(ggplot2)

myData <- data.frame(x=1:100, y = 100:1)
myPlot <- ggplot(myData, aes(x,y)) + geom_line()

I'd like to have a function "seeCode" that returns the actual code used to construct the plot:

>seeCode(myPlot)
  ggplot(myData, aes(x,y)) + geom_line()
Caiman answered 28/5, 2015 at 22:51 Comment(0)
M
6

That information isn't retained. There's not a one-to-one mapping from ggplot object to the code that produced it, just as there's no way to know if a "5" came from "3+2" or "4+1". The myPlot contains information about how the object might have been created, but there is no inverse function that exists. And you can arbitrarily modify the ggplot object however you like (it's just a list) so it might not translate nicely to simple ggplot function calls.

Maximamaximal answered 29/5, 2015 at 0:50 Comment(3)
I thought ggplot_build(myPlot) returns how the plot was created.Educt
It translates the ggplot object into something that can be plotted. There certainly is information about how the object might have been created, but there is no inverse function that exists. And you can arbitrarily modify the ggplot object however you like (it's just a list) so it might not translate nicely to simple ggplot function calls.Maximamaximal
I did not know about ggplot_build(). Although it does not return the statement that constructed the plot it makes it easy to retrieve some useful info. Thanks @EductCaiman
T
1

The closest thing I've encountered to this problem is in {constructive}. It tries to regenerate code that can be used to recreate the plot, though it is not always literal. It sort-of works like dput() but with more complex objects and nicer code formatting.

library(ggplot2)
library(constructive)

myData <- data.frame(x=1:100, y = 100:1)
myPlot <- ggplot(myData, aes(x,y)) + geom_line()

construct(myPlot)
#> {constructive} couldn't create code that reproduces perfectly the input
#> ℹ Call `construct_issues()` to inspect the last issues
#> data.frame(x = 1:100, y = 100:1) |>
#>   ggplot2::ggplot(ggplot2::aes(x, y)) +
#>   ggplot2::geom_line()

Created on 2024-06-13 with reprex v2.1.0

Tem answered 13/6 at 16:10 Comment(3)
Note that constructive tells you when the reconstruction is not perfect, as it does here. In that case however the only difference between the original object and the reconstructed one is in NSE artifact due to the fact constructive prefixes functions with ggplot2:: (call construct_issues() to see those differences). For practical purposes the reconstructed object is equivalent.Hillary
Thanks for the clarification on this! I'm not sure that the call saved for the use in error messages is closely related to non standard evaluation, but I'm glad {constructive} has this great fidelity!Tem
Well to me capturing a call is NSE, NSE is a terribly unspecific term though, I should have said call capture. I was wondering why these were stored so it's interesting for me to learn it's for errors!Hillary

© 2022 - 2024 — McMap. All rights reserved.