SeGa
SeGa

Reputation: 9809

Fine adjust legend of overlayed barplot

I have 2 datasets that I want to plot together. If the data from the second dataset matches a certain condition, it should be plotted with a semi-transparent red color, otherwise it should not be visible.

Furthermore, I would like to include a sub-legend for that second plot but only with 1 element (red color and label = FALSE)

In the example below, only the first bar from the variable "id_1" matches that criteria and therefore has an overlayed red bar. But I am still missing the correct legend.


Test Data:

df <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
                                      1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
                                      1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("id_1", "id_2"), class = "factor"), 
                     day = structure(c(17897, 17903, 17910, 17917, 17897, 17903, 
                                       17910, 17917, 17897, 17903, 17910, 17917, 17897, 17903, 17910, 
                                       17917, 17897, 17903, 17910, 17917, 17897, 17903, 17910, 17917, 
                                       17897, 17903, 17910, 17917, 17897, 17903, 17910, 17917), class = "Date"), 
                     variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
                                            2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                            4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("reas_1", "reas_12", 
                                                                                        "reas_123", "reas_1234"), class = "factor"), value = c(0, 
                                                                                                                                               0, 3, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 2, 1, 0, 1, 0, 0, 5, 
                                                                                                                                               0, 0, 0, 2, 0, 1, 6, 10, 0, 1, 9, 6)), row.names = c(NA, 
                                                                                                                                                                                                    -32L), class = "data.frame")
overlay <- structure(list(id = structure(1:2, .Label = c("id_1", "id_2"), class = "factor"), 
                          day = structure(c(17897, 17897), class = "Date"), variable = structure(c(1L, 
                                                                                                   1L), .Label = "plausible_sum", class = "factor"), value = c(10, 
                                                                                                                                                               0), fill = c("red", "transparent")), row.names = c(NA, -2L
                                                                                                                                                               ), class = "data.frame")

cbPalette <- c('#3652a3', '#60a0df', '#b7dbff', '#dd0000', '#fd7676')
legendLabels <- c("Var1", "Var2", "Var3", "Var4")


Plot Code:

library(ggplot2)

ggplot() +
  geom_bar(data = df, aes(x = day, y = value, fill = variable), stat = "identity", width = 3) +
  geom_bar(data = overlay, aes(x = day, y = 16), fill = overlay$fill,
           alpha = c(0.3, 0), stat="identity", width = 3) +
  facet_wrap(~id, nrow = 1) +
  scale_fill_manual(name = "Vars: ", values = c(cbPalette), labels = c(legendLabels))

Result vs. Expected:

The right image has a different legend. enter image description here

Upvotes: 2

Views: 42

Answers (1)

JdeMello
JdeMello

Reputation: 1718

Assign a value in aes() in the geom_bar(data=overlay,...) portion. That allows you to manipulate the legend box for this element in the scale_*_manual() function. In this example, I've assigned aes(..., colour="FALSE") and edited the legend in scale_colour_manual() using the guide argument. override.aes() allows you to manipulate aesthetic parameters of the legend.

library(ggplot2)

ggplot() +
  geom_bar(data = df, aes(x = day, y = value, fill = variable), stat = "identity", width = 3) +
  geom_bar(data = overlay, aes(x = day, y = 16, colour="FALSE"), fill = overlay$fill,
           alpha = c(0.3, 0), stat="identity", width = 3) +
  facet_wrap(~id, nrow = 1) +
  scale_fill_manual(name = "Vars: ", values = c(cbPalette), labels = c(legendLabels)) + 
  scale_colour_manual(values = c("FALSE" = "transparent"), 
                      name=c("Overlay"),
                      guide=guide_legend(override.aes = list(fill=overlay$fill[[1]], 
                                                             alpha=0.3))) 

Plot:

enter image description here

Upvotes: 1

Related Questions