2017-03-05 11 views
0

ggplot geom_barグラフのデータをx軸で整理しようとしています(Crude.Rate =疾患あたりの死亡数)、しかし、米国の状態であるy軸はアルファベット順にデータを保持しています。私はx軸の数値データの要素を作成しようとしましたが、グラフはアルファベット順にとどまり、x軸のラベルはちょうどy軸に合わせて移動しました。x軸の数値データでy軸のアルファベット順ではなく、ggplot barchartと注文してください

コード:

stDis10 <- cdcData %>% 
    filter(Year == "2010", ICD.Chapter == "Heart Attack") %>% 
    arrange(desc(Crude.Rate)) %>% 
    select(State, ICD.Chapter, Crude.Rate) 

stDis10$Crude.Rate <- factor(stDis10$Crude.Rate, levels = stDis10$Crude.Rate) 

ggplot(stDis10, aes(y = Crude.Rate, x = State)) + geom_bar(stat='identity') + coord_flip() 

答えて

0

あなたが要因にCrude.Rateを変更する必要はありません。あなたはCrude.Rateによって国家を並べ替えることができます。

ggplot(stDis10, aes(y = Crude.Rate, x = reorder(State, Crude.Rate))) + geom_col() + coord_flip() 

(ggplot2の最新バージョンでは、geom_col()ではなくgeom_bar(stat = "identity")を使用することができます。)

関連する問題