【问题标题】:How can I see the code of a ggplot variable in R? [duplicate]如何在 R 中查看 ggplot 变量的代码? [复制]
【发布时间】:2021-05-12 10:44:08
【问题描述】:

如果我运行这段代码,输出显然是一个情节。

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

为绘图存储变量可能很有用。如果这是一个更复杂的情节,我可能想看看我稍后为p 声明的内容,例如调试情节的主题。使用 View(p)str(p) 根本不能提供一个很好的概述。

有什么方法可以让我看到我为这样的 ggplot 变量声明的内容?还是我必须实际 Ctrl-F 我的 R 文件才能手动查找我所做的事情?

编辑:这应该让我的问题更清楚:

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)?

【问题讨论】:

  • 嗨 :) 我不确定是否完全理解。如果您想在控制台中查看p 对象的某些部分,您可以使用p$name_of_the_thing_you_want_to_see。例如,p$mapping 将为您提供用于 xy 轴的变量。如果您想通过一个呼叫显示多个信息,您可以使用带有print() 的命名列表。例如:print(list(aes = p$mapping, data = head(p$data)))
  • @Paul 我很高兴知道我可以使用$ 获得有关 ggplot 变量的具体信息,谢谢!不过,这并不是我真正想要的。 (为了澄清我自己的问题),如果我传递参数p,我希望有一个函数将ggplot(mtcars, aes(mpg, hp)) + geom_line() 作为字符串输出。

标签: r ggplot2


【解决方案1】:

没有办法像您描述的那样从 ggplot2 对象获取函数调用。 但是,您可以使用 Ctrl+. 快捷方式轻松转到 RStudio 中的绘图定义。

【讨论】:

    【解决方案2】:

    您可以将函数调用存储在字符向量中,然后在您想要实际运行该函数时使用eval(parse(text=..))。例如:

    #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)
    

    【讨论】:

      【解决方案3】:

      我相信您正在寻找的是ggplot_build。例如:

      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
      

      列表l 包含几乎所有关于你的情节的信息:

      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"
      

      【讨论】:

      • 这是正确的方向,谢谢!也许我只需要学习如何解释ggplot_build(x) 的输出,但它仍然不能真正满足我的要求。我想要的是一个以p 作为参数并给我gplot(mtcars, aes(mpg, hp)) + geom_line() 作为输出的函数。该输出可能必须是一个字符串。
      【解决方案4】:

      我想您已经考虑过了,但只是为了确保我将其发布为答案。 如果您不介意使用函数来制作情节,那么获取代码非常简单,即

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

      输出:

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

      您仍然可以使用它来有效地绘制和添加诸如标题之类的内容:

      f_plot(mtcars) +
        theme_classic() # works!
      

      而且,这样你也可以使用View(f_plot)在新窗口中打开代码(RStudio)

      【讨论】:

        猜你喜欢
        • 2011-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-24
        • 1970-01-01
        • 2019-03-23
        • 1970-01-01
        相关资源
        最近更新 更多