将ggplot分配给for循环中的变量

我正在使用它在for循环中将图形打印到PDF,这很好用:

print(ggplot(subdata3, aes(x = Year, y = value, colour = Stat)) 
+ geom_line() + expand_limits(y=c(0,100)) 
+ ggtitle(paste0(as.character(ScenName),":\n", as.character(k))) 
+ ylab(paste0(j, " (", units, ")")))

现在,我需要将每个ggplot分配给一个变量’p’,然后将其存储在一个列表中,并由multiplot用于在页面上排列多个图.

这是我尝试将绘图分配给变量:

p <- ggplot(subdata3, aes(x = Year, y = value, colour = Stat)) 
+ geom_line() + expand_limits(y=c(0,100)) 
+ ggtitle(paste0(as.character(ScenName),":\n", as.character(k))) 
+ ylab(paste0(j, " (", units, ")"))

我做的唯一更改是删除print()并进行变量赋值.之后,我收到此错误:

Error in +ggtitle(paste0(as.character(ScenName), ":\n", as.character(k))) : 
  invalid argument to unary operator

我已尝试对各种()组进行多次调整以尝试找到解决方案,但是,似乎没有任何效果.

有什么想法吗?

最佳答案 你有错误的地方.试试这个:

p <- ggplot(subdata3, aes(x = Year, y = value, colour = Stat)) +
  geom_line() + expand_limits(y=c(0,100)) +
  ggtitle(paste0(as.character(ScenName),":\n", as.character(k))) +
  ylab(paste0(j, " (", units, ")"))
点赞