1
votes

I have the following data set:

dput(ByStationR90Data[1:5,])

structure(list(Station = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("125", 
"137"), class = "factor"), SampleDate = structure(c(13216, 13313, 
13342, 13397, 13488), class = "Date"), Date = c(20060309, 20060614, 
20060713, 20060906, 20061206), FecalColiform = c(2, 2, 79, 2, 
2), Flog = c(0.301029995663981, 0.301029995663981, 1.89762709129044, 
0.301029995663981, 0.301029995663981), Avlog = c(NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_), STD = c(NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), F90 = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_)), .Names = c("Station", "SampleDate", "Date", "FecalColiform", 
"Flog", "Avlog", "STD", "F90"), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -5L), vars = list(Station), drop = TRUE, indices = list(
0:4), group_sizes = 5L, biggest_group_size = 5L, labels = structure(list(
Station = structure(1L, .Label = c("125", "137"), class = "factor")), class = "data.frame", row.names = c(NA, 
-1L), vars = list(Station), drop = TRUE, .Names = "Station"))

I have created a graph of the rolling 90th percentiles of fecal coliform for two marine water sampling stations. I want to demonstrate the the range of values that put the station in a "Threatened" status (30-43). I almost have it! I have used geom_rect to create a box and made it red, with a label. I just want to make it somewhat transparent because the red is overwhelming as is. Every time I add alpha to the equation it does not change the red color and instead adds a weird box above the legend. Reading through other problems with alpha, I have tried moving it to different places, and moved parentheses around and I either get errors or something that looks worse.

enter image description here

My code:

ThreatSt <- ggplot(subset(ByStationR90Data, Station %in% c("125", "137"))) +
    geom_hline(yintercept = 43, color = "red", linetype="dashed", size = .7, show_guide = TRUE) +
    geom_rect(mapping = aes(xmin = as.Date(c('2010-10-01')), xmax = as.Date(c('2016-04-01')), 
        ymax = 43, ymin = 30, fill = "Threatened Zone", show_guide = FALSE)) +
    scale_fill_manual("", breaks = "Threatened Zone", values = "red", alpha =  0.5)+
    geom_line(aes(SampleDate, F90, group = Station, colour = Station), size=1.5) +
    scale_color_brewer(palette = "Dark2")

ThreatSt <- ThreatSt +
    theme_bw() +
    ggtitle("Fecal Coliform Rolling 90th Percentiles for Stations 125 and 137
      \n Hood Canal #3 Growing Area") +
    ylab("Fecal Coliform (fc/100 ml)") +
    annotate(geom="text", x=as.Date("2015-01-01"), y=45,label="NSSP Limit", color="red") +
    scale_y_continuous(breaks=seq(0,100,10), limits = c(10,60)) +
    scale_x_date( 
    limits = as.Date(c('2011-01-01', '2016-01-01')), 
    labels=(c("2011", "2012", "2013", "2014", "2015", "2016")))

Any insight and/or help is appreciated. Thanks!

1
Try moving the alpha = 0.5 to the geom_rect layer. It's an aesthetic and should be in the geom.joran
I believe show_guide (show.legend in ggplot2 2.0.0) should be outside of the aes mapping in geom_rect.aosmith
Could you try to load your output of dput? I'm not able to read...Erwan LE PENNEC
joran and aosmith, Thank you for the suggestions, however I tried both of them and my output is exactly the same.Lindsey Hamilton
The proposed changes seem to work fine for me: geom_rect(mapping = aes(xmin = as.Date(c('2010-10-01')), xmax = as.Date(c('2016-04-01')), ymax = 43, ymin = 30, fill = "Threatened Zone"), alpha = .5, show_guide = FALSE)aosmith

1 Answers

3
votes

Using a mock dataset, I propose the following solution

df <- data.frame(SampleDate = c(1,2,3,1,2,3),
                 Station = c("A","A","A","B","B","B"),
                 R90 = c(90,95,100,85,100,120))
 library(ggplot2)
ggplot(data = df) +
  geom_line(aes(x = SampleDate, y = R90, group = Station, color = Station), size = 1.5) +
  geom_rect(aes(fill = "Threatened Zone"), xmin = -Inf, xmax = +Inf,
            ymin = 95, ymax = 105, alpha = .125) +
  scale_fill_manual(breaks = "Threatened Zone", values = "red",
                    guide = guide_legend(title = "Threatened Zone",
                                         override.aes = list(alpha = .5)))

enter image description here

I don't understand yet why I need to use a value of .5 instead of .125 when overriding alpha. (I'm still invesigating this issue...)