-1

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

Sinval
  • 1,315
  • 1
  • 16
  • 25
EMCA
  • 1

1 Answers1

0

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")
Vasily A
  • 8,256
  • 10
  • 42
  • 76
  • That works, but then it doesn't appear in the legend (which is why it was in the aes) - note that this works for other data sets but not this one (actually other data subsets of the same dataset) – EMCA Feb 18 '21 at 20:26
  • Actually i got it working, but it doesn't make sense - if I replace the values=1 with values=c(6,8.5) in the scale_linetype_manual call, then it works. But this doesn't make sense? – EMCA Feb 18 '21 at 20:53
  • your example contains a lot of details which are not relevant to the issue. Try to reduce it and keep only the minimum that is necessary to reproduce the problem. See resources on [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [How to make a reproducible R example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Vasily A Feb 19 '21 at 06:27