ggplot2: Change color for each facet in bar chart

前端 未结 2 1505
萌比男神i
萌比男神i 2021-02-11 01:26

I have a faceted bar chart done with ggplot2 with this code:

ggplot(data_long, aes(x=region, y=wert)) + 
geom_bar(aes(fill = kat ), position = \"dodge\", width=.         


        
2条回答
  •  忘了有多久
    2021-02-11 01:45

    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")
    

提交回复
热议问题