R ggplot barplot; Fill based on two separate variables

前端 未结 3 1882
挽巷
挽巷 2020-12-13 20:31

A picture says more than a thousand words. As you can see, my fill is based on the variable variable.

Within each bar there is however mul

相关标签:
3条回答
  • 2020-12-13 20:39

    Adding alpha=complexity might work:

    ggplot(short.m, aes(x=Method, y= value/100 , fill=variable, alpha=complexity)) +
    geom_bar(stat="identity",position="dodge", colour="black") + coord_flip()
    
    0 讨论(0)
  • 2020-12-13 20:40

    You might need to separate your Method and variable factors. Here are two ways to do that:

    Use facet_wrap():

        ggplot(short.m, aes(x=variable, y=value/100, fill=Complexity)) + 
        facet_wrap(~ Method) + geom_bar(position="stack", colour="black") +
        scale_alpha_manual(values=c(0.1, 0.5, 1)) + coord_flip()
    

    Use both on the x-axis:

        ggplot(short.m, aes(x=Method:variable, y=value/100, group=Method, fill=variable, alpha=Complexity,)) + 
        geom_bar(stat="identity", position="stack", colour="black") +
        scale_alpha_manual(values=c(0.1, 0.5, 1)) + coord_flip()
    
    0 讨论(0)
  • 2020-12-13 20:46

    This is far from perfect, but hopefully a step in the right direction, as it's dodged by variable, but still manages to represent Complexity in some way:

    ggplot(short.m, aes(x=Method, y=value/100, group=variable, fill=variable, alpha=Complexity,)) + 
      geom_bar(stat="identity",position="dodge", colour="black") +
      scale_alpha_manual(values=c(0.1, 0.5, 1)) +
      coord_flip()
    

    enter image description here

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