2017-09-01 132 views
0

我有一個下面的數據框,我試圖繪製條形圖。如何使用ggplot2在條形圖中排列y軸

country <- c('AUD','USD','GBP','ROW','EUR') 
count <- c(58, 28, 8, 4, 2) 
data <- data.frame(country, count) 

    ggplot(data = data , aes(x = 'COUNTRY', y = reorder(count, -count), fill = country))+ 
    geom_bar(stat = "identity")+ 
    xlab("COUNTRY")+ 
    ylab("TOTAL")+ 
    theme_minimal()+ 
    geom_text(aes(label = country), vjust = -0.5, size = 3)+ 
    scale_fill_brewer(palette="Paired")+ 
    theme(legend.position = "bottom", 
     legend.title = element_blank()) 

該代碼生成的圖形沒有軸和點標籤的順序。 它生成以下圖。

enter image description here

我需要幫助重新安排這個軸和計數標籤。

+0

什麼是「TOTAL」?這不是'data' – PoGibas

+0

這是我給出的整個單欄的字符標籤 – Prafulla

+0

爲什麼你把'TOTAL'放在X上,但是把Y命名爲'TOTAL'?很難理解你要做什麼 – PoGibas

回答

1

我不太清楚你想要輸出的樣子。這樣的事情會好嗎?

ggplot(data = data , aes(x = 'COUNTRY', y = count, 
         fill = reorder(country, count)))+ 
    geom_bar(stat = "identity")+ 
    xlab("COUNTRY")+ 
    ylab("TOTAL")+ 
    theme_minimal()+ 
    geom_text(aes(label = sprintf("%s (%d)", country, count), 
       y = cumsum(count) - 0.5*count), size = 3)+ 
    scale_fill_brewer(palette="Paired")+ 
    theme(legend.position = "bottom", 
     legend.title = element_blank()) 
+0

我也不清楚OP想要什麼,但這可能是您的解決方案。你只需要翻轉Y軸(從最低到最高)。 – PoGibas