Home > Back-end >  Add color column to geom_bar plot with ggplot2
Add color column to geom_bar plot with ggplot2

Time:09-25

I would need help in order to add scare colors into a geom_bar plot from a ggplot2 figure.

Here is what I can manage to do so far :

    head(data)
  x  y group Axis_color
1 A 25     F         G1
2 A 88     G         G1
3 A 88     H         G1
4 A 22     I         G1
5 A 18     J         G1
6 B 54     F         G3

color_list<- c("F"="orange", "G"="darkgreen", "H"="grey", "I"="pink", "J"="purple", "G1"="blue","G2"="red","G3"="green")
ggplot(data, aes(x = x, y = y, fill = group, label = y))   
  coord_flip() 
  geom_bar(stat = "identity")

Which gives:

enter image description here

But I would like to add the column "Axis_color" in order to add colors stored in the color_list such as :

enter image description here

As you can see I get two different legends axis as well.

Here are the data if it can helps :

structure(list(x = c("A", "A", "A", "A", "A", "B", "B", "B", 
"B", "B", "C", "C", "C", "C", "C", "D", "D", "D", "D", "D", "E", 
"E", "E", "E", "E"), y = c(25, 88, 88, 22, 18, 54, 25, 37, 68, 
72, 36, 84, 17, 64, 48, 15, 17, 72, 61, 25, 66, 10, 18, 99, 63
), group = c("F", "G", "H", "I", "J", "F", "G", "H", "I", "J", 
"F", "G", "H", "I", "J", "F", "G", "H", "I", "J", "F", "G", "H", 
"I", "J"), Axis_color = c("G1", "G1", "G1", "G1", "G1", "G3", 
"G3", "G3", "G3", "G3", "G1", "G1", "G1", "G1", "G1", "G2", "G2", 
"G2", "G2", "G2", "G3", "G3", "G3", "G3", "G3")), row.names = c(NA, 
-25L), class = "data.frame")

CodePudding user response:

library(ggnewscale)
library(tidyverse)

data <- structure(list(x = c(
  "A", "A", "A", "A", "A", "B", "B", "B",
  "B", "B", "C", "C", "C", "C", "C", "D", "D", "D", "D", "D", "E",
  "E", "E", "E", "E"
), y = c(
  25, 88, 88, 22, 18, 54, 25, 37, 68,
  72, 36, 84, 17, 64, 48, 15, 17, 72, 61, 25, 66, 10, 18, 99, 63
), group = c(
  "F", "G", "H", "I", "J", "F", "G", "H", "I", "J",
  "F", "G", "H", "I", "J", "F", "G", "H", "I", "J", "F", "G", "H",
  "I", "J"
), Axis_color = c(
  "G1", "G1", "G1", "G1", "G1", "G3",
  "G3", "G3", "G3", "G3", "G1", "G1", "G1", "G1", "G1", "G2", "G2",
  "G2", "G2", "G2", "G3", "G3", "G3", "G3", "G3"
)), row.names = c(
  NA,
  -25L
), class = "data.frame")

color_list <- c("F" = "orange", "G" = "darkgreen", "H" = "grey", "I" = "pink", "J" = "purple", "G1" = "blue", "G2" = "red", "G3" = "green")


data %>%
  as_tibble() %>%
  ggplot(aes(x, y))  
  geom_bar(aes(fill = group), stat = "identity")  
  scale_fill_manual(
    values = color_list[names(color_list) %>% discard(~ .x %>% str_starts("G[0-9]"))]
  )  
  new_scale_fill()  
  geom_bar(aes(fill = Axis_color, y = -10), stat = "identity")  
  scale_fill_manual(
    values = color_list[names(color_list) %>% keep(~ .x %>% str_starts("G[0-9]"))]
  )  
  coord_flip()

Created on 2021-09-25 by the reprex package (v2.0.1)

CodePudding user response:

One option to achieve your desired result would be to make use of geom_tile and the ggnewscale package like so.

  1. For the geom_tile I make use of a simplified dataset containing only the unique or distinct categories and axis color values. Depending on your desired result you could adjust the width of the tiles via the width argument.
  2. As geom_tile by default will result in a slightly different look of the legend keys I make use of the override.aes argument of guide_legend so that the legend keys are the same as for the geom_col

Note that I switched to geom_col and switched the role of the x and y aesthetics which allows to get rid of the coord_flip.

library(ggplot2)
library(ggnewscale)

color_list<- c("F"="orange", "G"="darkgreen", "H"="grey", "I"="pink", "J"="purple", "G1"="blue","G2"="red","G3"="green")
ggplot(data, aes(x = y, y = x, fill = group, label = y))   
  geom_col()  
  scale_x_continuous(expand = c(.025, 0))  
  scale_fill_manual(values = color_list[unique(data$group)], guide = guide_legend(order = 1))  
  new_scale_fill()  
  geom_tile(data = dplyr::distinct(data, x, Axis_color), aes(x = -15, y = x, fill = Axis_color), 
            height = .9, width = 15, inherit.aes = FALSE)  
  scale_fill_manual(values = color_list[sort(unique(data$Axis_color))], 
                    guide = guide_legend(order = 2, override.aes = list(lwd = .5, color = NA)))

  • Related