使用geom_rect时删除图例背景

时间:2017-02-01 23:10:42

标签: r ggplot2 background legend

我在ggplot中制作图表,并希望我的密钥没有背景颜色。由于最后一行,legend.key = element_blank():

#For Testing ggplot(data= Data, aes(x = Date, y = Elev, group = Well, colour = Well)) + geom_line(size = 0.75) + xlab("") + ylab("Elevation (ft.)") + scale_color_brewer(palette = "Spectral") + scale_x_date(breaks = date_breaks("1 year"), date_labels = ("%b %Y")) + theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1), panel.grid.major.x = element_blank(), panel.grid.major.y = element_line( size=.1, color="gray"), panel.grid.minor = element_blank(), axis.line.x = element_line(color = "black"), axis.line.y = element_line(color = "black"), panel.background = element_rect(fill = "white"), legend.key=element_blank())

但是,我还想使用geom_rect对图表的某些部分进行着色。问题是,当我在图表的某些区域添加淡紫色阴影时,图例背景变为薰衣草。事实上,似乎对于我用geom_rect制作的每个lavendar盒子,传说背景变成了薰衣草的深色...下面的代码是上面的代码加上我的geom_rect命令:

ggplot(data= Data, aes(x = Date, y = Elev, group = Well, colour = Well)) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2003-08-11", "%Y-%m-%d"),
                xmax = as.Date("2003-08-14",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2004-04-29", "%Y-%m-%d"),
                xmax = as.Date("2004-12-20",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2005-04-07", "%Y-%m-%d"),
                xmax = as.Date("2005-12-12",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2006-04-21", "%Y-%m-%d"),
                xmax = as.Date("2006-12-08",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2007-03-27", "%Y-%m-%d"),
                xmax = as.Date("2007-12-03",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2008-04-09", "%Y-%m-%d"),
                xmax = as.Date("2008-11-28",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2009-05-11", "%Y-%m-%d"),
                xmax = as.Date("2009-11-26",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2010-04-10", "%Y-%m-%d"),
                xmax = as.Date("2010-10-10",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2011-04-19", "%Y-%m-%d"),
                xmax = as.Date("2011-11-18",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2012-04-12", "%Y-%m-%d"),
                xmax = as.Date("2012-11-14",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2013-05-09", "%Y-%m-%d"),
                xmax = as.Date("2013-11-12",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2014-05-23", "%Y-%m-%d"),
                xmax = as.Date("2014-12-01",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2015-04-10", "%Y-%m-%d"),
                xmax = as.Date("2015-11-28",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_rect(data = Data, 
            aes(xmin = as.Date("2016-04-01", "%Y-%m-%d"),
                xmax = as.Date("2016-12-31",  "%Y-%m-%d"),
                ymin = -Inf, 
                ymax = Inf),
            fill = "lavender", 
            linetype = 0,
            alpha = 0.05) +
  geom_line(size = 0.75) +
  xlab("") + ylab("Elevation (ft.)") +
  scale_color_brewer(palette = "Spectral") +
  scale_x_date(breaks = date_breaks("1 year"),
              date_labels = ("%b %Y")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line( size=.1, color="gray"),
        panel.grid.minor = element_blank(), 
        axis.line.x = element_line(color = "black"),
        axis.line.y = element_line(color = "black"),
        panel.background = element_rect(fill = "white"),
        legend.key=element_blank())

任何人都可以帮助我从我的传奇中删除薰衣草背景吗?以下是代码的主管:

           Date            Well   Elev
1    2002-05-23            MW-3 929.04
2    2002-05-29            MW-3 929.39
3    2002-05-31            MW-3 929.37
4    2002-06-05            MW-3 929.36
5    2002-06-12            MW-3     NA
6    2002-06-13            MW-3 929.47
7    2002-06-19            MW-3 929.42
8    2002-06-26            MW-3 930.02
9    2002-07-05            MW-3 930.00

1 个答案:

答案 0 :(得分:3)

您可以在inherit.aes=FALSE内添加geom_rect来消除图例中的淡紫色背景。我还想指出,您可以大大减少图形所需的代码量。您只需要在geom_rect的一次调用中拥有一个xmin个日期向量和一个xmax个向量的向量,而不是为每对日期分别调用geom_rect。 。例如:

Data = data.frame(x=as.Date(c("2005-01-01", "2010-01-01")), y=c(5,6), group=c("A","B"))

rect.data = data.frame(xmin = as.Date(c("2003-08-11","2004-04-29","2016-04-01")),
                       xmax = as.Date(c("2003-12-14","2004-12-20","2016-12-13")),
                       ymin = -Inf, 
                       ymax = Inf)

ggplot(Data, aes(x, y, colour=group)) +
  geom_rect(data=rect.data, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
            fill="lavender", linetype=0, alpha=1, inherit.aes=FALSE) +
  geom_point(size=3) +
  theme_classic()

enter image description here

此外,如果您可以制定明确的标准来确定着色的开始日期和结束日期,那么您可以通过编程方式直接从数据中提取日期,而不必手动硬编码。