2016-02-29 75 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