[R] Adding non-data line to legend ggplot2 Maximum Contaminant Level

David Doyle kydaviddoyle at gmail.com
Tue Sep 26 21:51:32 CEST 2017

Hello everyone,

I have a plot showing chloride concentrations for various point over time.
I also have a dotted line that show the Secondary Maximum Contaminant Level
(my screening limit) on the graphs at 250 mg/L.  But I can not figure out
how to include the dotted line / Secondary Maximum Contaminant Level in
the legend.  Any thoughts?  My code is as following and is linked to my
data on the net.

Thank you in advance

#Loads the ggplot2 package.

##This loads your data from your worksheet
MyData <-read.csv("http://doylesdartden.com/Stats/TimeSeriesExample.csv",

#Sets which are detections and nondetects

MyData$Detections <- ifelse(MyData$D_Chloride ==1, "Detected", "NonDetect")

#does the plot
p <- ggplot(data = MyData, aes(x=Year, y=Chloride , col=Detections)) +
  geom_point(aes(shape=Detections)) +

  #sets the detect vs. non-detect colors
  scale_colour_manual(values=c("black","red")) +

  #sets the y scale and log base 10
  scale_y_log10() +

  ##adds line

  #location of the legend
  theme(legend.position=c("right")) +

  #sets the line color, type and size
  geom_line(colour="black", linetype="dotted", size=0.5) +
  ylab("Chloride (mg/L)")

## does the graph using the Location IDs as the different Locations.
p + facet_grid(Location ~ .)

	[[alternative HTML version deleted]]

More information about the R-help mailing list