plotnine 中离散 x 轴的逆序

reverse order of discrete x axis in plotnine

在 python 中,我试图颠倒 x 轴的顺序,使 2015 年在最左边,2019 年在右边。目标是在我 coord_flip() 情节之后将 2019 年放在底部。参见 here。

我尝试使用 scale_x_reverse 但它不支持离散轴。还有其他建议吗?

ggplot(df4) + geom_bar(aes(x='fiscal_year', y='prime_awarded_value', fill='color_of_money'), stat = 'identity', size=5, alpha=.8)\
+ scale_fill_manual(values=("#AED6F1", "#96ceb4","#BFC9CA","#aa6f73"))\
+ labs(title="Color of Money by Fiscal Year", x='Fiscal Year', y='Total Prime Obligations', fill='Color of Money')\
+ scale_y_continuous(breaks=(0, 50000000000, 100000000000, 150000000000, 200000000000, 250000000000, 300000000000, 350000000000), labels=('0', 'B', '0B', '00B', '00B', '0B', '0B', '0B'), limits=(0,150000000000))\
+ geom_text(aes(x='fiscal_year', y='prime_awarded_value', fill = 'color_of_money', label='money', angle=30), size=16, position = position_stack(vjust = .5))\
+ theme(figure_size=(20, 10), \
        
        #axis_text = element_text(margin={'t':5, 'r':5,'l':5, 'b':40}),\
        axis_text_x=element_text( hjust=.5, vjust=1, size=14, color ='black', face = 'plain'),\
        axis_text_y=element_text(angle=30, hjust=1, vjust=.5, size=14, color ='black', face = 'plain'),\
        axis_title = element_text(size = 24, face = "bold"), \
        
        #axis_ticks =element_blank(),\
        axis_ticks_length = 5,\
        axis_ticks_pad = 35,\
    
        legend_text = element_text(size =16),\
        legend_title = element_text(size =18, face = "bold", color = "black", vjust=0),\
        legend_position=(.267, .93), legend_direction='horizontal',\
        legend_box_margin=10,\
        legend_entry_spacing=10,\
        legend_background=element_rect(color="grey", size=2, fill='white'),\
    
        plot_title = element_text(size = 24, weight = 'bold', lineheight=7),\
        #plot_background = element_rect(fill='#D4DBDB'),\
        panel_background = element_rect(fill = "white", color= "grey"),\
        panel_border = element_rect(size = 2),\
        panel_grid_major_y =  element_line(colour = "grey", linetype = "solid", size = .3))

使用分类列并根据需要设置类别的顺序。

df4['fiscal_year'] = pd.Categorical(df4['fiscal_year'], categories=[2019, 2018, 2017, 2016, 2015], ordered=True)