r - 按顺序创建绘图并排列多个页面

时间:2018-05-09 21:50:39

标签: r plot ggplot2

我需要从文件夹中的所有文件顺序创建绘图,将计算值中的某些信息添加到绘图中(例如,平均SOC,TNPP,ANPP,BNPP),并根据需要将它们排列在所需的页面上以将它们排列在5 x 3布局。

请在此处找到包含试用文件的示例文件夹: https://www.dropbox.com/sh/evty00a0t9a062b/AAABG-rIq3Uhtlf-yOWo2fNGa?dl=0

根据不同的在线资源和其他主题,我尝试过:

path <- "C:/Users/.../trialFiles"
dfs <- dir(path, "*.csv", full.names = FALSE, ignore.case = TRUE, all.files = TRUE)

plotModel <- function(df) {
    dat <- read.csv(paste(path, df, sep = "/"), header = TRUE, sep = ",") 
    Time <- dat$time
    SOC <- dat$somtc
    AGBM <- dat$agcprd
    BGBM <- dat$bgcjprd
    time_frame <- Time >= oT & Time <= fT
    sTime <- Time[time_frame]
    sSOC <- SOC[sTime]
    sAGBM <- AGBM[sTime]
    sBGBM <- BGBM[sTime]

    iM_AGBM <- mean(sAGBM)
    iM_BGBM <- mean(sBGBM)
    iMSOC <- mean(sSOC)
    iTNPP <- sum(iM_AGBM, iM_BGBM)

    plot(Time, SOC)
    legend("bottomright", bty = "n", legend = paste(df, "\n\n",
                                                    "SOC =", format(iMSOC, digits = 6), "\n",
                                                    "TNPP =", format(iTNPP, digits = 6), "\n", 
                                                    "ANPP =", format(iM_AGBM, digits = 5), "\n",
                                                    "BNPP =", format(iM_BGBM, digits = 5), sep = ""))

}

eq_plot <- lapply(dfs, plotModel)

nPlot <- length(eq_plot)
cols <- 3
layout <- matrix(seq(1, cols * ceiling(nPlot/cols)),
                ncol = cols, nrow = ceiling(nPlot/cols))

grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

for (i in 1:nPlot) {
  matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

  print(eq_plot[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                  layout.pos.col = matchidx$col))
}

它没有给我一个错误,但也没有显示图表,只显示图例中指定的文字。

我也尝试过使用ggplot,如下所示:

dfs <- dir(period, "*.csv", full.names = FALSE, ignore.case = TRUE, all.files = TRUE)

plotModel <- function(df) {
    dat <- read.csv(paste(period, df, sep = "/"), header = TRUE, sep = ",") 
    Time <- dat$time
    SOC <- dat$somtc
    AGBM <- dat$agcprd
    BGBM <- dat$bgcjprd
    time_frame <- Time >= oT & Time <= fT
    sTime <- Time[time_frame]
    sSOC <- SOC[sTime]
    sAGBM <- AGBM[sTime]
    sBGBM <- BGBM[sTime]

    iM_AGBM <- mean(sAGBM)
    iM_BGBM <- mean(sBGBM)
    iMSOC <- mean(sSOC)
    iTNPP <- sum(iM_AGBM, iM_BGBM)

    ggplot(dat, aes(x=Time, y=SOC)) +
    geom_line() +
    ggtitle(df, subtitle = paste("SOC =", format(iMSOC, digits = 6), "\n",
                                            "TNPP =", format(iTNPP, digits = 6), "\n", 
                                            "ANPP =", format(iM_AGBM, digits = 5), "\n",
                                            "BNPP =", format(iM_BGBM, digits = 5), sep = ""))
}

eq_plot <- lapply(dfs, plotModel)
multi.page <- ggarrange(eq_plot, nrow = 5, ncol = 3)
ggexport(multi.page, filename = "diag_plots")

但是我收到以下错误消息:ggplot2 doesn't know how to deal with data of class uneval

请原谅,如果交叉发布。我尝试过这些例子,但是我做错了。

由于

1 个答案:

答案 0 :(得分:1)

您需要将函数中的绘图保存在对象中,然后将其返回。 这是该目录中前3个文件的解决方案。您需要调整此代码的最后两行来处理所有绘图:

library(ggplot2)
library(ggpubr)

path <- "C:/Users/.../path/to/your/dir"
dfs <- dir(path, "*.csv", full.names = FALSE, ignore.case = TRUE, all.files = TRUE)

# define oT and fT
oT=100
fT=1000

plotModel <- function(df) {
  dat <- read.csv(paste(path, df, sep = "/"), header = TRUE, sep = ",") 

  # This part can be optimized, see below the simplified version of the function
  Time <- dat$time
  SOC <- dat$somtc
  AGBM <- dat$agcprd
  BGBM <- dat$bgcjprd
  time_frame <- Time >= oT & Time <= fT
  sTime <- Time[time_frame]
  sSOC <- SOC[sTime]
  sAGBM <- AGBM[sTime]
  sBGBM <- BGBM[sTime]

  iM_AGBM <- mean(sAGBM)
  iM_BGBM <- mean(sBGBM)
  iMSOC <- mean(sSOC)
  iTNPP <- sum(iM_AGBM, iM_BGBM)

  # save graph in an object
  g<- ggplot(dat, aes(x=Time, y=SOC)) +
    geom_line() +
    ggtitle(df, 
            subtitle = paste("SOC =", format(iMSOC, digits=6), "\n",
                             "TNPP =", format(iTNPP, digits=6), "\n", 
                             "ANPP =", format(iM_AGBM, digits=5), "\n",
                             "BNPP =", format(iM_BGBM, digits=5), sep = ""))
  return(g)
}

eq_plot <- lapply(dfs, plotModel)

ggarrange(eq_plot[[1]], eq_plot[[2]], eq_plot[[3]], nrow = 1, ncol = 3) %>%
ggexport(filename = "diag_plots.png")
dev.off()

enter image description here

为了清晰和高效,可以简化功能主体:

plotModel <- function(df) {

  dat <- read.csv(paste(path, df, sep = "/"), header = TRUE, sep = ",") 
  var.means <- colMeans(dat[dat$time >= oT & dat$time <= fT, c("agcprd","bgcjprd","somtc")])

  # save graph in an object
  g<- ggplot(dat, aes(x=time, y=somtc)) +
    geom_line() +
    ggtitle(df, 
            subtitle = paste("SOC =", format(var.means["somtc"], digits=6), "\n",
                             "TNPP =", format(var.means["agcprd"] + var.means["bgcjprd"], digits=6), "\n", 
                             "ANPP =", format(var.means["agcprd"], digits=5), "\n",
                             "BNPP =", format(var.means["bgcjprd"], digits=5), sep = ""))
  return(g)
}
相关问题