Home > Enterprise >  Customize fill group ratios ggplot2
Customize fill group ratios ggplot2

Time:09-22

I have the following dataframe:

df <- tribble(
  ~age_group, ~count, ~outcome, ~g1, ~g2, ~g3,
  "first", 10, 110, 0.3, 0.6, 0.1,
  "second", 20, 70, 0.4, 0.3, 0.3,
  "third", 80, 35, 0.25, 0.5, 0.25
)

# # A tibble: 3 × 6
#   age_group count outcome    g1    g2    g3
#   <chr>     <dbl>   <dbl> <dbl> <dbl> <dbl>
# 1 first        10     110  0.3    0.6  0.1 
# 2 second       20      70  0.4    0.3  0.3 
# 3 third        80      35  0.25   0.5  0.25

ggplot(df, aes(x = age_group, y = outcome))  
  geom_col()

I would like to have a bar plot with age_group on the x-axis, outcome on the y-axis. g1, g2, and g3 specify the fill proportions for the respective groups in each bar.

How can I specify these manual fill ratios?

Bonus if you can get the counts below the x-axis as so:

enter image description here

  • Related