Home > Blockchain >  ggplot: how to draw a plot with colored columns?
ggplot: how to draw a plot with colored columns?

Time:02-05

Hy guys, I'm need to replicate this kind of plot enter image description here

CodePudding user response:

This expanded code is based on @Chamkrai's excellent first-answer, please accept that answer over this. I offer it here for concise clarity on the comments.

df %>%
  mutate(pct = scales::percent((Real_Wage_2 - Real_Wage_1) / Real_Wage_1)) %>%
  pivot_longer(-c(City_Code, pct)) %>%
  group_by(City_Code) %>%
  mutate(pcty = sum(value)) %>%
  ggplot()  
  aes(x = reorder(City_Code, value), y = value, fill = name)  
  geom_col()  
  coord_flip()  
  geom_text(aes(label = pct, y = pcty), data = ~ subset(., name == "Real_Wage_1"), hjust = 1.1)

ggplot barplot with colors and labels

  • Related