简体   繁体   中英

How do I export multiple plots as png files from R?

I have an R script that creates multiple trend plots from tabular data. I need to export each plot as a png file. I have searched and tried (to no avail) using png(). It seems like this should be a relatively easy fix. My code is below. Could someone please offer some suggestions as to how I might solve this dilemma?

library(dplyr)
library(tidyr)
library(readr)
library(ggplot2)
library(magrittr)
library(stringi)
library(lubridate)
library(stats)

#load in datafiles

c_data <- read_csv  ("C:/Projects/A_AX_tech_memo/data_analysis/AAX_data_2017_dtcts.csv")

C_data_out <-
c_data %>%
group_by(METHOD_NAME, STD_CON_LONG_NAME, SAMP_SITE_NAME, FILTERED_FLAG) %>%
ungroup() %>%
select(METHOD_NAME, STD_CON_LONG_NAME, SAMP_SITE_NAME, SAMP_DATE,   STD_VALUE_RPTD, STD_ANAL_UNITS_RPTD, FILTERED_FLAG, STD_REPORTING_LIMIT, STD_REQUIRED_DETECTION_LIMIT, LAB_QUALIFIER) %>%
rename(Method = METHOD_NAME, Constit = STD_CON_LONG_NAME, Well = SAMP_SITE_NAME, Date = SAMP_DATE, Value = STD_VALUE_RPTD, Unit = STD_ANAL_UNITS_RPTD, Filtered = FILTERED_FLAG, MDL = STD_REPORTING_LIMIT, RDL = STD_REQUIRED_DETECTION_LIMIT, Flag =LAB_QUALIFIER) %>%
mutate(Date = mdy(Date))

dfs <- split(C_data_out, with(C_data_out, interaction(Well, Constit, Filtered)), drop = TRUE)

plotz <- lapply(dfs, function(x){
  ggplot(data = x, aes(Date, Value)) +
    geom_point(data = x, aes(color = Flag), size = 3) +
    ggtitle(paste(x$Well, x$Constit, x$Filtered, sep =".")) +
    ylab("ug/L or Pci/L") +
    geom_smooth(method = "lm", se = FALSE, rm.na = TRUE) +
    geom_hline(aes(yintercept=x$MDL, linetype="MDL"), color ="dark green", lwd=1, lty=2) +
    geom_hline(aes(yintercept=x$RDL, linetype="RDL"), color ="purple", lwd=1, lty=2)
})

Here is an example of what I have tried with png:

names <- lapply(dfs, function(x){
  ggtitle(paste(x$Well, x$Constit, x$Filtered, sep ="."))
})

plotz <- lapply(dfs, function(x){
  mypath <- file.path("C:","plots", paste(names[i], ".png", sep = ""))
  png(file=mypath)
  ggplot(data = x, aes(Date, Value)) +
  geom_point(data = x, aes(color = Flag), size = 3) + 
  ggtitle(paste(x$Well, x$Constit, x$Filtered, sep =".")) +
  ylab("ug/L or Pci/L") +
  geom_smooth(method = "lm", se = FALSE, rm.na = TRUE) +
  geom_hline(aes(yintercept=x$MDL, linetype="MDL"), color ="dark green", lwd=1, lty=2) +
  geom_hline(aes(yintercept=x$RDL, linetype="RDL"), color ="purple", lwd=1, lty=2)
  dev.off()
})

This gives me this error:

 Error in paste(names[i], ".png", sep = "") : object 'i' not found 

Any and all help would be much appreciated.

This snippet works using the map and walk functions from purrr.

mtcars_split <-
  mtcars %>% 
  split(.$cyl) 

paths <- 
  paste0(names(mtcars_split),".png") 

plots <- 
  mtcars_split %>%
  map(~ ggplot(data=.,mapping = aes(y=mpg,x=wt)) + geom_point()) 

pwalk(list(filename=paths,plot=plots),ggsave)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM