Measurements_l
我有一个我曾经使用过的 df 列表lapply
和一个function
包含ggplot
来绘制每个列表的列表:
Measurements_l <- split(Measurements,list(Measurements$Sample.type,Measurements$Site), drop=TRUE)
lapply(names(Measurements_l), function(i){
ggplot(Measurements_l[[i]], aes(Date, Activity, group = Nuclides, col = as.factor(Nuclides))) +
geom_line() +
geom_point() +
facet_grid(rows = vars(Locality)) +
xlab("Date") +
ylab(paste("Concentration in", Measurements_l[[ i ]]$Measuring.Unit[ 1 ])) +
theme(legend.title=element_blank(),
guides(col=guide_legend(ncol=1)) +
ggsave(paste(i, ".png", sep = ""), dpi = 600, width = 30, height = 22, units = "cm")
})
dev.off()
使用最新的 R 版本(4.1.2),我遇到了一些问题:
Error in `ggplot_add()`:
! Can't add `ggsave(paste(i, ".png", sep = ""), dpi = 600, width = 30, height = 20, ` to a ggplot object.
* Can't add ` units = "cm")` to a ggplot object.
Run `rlang::last_error()` to see where the error occurred.
Called from: signal_abort(cnd, .file)
Browse[1]> dev.off()
Error during wrapup: cannot shut down device 1 (the null device)
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
ggsave
被包含在我的ggplot
功能中,我不知道如何解决这个问题。任何的想法 ?由于这个较新版本,我不想重写所有脚本中的所有函数。
此外,为什么不能再对 ggplot 对象使用“cm”单位?
可重复的示例(导致 4 个图表)
Measurements
Locality Sample Nuclides Activity Measuring Unit Date
PARIS MILK I-131 1 BQ/L 2010
PARIS MILK I-131 2 BQ/L 2020
PARIS WATER I-131 3 BQ/L 2010
PARIS WATER I-131 4 BQ/L 2020
BRUSSELS MILK I-131 5 BQ/L 2010
BRUSSELS MILK I-131 6 BQ/L 2020
BRUSSELS WATER I-131 7 BQ/L 2010
BRUSSELS WATER I-131 8 BQ/L 2020
Measurements_l <- split(Measurements,list(Measurements$Sample,Measurements$Locality), drop=TRUE)
lapply(names(Measurements_l), function(i){
ggplot(Measurements_l[[i]], aes(Date, Activity, group = Nuclides, col = as.factor(Nuclides))) +
geom_line() +
geom_point() +
facet_grid(rows = vars(Locality)) +
ggsave(paste(i, ".png", sep = ""), dpi = 600, width = 30, height = 22, units = "cm")
})
dev.off()