R: 對已處理數據繪製按比例分佈的等高柱狀圖

等高分佈式柱狀圖

R代碼以下:segmentfault

#輸入數據
s1=c(1:3)
s2=c(2:4)
s3=c(3:5)
#創建數據集
d=data.frame(s1,s2,s3,row.names=c('miffy','kitty','tommy'))
#轉換數據集結構
library(reshape2)
df=melt(as.matrix(d))
#使用ggplot2繪圖
library(ggplot2)
ggplot(df,aes(Var2,value,fill=Var1))+geom_bar(stat="identity",position="fill")

特別注意數據集ddf的區別,在命令行下鍵入名稱後回車查看:分佈式

>d
      s1 s2 s3
miffy  1  2  3
kitty  2  3  4
tommy  3  4  5
>df
   Var1 Var2 value
1 miffy   s1     1
2 kitty   s1     2
3 tommy   s1     3
4 miffy   s2     2
5 kitty   s2     3
6 tommy   s2     4
7 miffy   s3     3
8 kitty   s3     4
9 tommy   s3     5

特別感謝YGC:《使用ggplot2畫圖》,我只是代碼的搬運工。ide

相關文章
相關標籤/搜索