我有以下数据集:
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"))
我已经创建了两个海水取样站的粪便大肠杆菌滚动百分位数的图表。我想展示使电台处于“威胁”状态(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")))
感谢任何见解和/或帮助。 谢谢!
答案 0 :(得分:2)
使用模拟数据集,我提出以下解决方案
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的值。 (我还在研究这个问题......)