Adding shading alternate areas for categorical variable in a bar plot in ggplot2

前端 未结 2 644
渐次进展
渐次进展 2021-01-21 18:26

I am trying t o plot a bar plot using ggplot2as follows:

library(ggplot2)
ggplot(mtcars, aes(factor(carb))) +
  geom_bar() +
  coord_flip()
<         


        
2条回答
  •  星月不相逢
    2021-01-21 18:31

    Perhaps something along these lines where you overlay the wider shaded bars, which vary in color, with the darker smaller bar?

    ggplot(mtcars, aes(factor(carb))) +
      geom_bar(width = 1.1, aes(x = factor(carb), fill = ifelse(mtcars$carb %in% c(1,3,6), "blue", "transparent"))) +
      guides(fill = FALSE) +
      geom_bar(width = 0.7) +
      scale_fill_manual(values = c("transparent", "blue")) +
      coord_flip()
    

提交回复
热议问题