【发布时间】: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将为您提供用于x和y轴的变量。如果您想通过一个呼叫显示多个信息,您可以使用带有print()的命名列表。例如:print(list(aes = p$mapping, data = head(p$data))) -
@Paul 我很高兴知道我可以使用
$获得有关 ggplot 变量的具体信息,谢谢!不过,这并不是我真正想要的。 (为了澄清我自己的问题),如果我传递参数p,我希望有一个函数将ggplot(mtcars, aes(mpg, hp)) + geom_line()作为字符串输出。