2017-08-22 3 views
1

私はこの単純なコードを持ち、図をプロットしようとしています。私の意図は、私が作ったもの、すなわちorder_numと同じ順序でx軸をプロットすることでした。1:10から10までです。しかし、ggplotは私の注文を変更しました。どのように私はデータフレームにそれらを置く元の順序を維持することができます。ggplotを使用するときに元の順序を保ちます

データを読み込み
data_order=data.frame(order_num=as.factor(c(rep(1:10),"10+")), 
    ratio=c(0.18223,0.1561,0.14177,0.1163,0.09646, 
    0.07518,0.05699,0.04,0.0345,0.02668,0.006725)) 

    ggplot(data_order,aes(x=order_num,y=ratio))+geom_bar(stat = 'identity') 

enter image description here

+1

[ggplot2棒グラフで注文バー](https://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph) – beetroot

答えて

1

(私たちは、次のステップでそれをやっているだろう、as.factorの除去に注目してください。これは必須ではありません。!)

data_order=data.frame(order_num=c(rep(1:10),"10+"), 
         ratio=c(0.18223,0.1561,0.14177,0.1163,0.09646, 
           0.07518,0.05699,0.04,0.0345,0.02668,0.006725)) 

あなたが必要ggplotの代わりにdataframeを操作します。

data_order$order_num <- factor(data_order$order_num, levels = data_order$order_num) 

レベルを変更すると、期待どおりになります。

ggplot(data_order,aes(x=order_num,y=ratio))+geom_bar(stat = 'identity') 

enter image description here

+0

の可能性のある重複確かに、このxラベルの順序を変更しました。しかし比率はそれに応じて変化しなかった。私のオリジナルの図からわかるように、x = 2のとき、それは2番目に高いはずです。 – yangyang

+0

あなたのコメントに基づいて、x_order_tot = factor(c(rep(1:10)、 "10 +")、levels = factor(c(rep(1:11))))data_order = data.frame (order_num = x_order_tot、ratio = c(0.18223,0.1561,0.14177,0.1163,0.09646,0.07518,0.05699,0.04,0.0345,0.02668,0.006725)) – yangyang

+0

これはソリューションに到達するのを助けてくれてうれしいです。 @yangyang最新の解決策を今すぐ確認してください。 – Prradep

関連する問題