W Pythonie próbuję odwrócić kolejność mojej osi X, tak że 2015 jest w lewo i 2019 r. Jest po prawej stronie. Celem jest wtedy 2019 na dole po tym, jak skoord_flip () fabuła. Zobacz Oto.

Próbowałem użyć skali_x_reverse, ale nie obsługuje dyskretnej osi. Jakieś inne sugestie?

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', '$50B', '$100B', '$1500B', '$2000B', '$250B', '$300B', '$350B'), 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))
0
mfwill 28 październik 2020, 17:54

1 odpowiedź

Najlepsza odpowiedź

Użyj kolumny kategorycznej i ustaw kolejność kategorii sposób, w jaki je chcesz.

df4['fiscal_year'] = pd.Categorical(df4['fiscal_year'], categories=[2019, 2018, 2017, 2016, 2015], ordered=True)
0
has2k1 29 październik 2020, 08:53