How to fill (color) bars in a bar chart a different color within the bar itself up to a certain number?

0

Issue


groups <- data.frame(group = c("A", "B"),
                                Reels = c(155, 343),
                                Fish = c(41, 221))
groups %>% 
  ggplot(aes(x = group, y = Reels)) + 
  geom_col() +
  labs(
    x = "Group",
    y = "Count"
  ) 

I want to fill the bars in this graph up the amount of fish that were caught. So you can see the different color on the graph. I think I would use the fill argument but am having trouble getting that value to cut off at 41/155, for example.

Sort of like in https://r4ds.had.co.nz/data-visualisation.html section 3.8 where they colors the graph according to diamond clarity.

My goal is two bars – one for A and one for B that are 155 and 343 tall, respectively. I then want to color in up to the amount of Fish (41/155). Any advice? Please let me know if this needs more clarification.

Solution

Try this, with a text label thrown in for good measure:

groups %>% 
  ggplot(aes(x = group, y = Reels)) + 
  geom_col(aes(y = Fish), fill = "red") +
  geom_col(fill = "transparent", color = "black") +
  geom_text(aes(y = Fish + 1, label = paste(Fish, Reels, sep = " / ")), vjust = -0.5) +
  labs(
    x = "Group",
    y = "Count"
  ) 

enter image description here

Answered By – r2evans

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More