Plotting NDs
One very useful procedure is to plot nondetects a different color and/or symbols.
Here is the simple code to load and generate the plot. The example spreadsheet is below.
#Add line between the points
lines(Year,MW1)
#NDs plotted different colors For spreadsheet with Year and data in MW01 and Detect on and off in D_MW01
attach(data)
reg1 <- lm(MW1~Year)
par(cex=1)
plot(Year, MW1, col=ifelse(D_MW1, "black", "red"),ylab = "Parameter X mg/L", pch=ifelse(D_MW1, 19, 17), cex = 1.5)
#Load your data. The data is in a spreadsheet named nd-spreadsheet and we are going to call it data in R
data <- read.table("nd-spreadsheet.csv", header=TRUE, sep=",",)
#set the dir to where you data is
setwd("c:/R")
#Load your data. The data is in a spreadsheet named nd-spreadsheet and we are going to call it data in R
data <- read.table("nd-spreadsheet.csv", header=TRUE, sep=",",)
#NDs plotted different colors For spreadsheet with Year and data in MW01 and Detect on and off in D_MW01
attach(data)
reg1 <- lm(MW1~Year)
par(cex=1)
plot(Year, MW1, col=ifelse(D_MW1, "black", "red"), pch=ifelse(D_MW1, 19, 17), cex = 1.5)
# Done
__________________________________________________________________________________________
#BELLS AND WHISTLES CODE (Has a regression line with confidence intervals at 95% around it)
Notes Read 1st:
lines with # in front is for your information and will be ignored by R.
You will need make a dir on your C drive named R.
You will need to save the attached spreadsheet to that dir.
Nondetects are defined by the 0 in the D_MW1 column and detects have a 1
_____________________________________________________
#SIMPLE CODE
#set the dir to where you data is
setwd("c:/R")
#Adds confindance bands at 0.95 level
ciband= with(data,predict(reg1,as.data.frame(Year),interval='confidence',level=.95))
ciband= data.frame(Year,ciband)
with(ciband,lines(Year,fit,lty=1,lwd=3))
with(ciband,lines(Year,lwr,lty=3,lwd=3,col="blue"))
with(ciband,lines(Year,upr,lty=3,lwd=3,col="blue"))
#Add Legend to graph
legend("topleft", c("Confidence interval 95%","Regression", "Detected", "NonDetect"), col = c("blue",1, "black","red"), cex = 1,
text.col = "black", lty = c(2, 1 ,-1, -1), pch = c(-1, -1, 19, 17),
merge = TRUE, bg = 'gray90')
#Add title
title(main="MW1")
# Done
_________________________________________________________
Here is a little different ver using a different data set format.
#Makes the scale log
scale_y_log10()+
##sets the colors
scale_colour_manual(values=c("black","red")) +
#location of the legend
theme(legend.position=c(0.05,0.1)) +
#sets the line color, type and size
geom_line(colour="black", linetype="dotted", size=0.5) +
##Graph title
ggtitle("Antimony mg/L")
## does the graph using the Well IDs as the different wells.
p + facet_grid(Well.ID ~ .)
#saves the graph to the working dir. Note if you want pdf just change jpg to pdf
ggsave("Antimony.jpg")
##Note you can adjust the print size by adjusting the output window in R or you can do some searches for ggplot size.
#######
#does the plot
p <- ggplot(data = mydata, aes(x=Year, y=Antimony, col=Detections)) +
geom_point(aes(shape=Detections)) +
#Sets whic are detections and nondetects
mydata$Detections <- ifelse(mydata$D_Antimony==1, "Detected", "NonDetect")
#Loads data
mydata <-read.csv("http://doylesdartden.com/R/2014_02_data.csv", sep=",")
#Loads ggplot2 package NOTE you must install it 1st.
library(ggplot2)
#sets the working dir
setwd("c:/R")