4.1.2 R 版本导致 ggplot 出现一些问题

The 4.1.2 R version leads to some issues with ggplot

我有一个 df Measurements_l 的列表,我曾经使用 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()

我在评论中建议的以下内容对我有用:

library(ggplot2)

Measurements <- data.frame(
  Locality = rep(c("PARIS", "BRUSSELS"), each = 4),
  Sample   = rep(rep(c("MILK", "WATER"), each = 2), 2),
  Nuclides = "I-131",
  Activity = 1:8,
  Measuring_Unit = "BQ/L",
  Date     = rep(c(2010, 2020), 4)
)

Measurements_l <- split(Measurements,list(Measurements$Sample,Measurements$Locality), drop=TRUE)

lapply(names(Measurements_l), function(i){
  g <- 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 = ""), plot = g, 
         dpi = 600, width = 30, height = 22, units = "cm")
})

它在工作目录中产生了 4 个名称为 'WATER.BRUSSELS.png'、'WATER.PARIS.png'、'MILK.BRUSSELS.png' 和 'MILK.PARIS.png' 的地块。