Order of factor levels in facet_wrap

后端 未结 2 704
Happy的楠姐
Happy的楠姐 2020-12-21 11:22

I\'d like to produce a facet_wrap where the order of factors within facets is based on the one of the column factor order. The heart of the problem is each group has duplica

2条回答
  •  轮回少年
    2020-12-21 11:46

    This solution makes each group unique and arranges in the desired order, then changes the names back to your original names.

    df_ex$names<-paste(df_ex$address,df_ex$clas,df_ex$No)
    df_ex$names<-factor(df_ex$names,levels=c("A Good 1","B Ugly 1","C Ugly 1", "A Good 2", "C Bad 2", "B Ugly 2", "C Good 3", "A Bad 3", "B Bad 3"))
    
    
    ggplot(df_ex, aes(x=names,y="",fill=clas)) + #x axis bias voltage dependence
      geom_tile() + 
      scale_fill_manual(values=c('Good'="green","Bad"="Blue","Ugly"="black"))+
      facet_wrap(~No,ncol=1,scales = "free_x")+
      theme(legend.position = "top",axis.text.y = element_text(size = 20,angle = 90),axis.text.x = element_text(size=12,face="bold",colour = "black"),
            axis.title.y = element_text(face="bold",size = 20, colour = "black"),
            axis.title.x = element_text(face="bold",size = 20 , colour = "black"),
            strip.text = element_text(size=26, face="bold"),
            strip.background = element_rect(fill="#FFFF66", colour="black", size=0.5),
            plot.title=element_text(face="bold",color="red",size=14),
            legend.title = element_text(colour="black", size=26,face="bold"),
            legend.text = element_text(colour="black", size=18))+
      labs(x = "address",y = "")+
      scale_x_discrete(breaks=df_ex$names, labels=df_ex$address)
    

提交回复
热议问题