How can I see the code of a ggplot variable in R?

Viewed 328

If I run this code the output is obviously a plot.

> p <- ggplot(mtcars, aes(mpg, hp)) +
+ geom_line()
> p

Storing variables for plots can be useful. If this was a more complex plot I might want to see what I declared for p later down the line, for example to debug the theme of a plot. Using View(p) or str(p) doesn't give a good overview at all.

Is there any way that I can see what I declared for ggplot variables like this? Or do I have to actually Ctrl-F my R file in order to manually look up what I did?

Edit: This should make my question more clear:

f <- function (x) {
  result <- x+2
  return(result)
}

library(ggplot2)
p <- ggplot(mtcars, aes(mpg, hp)) +
  geom_line()

#printing f shows me the original form of the variable the way I defined it
print(f)
#printing p does not show me the original form of the variable the way I defined it, but rather executes it
print(p)

#what function do I need to use on p in order to produce the same result as for print(f)?
4 Answers

There isn't a way to get the function call from a ggplot2 object like you describe. However, you can easily go to the definition of your plot in RStudio, by using the Ctrl+. shortcut.

You could store the function call in a character vector and then use eval(parse(text=..)) when you want to actually run the function. For example:

#store the function call in a string
function_call<-"p <- ggplot(mtcars, aes(mpg, hp)) + geom_line()"
print(function_call)
#in order to get the plot, eval the string
eval(parse(text=function_call))
print(p)

I believe what you are looking for is ggplot_build. For example:

library(ggplot2)
p = ggplot(mtcars, aes(x = mpg, y = hp)) +
        geom_point()
l = ggplot_build(p)
df = l[[1]][[1]]
head(df)
     x   y PANEL group shape colour size fill alpha stroke
1 21.0 110     1    -1    19  black  1.5   NA    NA    0.5
2 21.0 110     1    -1    19  black  1.5   NA    NA    0.5
3 22.8  93     1    -1    19  black  1.5   NA    NA    0.5
4 21.4 110     1    -1    19  black  1.5   NA    NA    0.5
5 18.7 175     1    -1    19  black  1.5   NA    NA    0.5
6 18.1 105     1    -1    19  black  1.5   NA    NA    0.5

The list l contains pretty much every info about your plot:

print(l)
$data
$data[[1]]
      x   y PANEL group shape colour size fill alpha stroke
1  21.0 110     1    -1    19  black  1.5   NA    NA    0.5
2  21.0 110     1    -1    19  black  1.5   NA    NA    0.5
3  22.8  93     1    -1    19  black  1.5   NA    NA    0.5
4  21.4 110     1    -1    19  black  1.5   NA    NA    0.5
5  18.7 175     1    -1    19  black  1.5   NA    NA    0.5
6  18.1 105     1    -1    19  black  1.5   NA    NA    0.5
7  14.3 245     1    -1    19  black  1.5   NA    NA    0.5
8  24.4  62     1    -1    19  black  1.5   NA    NA    0.5
9  22.8  95     1    -1    19  black  1.5   NA    NA    0.5
10 19.2 123     1    -1    19  black  1.5   NA    NA    0.5
11 17.8 123     1    -1    19  black  1.5   NA    NA    0.5
12 16.4 180     1    -1    19  black  1.5   NA    NA    0.5
13 17.3 180     1    -1    19  black  1.5   NA    NA    0.5
14 15.2 180     1    -1    19  black  1.5   NA    NA    0.5
15 10.4 205     1    -1    19  black  1.5   NA    NA    0.5
16 10.4 215     1    -1    19  black  1.5   NA    NA    0.5
17 14.7 230     1    -1    19  black  1.5   NA    NA    0.5
18 32.4  66     1    -1    19  black  1.5   NA    NA    0.5
19 30.4  52     1    -1    19  black  1.5   NA    NA    0.5
20 33.9  65     1    -1    19  black  1.5   NA    NA    0.5
21 21.5  97     1    -1    19  black  1.5   NA    NA    0.5
22 15.5 150     1    -1    19  black  1.5   NA    NA    0.5
23 15.2 150     1    -1    19  black  1.5   NA    NA    0.5
24 13.3 245     1    -1    19  black  1.5   NA    NA    0.5
25 19.2 175     1    -1    19  black  1.5   NA    NA    0.5
26 27.3  66     1    -1    19  black  1.5   NA    NA    0.5
27 26.0  91     1    -1    19  black  1.5   NA    NA    0.5
28 30.4 113     1    -1    19  black  1.5   NA    NA    0.5
29 15.8 264     1    -1    19  black  1.5   NA    NA    0.5
30 19.7 175     1    -1    19  black  1.5   NA    NA    0.5
31 15.0 335     1    -1    19  black  1.5   NA    NA    0.5
32 21.4 109     1    -1    19  black  1.5   NA    NA    0.5


$layout
<ggproto object: Class Layout, gg>
    coord: <ggproto object: Class CoordCartesian, Coord, gg>
        aspect: function
        backtransform_range: function
        clip: on
        default: TRUE
        distance: function
        expand: TRUE
        is_free: function
        is_linear: function
        labels: function
        limits: list
        modify_scales: function
        range: function
        render_axis_h: function
        render_axis_v: function
        render_bg: function
        render_fg: function
        setup_data: function
        setup_layout: function
        setup_panel_guides: function
        setup_panel_params: function
        setup_params: function
        train_panel_guides: function
        transform: function
        super:  <ggproto object: Class CoordCartesian, Coord, gg>
    coord_params: list
    facet: <ggproto object: Class FacetNull, Facet, gg>
        compute_layout: function
        draw_back: function
        draw_front: function
        draw_labels: function
        draw_panels: function
        finish_data: function
        init_scales: function
        map_data: function
        params: list
        setup_data: function
        setup_params: function
        shrink: TRUE
        train_scales: function
        vars: function
        super:  <ggproto object: Class FacetNull, Facet, gg>
    facet_params: list
    finish_data: function
    get_scales: function
    layout: data.frame
    map_position: function
    panel_params: list
    panel_scales_x: list
    panel_scales_y: list
    render: function
    render_labels: function
    reset_scales: function
    setup: function
    setup_panel_guides: function
    setup_panel_params: function
    train_position: function
    xlabel: function
    ylabel: function
    super:  <ggproto object: Class Layout, gg>

$plot

attr(,"class")
[1] "ggplot_built"

I suppose you already thought about it but just to be sure I post it as an answer. If you do not mind using a function to make your plot, it is very straighforward to get the code i.e.

f_plot <- function(df){
  require(ggplot2)
  p <- ggplot(mtcars, aes(mpg, hp)) +
    geom_line()
  return(p)
}

print(f_plot)

Output:

> print(f_plot)
function(df){
  require(ggplot2)
  p <- ggplot(mtcars, aes(mpg, hp)) +
    geom_line()
  return(p)
}

You can still use it to effectively plot and add things such as title:

f_plot(mtcars) +
  theme_classic() # works!

And, this way you can also use View(f_plot) to open the code in a new window (RStudio)

Related