1

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()
4

1 回答 1

2

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

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”的图。

于 2022-02-09T13:45:36.033 回答