2016-09-12 77 views
-1

假設有以下數據:ggplot geom_bar繪製百分

    Journey_category Perc_25 Perc_50 Perc_75 Perc_90 Perc_95 
1   Petrol - Urban - 0 - 0,2 l 0,1103 0,5158 3,9672 15,304 23,20 
2 Petrol - ExtraUrban - 0 - 0,2 l 0,0737 0,3562 3,5155 14,098 21,07 
3  Petrol - HighWay - 0 - 0,2 l 40,5790 49,9807 117,5530 189,179 213,79 

我想繪製它使用ggplot爲代表百分堆疊列。

東西臨客這樣的:

enter image description here

任何提示嗎?

PD。我知道圖表中的值不匹配,只是草稿。

+0

的可能的複製[創建R中堆積%的barplot(http://stackoverflow.com/questions/9563368/create-stacked-percent-barplot- in-r) – Axeman

+0

謝謝,但我認爲情況並非如此。我有百分位值,這個問題基本上是用餅圖表示的,但是用百分比表示百分比,而不是對應於特定百分比的數量。 –

+1

那好吧,你能告訴我們你到目前爲止所嘗試過的,以及它出錯的地方嗎? – Axeman

回答

0

我找到了一種方法來做到這一點:

p <- ggplot(dat_quant, na.rm = T) 
p <- p + ggtitle("Idle hours after journey vs charging time needed") 
p <- p + labs(x = "Journey category", y = "Idle time after journey (h)") 
p <- p + geom_bar(stat = "identity", aes(x = Journey_category, y = Perc_95, fill = "Perc_95")) 
p <- p + geom_bar(stat = "identity", aes(x = Journey_category, y = Perc_90, fill = "Perc_90")) 
p <- p + geom_bar(stat = "identity", aes(x = Journey_category, y = Perc_75, fill = "Perc_75")) 
p <- p + geom_bar(stat = "identity", aes(x = Journey_category, y = Perc_50, fill = "Perc_50")) 
p <- p + geom_bar(stat = "identity", aes(x = Journey_category, y = Perc_25, fill = "Perc_25"))