Home > Blockchain >  ggplot2 adding an extra legend for the bar colours (when having another legend for background colour
ggplot2 adding an extra legend for the bar colours (when having another legend for background colour

Time:02-11

I have a plot with a legend for how to interpret the background color - but I would also like a legend for the bar colours, something like:

Title: Type

[blue square] A-type

[black square] B-type

This is the plot:

enter image description here

Code to make the plot:

# Example data
data <- data.frame(
  Type = c("A1","A2", "B1", "B2") ,  
  Value = c(5, 6, 3, 2)
)

background_area_name <- as_factor(c("very weak", "weak", "moderate",  "strong", "very strong"))
levels(background_area_name)

background_data <- tibble(xmin = -Inf, xmax = Inf,
                          ymin = c(0, 2, 4, 6, 8),
                          ymax = c(   2, 4, 6, 8, Inf),
                          "Value strength" = factor(background_area_name, 
                                                          background_area_name
                          ))

# Plot (with missing legend for the bars)
ggplot(data, aes(x=Type, y=Value, 
                 width = 0.4))   
  
  geom_rect(data = background_data, 
            aes(ymin = ymin, 
                ymax = ymax, 
                xmin = xmin, 
                xmax = xmax, 
                fill = `Value strength`),
            inherit.aes = FALSE,
            alpha = 0.8)  
  
  geom_bar(stat = "identity",
           fill = c(rep("blue", 2), rep("black", 2)))   
  
  scale_fill_manual(values = alpha(c("#f0f5f5", 
                                     "#e0ebeb", 
                                     "#d1e0e0", 
                                     "#c2d6d6", 
                                     "#b3cccc"), 
                                   0.99),
                    guide = guide_legend(reverse = TRUE))  
  
  ylab("Value")  
  
  scale_y_continuous(breaks = c(0, 2, 4, 6, 8, 10),
                     expand = c(0, 0.00))  
  #                 expand = expansion(add = 0.06))  
  
  guides(colour = guide_legend(reverse=T)) 

CodePudding user response:

You can achieve this using the ggnewscale package:

library(ggnewscale)

ggplot(data, aes(x = Type, y = Value, width = 0.4))   
  geom_rect(data = background_data, 
            aes(ymin = ymin, 
                ymax = ymax, 
                xmin = xmin, 
                xmax = xmax, 
                fill = `Value strength`),
            inherit.aes = FALSE,
            alpha = 0.8)  
  scale_fill_manual(values = alpha(c("#f0f5f5", 
                                     "#e0ebeb", 
                                     "#d1e0e0", 
                                     "#c2d6d6", 
                                     "#b3cccc"), 
                                   0.99),
                    guide = guide_legend(reverse = TRUE))  
  new_scale_fill()  
  geom_bar(stat = "identity", aes(fill = paste0(substr(Type, 1, 1), "-type")))   
  scale_fill_manual(values = c("blue", "black"), name = "Type")    
  ylab("Value")  
  scale_y_continuous(breaks = c(0, 2, 4, 6, 8, 10),
                     expand = c(0, 0.00))  
  guides(colour = guide_legend(reverse = TRUE))

enter image description here

  • Related