[R] Adding non-data line to legend ggplot2 Maximum Contaminant Level
John Kane
jrkrideau at yahoo.ca
Thu Oct 5 15:49:20 CEST 2017
Well, here is one way but it seems a bit clumsy.
In words, I created a new data.frame with "250" in the Chloride vector and "SMCL" in the Detections vector and supplessed one legend.
Warning: For my convenience I am using different data.frame names .
library(ggplot2)
MyData <-read.csv("http://doylesdartden.com/Stats/TimeSeriesExample.csv", sep=",")
MyData$Detections <- ifelse(MyData$D_Chloride ==1, "Detected", "NonDetect")
dat1 <- MyData[, c(1, 3, 4, 6)]
dat2 <- dat1[, c(1,2)]
dat2$Detections <- rep("SMCL", nrow(dat2))
dat$Chloride <- rep(250, nrow(dat2))
dat3 <- rbind(dat1, dat2)
p <- ggplot(dat3, aes(Year, Chloride, colour = Detections)) +
geom_point(aes(shape=Detections), show.legend = F) +
geom_line( aes(Year, Chloride))
On Tuesday, October 3, 2017, 3:17:00 PM EDT, David Doyle <kydaviddoyle at gmail.com> wrote:
Hi John,
I used your recommendation of adding a new variable (see below) and it is plotting the line at the 250 mg/L mark but I still can not figure out how to get it added to the legend.
Thanks again for your help.
David
#Loads the ggplot2 package. NOTE you have to install it before hand.library(ggplot2)
##This loads your data from your worksheetMyData <-read.csv("http://doylesdartden.com/Stats/TimeSeriesExample.csv", sep=",")
#Sets which are detections and nondetects
MyData$Detections <- ifelse(MyData$D_Chloride ==1, "Detected", "NonDetect")
# adds my screening LevelMyData$SMCL <- rep(250, nrow(MyData))
#does the plotp <- ggplot(data = MyData, aes(x=Year, y=Chloride, col=Detections)) + geom_point(aes(shape=Detections)) + geom_line(data = MyData,aes(y=SMCL))+ #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() + #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 ~ .)
On Mon, Oct 2, 2017 at 5:09 AM, John Kane <jrkrideau at yahoo.ca> wrote:
MyData$newvar <- rep(250, nrow(MyData))
Again, I have not tried graphing this.
On Sunday, October 1, 2017, 10:38:09 PM EDT, David Doyle <kydaviddoyle at gmail.com> wrote:
Hi John,
How do I add a second y variable? ThanksDavid
On Sun, Oct 1, 2017 at 5:44 AM, John Kane <jrkrideau at yahoo.ca> wrote:
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