Order categorical data in a stacked bar plot with ggplot2

前端 未结 3 1675
梦毁少年i
梦毁少年i 2020-12-09 19:20

I have a matrix with the following entries:

dput(MilDis[1:200,])
structure(list(hhDomMil = c(\"HED\", \"ETB\", \"HED\", \"ETB\", \"PER\", 
\"BUM\", \"EXP\",          


        
3条回答
  •  情深已故
    2020-12-09 19:43

    If you relevel your hhDomMil as a factor like this:

    o<-c("ETB" "PMA" "PER" "KON" "TRA" "DDR" "BUM" "MAT" "HED" "EXP")
    d$hh<-factor(d$hhDomMil,levels=o)
    

    then your plot will be in the order you like:

    ggplot(d,(aes(x=kclust, fill=hh))) +geom_bar(position="fill")
    

提交回复
热议问题