[英]alpha does not change transparency but adds to ggplot2 legend with geom_rect
我有以下數據集:
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"))
我創建了一個圖表,顯示了兩個海水采樣站糞便大腸菌群的第90個百分位數。 我想演示使站點處於“威脅”狀態(30-43)的值范圍。 我差不多了! 我使用geom_rect創建了一個盒子,並使其帶有標簽的紅色。 我只想使其有些透明,因為紅色原樣不堪重負。 每次我向方程式添加alpha時,它都不會更改紅色,而是在圖例上方添加一個怪異的框。 閱讀過alpha的其他問題后,我嘗試將其移動到其他位置,並在括號內移動,這樣我會得到錯誤或看起來更糟的東西。
我的代碼:
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")))
任何見解和/或幫助表示贊賞。 謝謝!
我使用模擬數據集提出以下解決方案
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)))
我還不明白為什么覆蓋alpha時需要使用.5而不是.125的值。 (我仍在調查此問題...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.