如何交换facet_wrap的两个图

时间:2019-08-06 19:06:28

标签: r

我希望AVANT图形排在第一位

dt = data.frame(AVG=c("POT","TPOT"),AVANT=c(5000,4400),APRES=c(5020,6500))
data = gather(dt, key="measure",value="value",c("AVANT","APRES"))
ggplot(data,aes(x=AVG, y=value,fill=AVG))+geom_bar(stat='identity')+facet_wrap(~measure)

2 个答案:

答案 0 :(得分:0)

您只需要将度量值放在AVANT作为将APANT左侧的AVANT作为第一个级别的水平中即可。

data.ordered = mutate(data, measure=factor(measure, levels=c("AVANT","APRES")))
ggplot(data.ordered,aes(x=AVG, y=value,fill=AVG))+geom_bar(stat='identity')+facet_wrap(~measure)

答案 1 :(得分:0)

您可以在forcats::fct_rev内部使用forcats::fct_relevel或更安全地使用fcet_wrap,例如

ggplot(data,aes(x=AVG, y=value,fill=AVG)) + geom_bar(stat='identity') + 
facet_wrap(~fct_relevel(measure, c("APRES", "AVANT")))
#facet_wrap(~forcats::fct_rev(measure))