我是R的新手,并且有一个.csv格式的数据集,如下所示:
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
我有一些代码,我试图从数据集中创建多个图并将它们保存为指定文件夹中的.png&。每当我运行代码时,我都不会收到任何错误,但似乎没有生成图表,因为没有任何图表保存到文件夹位置。代码如下所示。任何人都可以帮我找出为什么我没有得到任何情节?
#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)
}
}
我在运行代码时收到警告,表示&#34; 8无法解析&#34;。这是否意味着我的数据的八列不可用?谢谢!
答案 0 :(得分:1)
类似的东西(答案底部的数据),
# 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/
另见this so answer。另外,使用facet_grid()
的奖励情节,因为它可以很好地了解一个地块中的数据,
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()