Reputation: 179
I am relatively new to R and have a dataset in .csv format that looks like this:
WCF Well Constit Date Value Unit Filtered Flag Median Min Max
299-E24-20_Chloroform_N 299-E24-20 Chloroform 12/6/2016 0.15 ug/L N J 0.145 0.14 0.19
299-E24-20_Chloroform_N 299-E24-20 Chloroform 3/14/2017 0.14 ug/L N J 0.145 0.14 0.19
299-E24-20_Chloroform_N 299-E24-20 Chloroform 6/15/2017 0.14 ug/L N J 0.145 0.14 0.19
299-E24-20_Chloroform_N 299-E24-20 Chloroform 9/20/2017 0.19 ug/L N J 0.145 0.14 0.19
299-E24-20_Sulfide_N 299-E24-20 Sulfide 12/6/2016 5800 ug/L N B 2200 2200 5800
299-E24-22_Sulfide_N 299-E24-22 Sulfide 12/6/2016 1470 ug/L N B 33 33 1470
299-E25-2_Sulfide_N 299-E25-2 Sulfide 11/1/2016 3380 ug/L N NA 33 33 3380
299-E25-2_Sulfide_N 299-E25-2 Sulfide 12/6/2016 1570 ug/L N B 33 33 3380
299-E24-20_Sulfide_N 299-E24-20 Sulfide 3/14/2017 2200 ug/L N UO 2200 2200 5800
299-E24-20_Sulfide_N 299-E24-20 Sulfide 6/15/2017 2200 ug/L N U 2200 2200 5800
299-E24-20_Sulfide_N 299-E24-20 Sulfide 9/20/2017 2200 ug/L N U 2200 2200 5800
299-E24-22_Sulfide_N 299-E24-22 Sulfide 3/13/2017 33 ug/L N U 33 33 1470
299-E24-22_Sulfide_N 299-E24-22 Sulfide 6/15/2017 33 ug/L N U 33 33 1470
299-E24-22_Sulfide_N 299-E24-22 Sulfide 9/18/2017 33 ug/L N U 33 33 1470
299-E25-2_Sulfide_N 299-E25-2 Sulfide 1/25/2017 1.00E+03 ug/L N U 33 33 3380
299-E25-2_Sulfide_N 299-E25-2 Sulfide 3/14/2017 33 ug/L N U 33 33 3380
299-E25-2_Sulfide_N 299-E25-2 Sulfide 4/19/2017 33 ug/L N U 33 33 3380
299-E25-2_Sulfide_N 299-E25-2 Sulfide 6/16/2017 33 ug/L N U 33 33 3380
299-E25-2_Sulfide_N 299-E25-2 Sulfide 9/15/2017 33 ug/L N U 33 33 3380
I have code that I have put together in an attempt to create multiple plots from the dataset and save them as .png's to a specified folder. Whenever I run the code, I don't get any errors but no plots appear to be generated because none are saved to the folder location. The code is listed below. Can anyone please help me find out why I'm not getting any plots?
#call required libraries
library(dplyr)
library(tidyr)
library(readr)
library(ggplot2)
library(magrittr)
library(stringi)
library(lubridate)
library(stats)
#load in datafiles
df <- read_csv ("C:/mypath/_dat.csv", col_names = TRUE)
df %>%
mutate(Date = mdy(Date))
# create graphing function
wcf.plot <- function(df){
wcf_list <- unique(df$WCF)
for( i in (wcf_list)){
plots <-
ggplot(data=subset(df, df$WCF==wcf_list[i]), aes(Date, Value, group = WCF)) +
geom_point(aes(color = Flag)) +
ggtitle(paste(wcf_list[i])) +
geom_abline(lm(data = df, Value~Date)) +
xlab("Sample Date") +
ylab("Conc ug/L or pCi/L")
ggsave(plots, file=paste(plots,'C:/Projects/multiplotter/multiplotter/plots_png/', wcf_list[i], ".png", sep='', scale=2))
print(plots)
}
}
I do get a warning when running the code indicating "8 failed to parse". Does that mean that eight columns of my data are not useable? Thanks!
Upvotes: 2
Views: 930
Reputation: 7948
something like this (data at the bottom of answer),
# setwd('C:/Projects/multiplotter/multiplotter/plots_png/')
for (var in unique(tbl$WCF)) {
ggplot(subset(tbl, WCF == var), aes(Date, Value)) +
geom_point(aes(color = Flag)) +
geom_smooth(method = "lm", se = FALSE) +
labs(x = "Sample Date", y = "Conc ug/L or pCi/L", title = paste0(var))
ggsave(paste0(var,'.png'), width = 20, height = 20, units = "cm")
}
cat('your plots where saved into;', getwd())
#> your plots where saved into; C:/Projects/multiplotter/multiplotter/plots_png/
See also this so answer. Plus, bonus plot using facet_grid()
as it does give a a good sense of what the data in one plot,
ggplot(tbl, aes(Date, Value)) +
geom_point(aes(color = Flag)) +
geom_smooth(method = "lm", se = FALSE) +
labs(x = "Sample Date", y = "Conc ug/L or pCi/L") + facet_grid(. ~ WCF)
df <- structure(list(WCF = structure(c(1L, 1L, 1L, 1L, 2L, 3L, 4L,
4L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("299-E24-20_Chloroform_N",
"299-E24-20_Sulfide_N", "299-E24-22_Sulfide_N", "299-E25-2_Sulfide_N"
), class = "factor"), Well = structure(c(1L, 1L, 1L, 1L, 1L,
2L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("299-E24-20",
"299-E24-22", "299-E25-2"), class = "factor"), Constit = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("Chloroform", "Sulfide"), class = "factor"),
Date = structure(c(3L, 5L, 7L, 11L, 3L, 3L, 2L, 3L, 5L, 7L,
11L, 4L, 7L, 10L, 1L, 5L, 6L, 8L, 9L), .Label = c("1/25/2017",
"11/1/2016", "12/6/2016", "3/13/2017", "3/14/2017", "4/19/2017",
"6/15/2017", "6/16/2017", "9/15/2017", "9/18/2017", "9/20/2017"
), class = "factor"), Value = c(0.15, 0.14, 0.14, 0.19, 5800,
1470, 3380, 1570, 2200, 2200, 2200, 33, 33, 33, 1000, 33,
33, 33, 33), Unit = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "ug/L", class = "factor"),
Filtered = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "N", class = "factor"),
Flag = structure(c(2L, 2L, 2L, 2L, 1L, 1L, NA, 1L, 4L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("B", "J",
"U", "UO"), class = "factor"), Median = c(0.145, 0.145, 0.145,
0.145, 2200, 33, 33, 33, 2200, 2200, 2200, 33, 33, 33, 33,
33, 33, 33, 33), Min = c(0.14, 0.14, 0.14, 0.14, 2200, 33,
33, 33, 2200, 2200, 2200, 33, 33, 33, 33, 33, 33, 33, 33),
Max = c(0.19, 0.19, 0.19, 0.19, 5800, 1470, 3380, 3380, 5800,
5800, 5800, 1470, 1470, 1470, 3380, 3380, 3380, 3380, 3380
)), .Names = c("WCF", "Well", "Constit", "Date", "Value",
"Unit", "Filtered", "Flag", "Median", "Min", "Max"),
class = "data.frame", row.names = c(NA, -19L))
tbl <- df %>% mutate(Date = mdy(Date)) %>% arrange(WCF) %>% group_by(WCF) %>% as_tibble()
Upvotes: 1