2013-12-07 57 views
-3

您好我有這樣設置需要繪製一個條形圖(以百分方式)GGPLOT2

ALL Critical Error Warning Review 

2016 1412  475 4  125 
154 45  49 2  58 
116 86  12 1  17 

我想用GGPLOT2繪製一個堆積條形圖,其中單個條將顯示100%的數據根據他們在「全部」中的貢獻,「全部」和休息「關鍵」,「錯誤」,「警告」,「評論」應該在另一個之上。

我嘗試它沒有運氣!需要一個手..感謝

回答

1

我不是很確定,如果您的描述所需的情節是不含糊。

我的解釋是以下幾點:

## Copied from user1317221_G - Thanks for that. 
babydf <- structure(list(ALL = c(2016L, 154L, 116L), Critical = c(1412L, 
45L, 86L), Error = c(475L, 49L, 12L), Warning = c(4L, 2L, 1L), 
    Review = c(125L, 58L, 17L)), .Names = c("ALL", "Critical", 
"Error", "Warning", "Review"), class = "data.frame", row.names = c(NA, 
-3L)) 

# Add IDs 
babydf <- cbind(id=1:nrow(babydf), babydf)) 

library(reshape2) 
library(ggplot2) 

# reshape the dataframe: 
df.reshaped <- melt(babydf, id.vars='id') 

ggplot(subset(df.reshaped, variable != 'ALL'), aes(x=id, y=value, fill=variable)) + geom_bar(stat='identity') 

enter image description here

如果你想有相同高度的所有酒吧,只是做

babydf[, 3:6] <- babydf[, 3:6]/babydf$ALL * 100 

melt之前。結果: enter image description here

+0

謝謝..它的工作有所延伸。但我想在Y軸的百分比而不是計數。謝謝 –

+0

嗯,我的第二個圖上的位置究竟在哪裏?我一直認爲所有100的酒吧應該是百分之百。 – Thilo

+0

對不起,我沒有注意到..非常感謝你。一個更多的幫助將不勝感激。實際上實際的數據集是這樣的。[https://dl.dropboxusercontent.com/u/93667882/issue_data.csv。我想爲每個BU明智的國家明智和司明智。我有問題需要prepossessing數據以適應ggplot2需求。幫助,將不勝感激。@ Thilo –

相關問題