Home > Software engineering >  Change color of bar labels in ggplot's stacked bar plot
Change color of bar labels in ggplot's stacked bar plot

Time:12-28

This stacked bar plot has gold coloured bars representing a positive number and red bars representing negative numbers. The number at the end of each bar is correct, however, its colour is wrong. The bar label colour should match the colour of the bar. I can only manage to change the negative labels to match the red colour of the bars.

How do I control the colour of both bar labels?

library("tidyverse")

red <- "#CD5C5C"
gold <- "#FCB700"

df <- structure(list(type = c("up", "up", "up", "up", "up", "down", 
                           "down", "down", "down", "down"), gic_term = c("1yr", "2yr", "3yr", 
                                                                         "4yr", "5yr", "1yr", "2yr", "3yr", "4yr", "5yr"), value = c(6, 
                                                                                                                                     1, 0, 0, 0, -2, -11, -14, -18, -22), value_to_display = c(6, 
                                                                                                                                                                                               1, 0, 0, 0, 2, 11, 14, 18, 22), vjust = c(-0.3, -0.3, -0.3, -0.3, 
                                                                                                                                                                                                                                         -0.3, 1.2, 1.2, 1.2, 1.2, 1.2), bar_num_col = c("#FCB700", "#FCB700", 
                                                                                                                                                                                                                                                                                         "#FCB700", "#FCB700", "#FCB700", "#C8F5C0", "#C8F5C0", "#C8F5C0", 
                                                                                                                                                                                                                                                                                         "#C8F5C0", "#C8F5C0")), row.names = c(NA, -10L), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                                                                    "tbl", "data.frame"))

ggplot(df, aes(fill = type, y = value, x = gic_term))   
  geom_bar(position = "stack", stat = "identity")  
  scale_fill_manual(values = c(red, gold))  
  labs(title = "Stacked Bar Graph")  
  theme_minimal()  
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    legend.position = "none"
  )  
  geom_text(aes(gic_term, label = value_to_display,  vjust = vjust, color = bar_num_col), size = 8)

CodePudding user response:

You can do it by adding scale_color_manual() with the hex color values. and you can either leave your current bar_num_col, or switch to the type field (same effect).

ggplot(df, aes(fill = type, y = value, x = gic_term))   
  geom_bar(position = "stack", stat = "identity")  
  scale_fill_manual(values = c(red, gold))  
  labs(title = "Stacked Bar Graph")  
  theme_minimal()  
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    legend.position = "none"
  )  
  geom_text(aes(gic_term, label = value_to_display,  vjust = vjust, color = type), size = 8) 
  scale_color_manual(values=c("#CD5C5C", "#FCB700"))
  • Related