简体   繁体   中英

alpha does not change transparency but adds to ggplot2 legend with geom_rect

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.

在此处输入图片说明

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!

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)))

在此处输入图片说明

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...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM