Sort legend in ggplot2

后端 未结 2 1947
栀梦
栀梦 2020-12-01 14:05

I have produced a stacked percent barplot from the following data, which is in a csv file,

,ONE,TWO,THREE
1,2432,420,18
2,276,405,56
3,119,189,110
4,90,163,1         


        
相关标签:
2条回答
  • 2020-12-01 14:58

    you can use a new option reverse = TRUE:

    ggplot(datam,aes(x = variable, y = value,fill = factor(as.numeric(ind)))) +
      geom_bar(position = "fill") + scale_y_continuous(labels =percent_format()) +
      scale_fill_discrete("Barcode\nMatch") + xlab("Barcode")+ylab("Reads") +
      guides(fill = guide_legend(reverse = TRUE))
    

    enter image description here

    0 讨论(0)
  • 2020-12-01 15:06

    Add + scale_fill_hue(breaks=c("new order 1","new order 2","new order...")) as in:

    library(ggplot2)
    ggplot(data=PlantGrowth, aes(x=group, fill=group)) + geom_bar() + 
        geom_bar(colour="black", legend=FALSE) + 
        scale_fill_hue(breaks=c("trt1","ctrl","trt2"))
    

    I'd also check out http://wiki.stdout.org/rcookbook/Graphs/Legends%20(ggplot2)/ for more.

    This may have changed and become easier with he new ggplot but I'm not sure.

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