2017-09-22 40 views
0

我想要生成一個條形圖(使用ggplot2)的位置估計數據沿y軸和基本計數沿x軸計數。我的數據是結構化的,像這樣:ggplot2 - 按重量值排序圖

Data <- data.frame(locations = c("A","B","C","D"...), estimates = c(200, 300, 400, 200...) 

然後我用dplyr據測算

library(dplyr) 
    Data <- Data %>% arrange(estimates) 

於是我跑我的GGPLOT2代碼

library(ggplot2) 
    ggplot(Data, aes(locations, weight = estimates))+ 
     geom_bar()+ 
     coord_flip() 

安排我的數據,但所產生的陰謀就是這樣,這些酒吧並沒有按照估計排序。

enter image description here

回答

1

有使用dplyr沒有意義的。所有你需要做的就是訂購estimates,提取相應locations並將它傳遞給scale_x_discrete,例如:scale_x_discrete(limits = Data$locations[order(Data$estimates)])

library(ggplot2) 

ggplot(Data, aes(locations, weight = estimates))+ 
     geom_bar()+ 
     coord_flip() + 
     scale_x_discrete(limits = Data$locations[order(Data$estimates)]) 

enter image description here