如果我运行这段代码,输出显然是一个图。
> 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)?
您可以将函数调用存储在字符向量中,然后在想要实际运行函数时使用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)
我相信您正在寻找的是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"
没有一种方法可以像您描述的那样从ggplot2对象获得函数调用。但是,通过使用Ctrl+.
快捷方式,您可以很容易地在RStudio中转到绘图的定义。
我想你已经考虑过了,但只是为了确保我把它作为答案发布。如果你不介意使用一个函数来绘制你的图,那么获得代码是非常简单的,即
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(中打开代码