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

John Kane jrkrideau at yahoo.ca
Sun Oct 1 12:44:03 CEST 2017

I just glanced at the problem but I think you would have to create a new variable to replace the hline. What about adding some text annotation in the graph instead?

    On Tuesday, September 26, 2017, 3:51:46 PM EDT, David Doyle <kydaviddoyle at gmail.com> wrote:  
 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]]

R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
	[[alternative HTML version deleted]]

More information about the R-help mailing list