r语言 - 使用 lapply 生成多个图形,遍历列表以标题图形不起作用



我创建了以下代码以成功从列表"hemi_split"中生成我想要的图形,但是当我尝试为图表命名时会生成以下错误

'"hemi_split[i] 中的错误:下标类型'列表'无效">

这是代码

graphs <- lapply(hemi_split, function(i){ 
ggplot(data=i, aes(x=type, y=shoot.mass))+
geom_point()+
facet_wrap(.~host)+ 
theme_minimal()+
labs(title=names(hemi_split[i]))
})

以下是我想为每个图表命名的列表元素的名称

names(hemi_split)
[1] "CADE" "CAFO" "CAHI" "CALE" "CAMI" "CARU" "CAWI" "COPI" "REEL" "TRER" "TRVE"

感谢您的任何帮助!

使用purrrimap,您可以访问数据和名称。

graphs <- purrr::imap(hemi_split, ~{ 
ggplot(data=.x, aes(x=type, y=shoot.mass))+
geom_point()+
facet_wrap(.~host)+ 
theme_minimal()+
labs(title=.y)
})

如果你确实在列表中申请,你会忘记名字,你可以试试:

graphs <- lapply(names(hemi_split), function(i){ 
ggplot(data=hemi_split[[i]], aes(x=type, y=shoot.mass))+
geom_point()+
facet_wrap(.~host)+ 
theme_minimal()+
labs(title=i))
})

最新更新