2016-02-29 86 views
0

排名我一直在試圖顯示此作爲一個排名,使得翻轉,在光柱我有以下的數據幀如何顯示使用ggplot

Country<-c("Chile_T", "Canada_T", "El Salvador_N", "Finland_N", "Germany_N", "Germany_T") 
Loss<-c(1.14e-06, 6.14e-07, 8.93e-09, 8.93e-09, 1.05e-10, 1.25e-11) 
df<- data.frame(Country, Loss) 

。到目前爲止這麼好,但我的酒吧總是訂購Country,我希望他們訂購Loss

我已經嘗試了一些東西,等等,這樣的:

df <- within(df, Loss <- factor(Loss,levels=names(sort(table(Loss), decreasing=TRUE)))) 

ggplot(data=df, aes(x=Country, y=Loss))+ 
    geom_bar(stat = "identity", width=0.95, fill="black")+ 
    labs(x = "", y = "")+ 
    coord_flip()+ 
    scale_y_discrete(breaks=NULL)+ 
    theme (legend.position="none", 
     axis.text.x = element_blank(), 
     axis.text.y = element_text(size=17), axis.title.y=element_text(size=17)) 

請,可有人點我在正確的方向?提前致謝!!!

回答

1

你可以使用reorder

Country<-c("Chile_T", "Canada_T", "El Salvador_N", "Finland_N", "Germany_N", "Germany_T") 
Loss<-c(1.14e-06, 6.14e-07, 8.93e-09, 8.93e-09, 1.05e-10, 1.25e-11) 
df<- data.frame(Country, Loss) 
library(ggplot2) 
ggplot(data=df, aes(x=reorder(Country, Loss), y=Loss))+ 
    geom_bar(stat = "identity", width=0.95, fill="black") + 
    coord_flip() 
+0

這一個工作正常!謝謝! – samyandi

+0

也許你也可以幫我解決由此產生的問題......我的德國價值觀非常小,酒吧是不存在的......有沒有一種方法可以巧妙地轉換軸,使所有酒吧都可見?我試過scale_y_log10(),但是奇怪地「翻轉」圖表... – samyandi

+0

它翻轉圖表,因爲你的值的日誌是負值。你嘗試過'scale_y_sqrt()'嗎?或者也許事先讓你的小損失數字更大。 – lukeA