简体   繁体   中英

ggplot2 adding an extra legend for the bar colours (when having another legend for background colours)

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:

在此处输入图像描述

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

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM