EMCA
EMCA

Reputation: 1

R geom_hline only showing solid lines

I have some plots I'm doing up with ggplot2, using a function for the basic plot then adding some elements. I have an issue with one involving adding a geom_hline, but where it works for other plots, this one doesn't seem to want to show the line as anything other than a solid. And when I scale_linetype_manual for this line, I have to make the values = 1 otherwise it removes the line (the yintercept = 0.5). The code is as follows:

#Aluminium (dissolved)
prm <- dat2[dat2$ParamID == "Aluminium (dissolved)",]
prm <- prm[prm$SiteID %in% c("SW1","SW2","SW3"),]
gplt <- ggplotCust(prm, c(0.05,1.0), dateStart, dateEnd, "Concentration (mg/L)",0.1) +
scale_y_continuous(trans='log10') +
  geom_hline(aes(yintercept=0.5,linetype="dotted"),colour="red",show.legend = NA) +
  scale_linetype_manual(name="Guideline Values",values=1, breaks=waiver(), labels="EA Criteria")

The first two lines subset the data (parameter and sites), and the third calls the custom base plot:

ggplotCust <- function(prm, lm, dateStart, dateEnd, yLbl, jt){

  ggplot(prm,aes(DateTime, rectRes)) +
    geom_point(aes(y = jitter(rectRes,jt), colour =SiteID, shape=SiteID),size=2) +
    geom_line(aes(y = jitter(rectRes,jt), colour =SiteID),lwd=1) +
    scale_color_manual(name  ="Site", values = c("GW1" = 'lightsalmon1', "SW1" = 'dodgerblue4',
                                                 "SW2"='forestgreen', "SW3"='purple')) +
    scale_shape_manual(name  ="Site", values = c("GW1" = 16, "SW1" = 15,
                                                 "SW2"=3, "SW3"=7)) +
    theme_minimal() +
    theme(legend.position="bottom") +
    labs(x = "Date", y = yLbl, caption = prm$ParamID[1]) +
    scale_x_date(date_breaks = "2 months", date_labels = "%b-%y") +
    coord_cartesian(xlim= as.Date(c(dateStart,dateEnd)), ylim = lm)
}

Variables are the dataset, y limits (vector), start and end dates to zoom in, y label and jitter size. So the issue is that while this works for similar data that is >1, correctly dotting or dashdotting the linetype, it doesn't with this one. Removing the log transform doesn't work either. Any ideas? The data is as follows (simplified couple of lines):

Row DateTime ParamID SiteID rectRes
114 20/04/2010 Aluminium (dissolved) SW1 0.14
154 20/07/2010 Aluminium (dissolved) SW1 0.08
196 21/10/2010 Aluminium (dissolved) SW1 0.13
234 19/01/2011 Aluminium (dissolved) SW1 0.07
289 26/05/2011 Aluminium (dissolved) SW1 0.03
328 16/08/2011 Aluminium (dissolved) SW1 0.08

Any help is greatly appreciated! Cheers

Upvotes: -1

Views: 342

Answers (1)

Vasily A
Vasily A

Reputation: 8646

Move the linetype="dotted" out of aes().

UPD. if you want to keep it in the legend, you can modify your last 2 lines to the following:

  geom_hline(aes(yintercept=0.5,linetype="dotted"),colour="red",show.legend = NA) +
  scale_linetype_manual(name="Guideline Values",values=c(dotted=2), labels="EA Criteria")

Upvotes: 0

Related Questions