首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在marrangeGrob之后使用ggsave

无法在marrangeGrob之后使用ggsave
EN

Stack Overflow用户
提问于 2017-08-30 17:03:57
回答 1查看 916关注 0票数 0

我正在使用下面的设置来创建一个ggplot2图表列表。

这样做的效果很好:

代码语言:javascript
复制
library(grid)
library(gridExtra)
library(ggplot2)

mycols <- c('year','displ')

mylist <- list()
for(item in mycols){

  p <- ggplot(mpg, aes_string(x = 'hwy', y = item)) +  
    geom_point()
  mylist[[(length(mylist) +1)]] <- p
}

ml = marrangeGrob(grob = mylist, nrow=2, ncol=1)
ggsave("P://multipage.pdf", ml, width =10, height = 5)

但是,在循环中,替换:

$<-.data.frame中的错误(*tmp*,"wrapvp",value = list(x = 0.5,y=0.5):替换有17行,数据有234个

这里有什么问题?单独而言,列表中的所有图表看起来都很好。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-30 17:46:53

这实际上与marrangeGrob无关,也与您如何构建列表有关。比较这些方法的输出结构

代码语言:javascript
复制
out1 <- list()
out1[[length(out1)+1]]<-list(a=1, b=2)
out1[[length(out1)+1]]<-list(a=2, b=2)
str(out1)
# List of 2
#  $ :List of 2
#   ..$ a: num 1
#   ..$ b: num 2
#  $ :List of 2
#   ..$ a: num 2
#   ..$ b: num 2

out2 <- list()
out2 <- append(out2, list(a=1, b=2))
out2 <- append(out2, list(a=2, b=2))
str(out2)
# List of 4
#  $ a: num 1
#  $ b: num 2
#  $ a: num 2
#  $ b: num 2

请注意,它们产生不同的结构。append()将元素添加到“根”列表中,而不是嵌套列表中的列表。您可以使用和额外的list()显式地自己做这件事。

代码语言:javascript
复制
out3 <- list()
out3 <- append(out3, list(list(a=1, b=2)))
out3 <- append(out3, list(list(a=2, b=2)))
str(out3)
# List of 2
#  $ :List of 2
#   ..$ a: num 1
#   ..$ b: num 2
#  $ :List of 2
#   ..$ a: num 2
#   ..$ b: num 2

但是,像这样在R中搅乱循环很少是必要的。最好使用内置迭代器,如lapply()Map()。例如

代码语言:javascript
复制
mylist <- lapply(mycols, function(item) {
  ggplot(mpg, aes_string(x = 'hwy', y = item)) + 
    geom_point()
})
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45966069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档