Home > database >  Conditionally color bars in ggplot
Conditionally color bars in ggplot

Time:08-04

I am trying to build a stacked chart using three variables but I am having trouble with representing the data.

Here is what the data looks like

local manager bins sum by bins
yes (0.01,0.09] 109
no (0.01,0.09] 109
yes (0.01,0.09] 109
yes (0.01,0.09] 109
no (0.89,0.99] 56
no (0.89,0.99] 56
no (0.99,1] 45
yes (0.69,0.79] 33
yes (0.69,0.79] 33

I want the bars to have the height of y-axis and be colored if local manager is yes/no/mixed, but I don't really know how to do that. For example the color of (0.01,0.09] bar would be orange since it has both yes and no. Thanks for the help!

CodePudding user response:

Here is a solution that generates the desired plot. The main trick is to code the "local manager" column into the desired categories: "yes", "no", "mixed" using summarise. In this example df is your dataframe, except I shortened the name of "sum by bins" column to simply "sum".

library(dplyr)
library(ggplot2)

df |>
  # Group by bins
  group_by(bins) |>
  # Summarise to get yes, no, mixed classes
  summarise(n = length(unique(localmanager)),
            class = ifelse(n == 1, unique(localmanager), "mixed"),
            y = unique(sum)) |>
  # ggplot
  ggplot(aes(x = bins, 
             y = y,
             fill = class))  
  # columns plot
  geom_col()  
  # Change fill colors according to class values
  scale_fill_manual(values = c("mixed" = "orange",
                               "no" = "firebrick3",
                               "yes" = "forestgreen"))

plot

  • Related