ggplot2: Change color for each facet in bar chart

本小妞迷上赌 提交于 2019-12-04 15:26:18

I'm not sure this is the best way to communicate your information, but this is how I'd approach it. Just map fill to region, and use alpha for year. Mine will be a bit different to yours because you didn't provide the structure of the data.

ggplot(data_long, aes(type, wert)) + geom_bar(aes(fill = region, alpha = factor(kat)), position = "dodge", stat = "identity") + 
  scale_alpha_manual(values = c(0.6, 1)) +
  facet_grid(. ~ region) +
  theme_bw() + theme( strip.background  = element_blank(),
                      panel.grid.major = element_line(colour = "grey80"),
                      panel.border = element_blank(),
                      axis.ticks = element_blank(),
                      panel.grid.minor.x=element_blank(),
                      panel.grid.major.x=element_blank() ) +
  theme(legend.position="bottom")

Based on the answer @alexwhan, I could manually set the colors using this code:

  farb <- c("#428953", "#CE2929", "#A3DD57", "#77E599", "#5675D6", "#65ECEF", "#FF8B07", "#D0B100", "#636363")

  ggplot(data_long, aes(region, wert)) +
  geom_bar(aes(fill = type, alpha = factor(kat)), position = "dodge", stat = "identity") + 
  scale_alpha_manual(values = c(0.6, 1)) +
  facet_grid(. ~ type) +
  theme_bw() + theme( strip.background  = element_blank(),
                      panel.grid.major = element_line(colour = "grey80"),
                      panel.border = element_blank(),
                      axis.ticks = element_blank(),
                      panel.grid.minor.x=element_blank(),
                      panel.grid.major.x=element_blank() ) +
  theme(legend.position="bottom") +
  scale_fill_manual(values= farb)

The color scale farbhas to be in the same order as the facets. Produces this chart:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!