Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove legend background when using geom_rect

I am making graph in ggplot and would like my key to have no background color. I am able to remove the background color with the following code because of the last line, 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())

However, I'd also like to shade certain parts of my graph using geom_rect. The problem is, when I add lavender shading in certain areas of my graph, the legend background becomes lavender. In fact, it seems that for every lavendar box I make with geom_rect, the legend background becomes a darker shade of lavender... The following code is the code above plus my geom_rect commands:

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

Can anyone help me remove the lavender background from my legend? Here is the head of the code:

           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
like image 367
snalven Avatar asked Aug 24 '26 11:08

snalven


1 Answers

You can get rid of the lavender background in the legend by adding inherit.aes=FALSE inside geom_rect. I also wanted to point out that you can vastly reduce the amount of code needed for your graph. Rather than separate calls to geom_rect for each pair of dates, you just need to have one vector of xmin dates and one vector of xmax dates in a single call to geom_rect. For example:

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

Also, if you can formulate explicit criteria for determining the start and end dates for the shading, then you can pull the dates directly from your data programmatically, rather than having to hard-code them by hand.

like image 143
eipi10 Avatar answered Aug 27 '26 06:08

eipi10



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!