[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
David


#Loads the ggplot2 package.
library(ggplot2)

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


#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
  geom_hline(aes(yintercept=250),linetype="dashed")+

  #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
https://stat.ethz.ch/mailman/listinfo/r-help
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