我一直在尝试创建返回ggplot列表的函数,并且遇到各种问题。但是从根本上来说,我不明白为什么这是TRUE
"data.frame" == class(c(qplot(1:10,rnorm(10)))[[1]])
这是
[TRUE,TRUE]
时c('gg','ggplot') == class(qplot(1:10,rnorm(10)))
我还没有看到任何与此类似的问题。我看到各种问题可以通过以下方式解决
lapply(someList, function(x) {
#make ggplot, then use print(...) or whatever
})
因此,我猜想有一些关于将ggplot对象传递出函数或环境之间或某些事物之间的事情。感谢您提供有关ggplot或R的任何线索。
最佳答案
library(ggplot2)
p = c(qplot(1:10,rnorm(10)))
p2 = qplot(1:10,rnorm(10))
p[[1]]
(与p[['data']])
相同)应该是dataframe
。它通常保存该图的数据。p
是list
,因为您使用了c
函数。p2
是ggplot
,因为这是qplot
返回的内容。看一下每个对象的属性。
attributes(p)
# $names
# [1] "data" "layers" "scales" "mapping" "theme" "coordinates"
# [7] "facet" "plot_env" "labels"
attributes(p2)
# $names
# [1] "data" "layers" "scales" "mapping" "theme" "coordinates"
# [7] "facet" "plot_env" "labels"
#
# $class
# [1] "gg" "ggplot"
要存储许多
ggplot
对象,请使用list
。ggplot.objects = list(p2,p2,p2)
c
帮助文件显示
ggplot
不是可能的输出。它还指出
c
is sometimes used for its side effect of removing attributes except names, for example to turn an array into a vector
如果你想
c
返回ggplot
对象,那么您可以尝试定义自己的c.ggplot
功能。您必须阅读大量有关S3和S4函数的信息,才能了解其中的内容。继续。
关于r - 如何创建ggplot列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22817386/