R ggplot2: Barplot partial/semi stack

前端 未结 1 913
花落未央
花落未央 2021-01-15 04:41

I have a barplot situation where I would like some of the levels to be stacked and some to be dodged. Is this possible? Here is a working example.

library(gg         


        
相关标签:
1条回答
  • 2021-01-15 04:50

    Is this what you want? I adapted from the link which @Henrik pointed out.

    # 1st layer
    g1 <- ggplot(dat1 %>% filter(variable == "full"),
                 aes(x=as.numeric(name) - 0.15, weight=value, fill=variable)) +
      geom_bar(position="identity", width=0.3) + 
      scale_x_continuous(breaks=c(1, 2, 3, 4), labels=unique(dat1$name)) +
      labs(x="name")
    
    # 2nd layer
    g1 + geom_bar(data=dat1 %>% filter(grepl("part", variable)),
                  aes(x=as.numeric(name) + 0.15, fill=variable),
                  position="stack", width=0.3)
    

    0 讨论(0)
提交回复
热议问题