2013-12-14 141 views
0

大家好,我正在嘗試使用矩陣和使用ggplot軟件包製作R中的圖形,因爲R和ggplot爲您提供了與圖形相關的元素的超棒質量。我有昏暗的20×20的矩陣,我試圖讓這樣的事情:如何在R中使用ggplot和矩陣製作堆疊柱狀圖

enter image description here

我不喜歡使用Excel由於其質量的圖形和因爲我喜歡[R但我不有足夠的關於ggplot的知識,我也不知道如何將矩陣中的最後一個值添加到矩陣的最後一列。也許你能幫我解決這個問題嗎?該dput版本我想情節矩陣是下一個:

structure(c(29965719.12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 28429960.41, 1458624.41, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26906565.89, 1440212.75, 
942099.599999998, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 25313701.12, 1401632.04, 927322.780000001, 1210951.2, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23748204.59, 1373259.46, 
909312.66, 1193709.88, 1151423.89, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 22257069.98, 1341041.19, 889758.559999999, 
1158986.03, 1136820.61, 1598355.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 20834214.97, 1305131.98, 856701.27, 1129474.09, 
1118606.69, 1578059.2, 1370856.78, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 19477094.16, 1263379.53, 837751.299999997, 1104522.4, 
1092284.99, 1551142.64, 1354974.07, 1420547.36, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 18165404.24, 1221357.6, 817402.140000001, 
1075676.58, 1071100.22, 1485230.97, 1318482.08, 1404038.55, 2048291.06, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17042526.26, 1183285.92, 799139.77, 
1051973.84, 1049700.79, 1444005.64, 1279183.6, 1384857.86, 2026632.72, 
3043637.6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15912055.89, 1150968.29, 
781767.969999999, 1027713, 1002821.26, 1416093.1, 1256348.38, 
1363220.55, 1995025.71, 3008201.12, 3669634.09, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 14837392.5, 1105477.16, 750258.4, 989174.439999999, 
979742.510000002, 1386614.86, 1231738.64, 1338049.47, 1960531.5, 
2930963.13, 3639935.05, 3641665.16, 0, 0, 0, 0, 0, 0, 0, 0, 13717721.37, 
1063604.11, 730910.129999999, 946145.08, 956427.08, 1356840.33, 
1188091.57, 1314698.33, 1919815.43, 2872918.27, 3587264.2, 3615734.97, 
6780879.37, 0, 0, 0, 0, 0, 0, 0, 12654080.83, 1034304.47, 689927.319999998, 
920539.020000001, 933540.619999999, 1312891.36, 1157533.56, 1273552.45, 
1883854.01, 2803699.4, 3508212.44, 3571759.54, 6728597.15, 6229245.09, 
0, 0, 0, 0, 0, 0, 11623390.05, 1007888.98, 671467.57, 875067.23, 
913815.460000001, 1222912.77, 1121345.7, 1218532.12, 1832366.96, 
2745773.97, 3399847.15, 3502858.93, 6604774.94, 6134253.45, 5553163.01, 
0, 0, 0, 0, 0, 10659885.1, 964108.52, 635246.380000001, 821454.51, 
885128.07, 1190048.79, 1090649.63, 1150376.15, 1783257.07, 2686616.54, 
3335146.8, 3406715.11, 6488674.75, 6014729.55, 5485803.42, 6372919.9, 
0, 0, 0, 0, 9735488.55, 938546.67, 607981.76, 786120.049999999, 
851119.98, 1162842.77, 1065054.88, 1073775.55, 1736231.45, 2614978.56, 
3271501.27, 3349167.83, 6368469.75, 5898741.78, 5418299.67, 6280598.84, 
6755054.64, 0, 0, 0, 8742383.48, 906474.289999999, 580510.6, 
755660.540000001, 829922.970000001, 1131575.38, 1039617.08, 1049134.57, 
1664696.67, 2552889.11, 3198760.39, 3264617.28, 6150581.39, 5627038.38, 
5316228.57, 6146363.09, 6677651.36, 7119008.27, 0, 0, 7882239.43, 
878026.84, 552812.210000001, 733031.09, 792261.039999999, 1097934.62, 
1014999.39, 1026214.95, 1630375.43, 2466505.47, 3145005.28, 3174913.27, 
6021366.2, 5462369, 5197168.91, 6012867.57, 6528649.55999999, 
7026048.51000001, 7585411.87, 0, 7116387.62, 850380.98, 499167.210000001, 
698711.68, 716107.529999999, 1070802.59, 981550.33, 977666.770000001, 
1595348.57, 2405959.16, 3084197.92, 3104095.11, 5928707.85, 5445385.54, 
5114034.19, 5886576.44000001, 6421200.57, 6749735.19, 7492647.16, 
7402737.51000001), .Dim = c(20L, 20L), .Dimnames = list(c("ene 10", 
"feb 10", "mar 10", "abr 10", "may 10", "jun 10", "jul 10", "ago 10", 
"sep 10", "oct 10", "nov 10", "dic 10", "ene 11", "feb 11", "mar 11", 
"abr 11", "may 11", "jun 11", "jul 11", "ago 11"), c("ene 10", 
"feb 10", "mar 10", "abr 10", "may 10", "jun 10", "jul 10", "ago 10", 
"sep 10", "oct 10", "nov 10", "dic 10", "ene 11", "feb 11", "mar 11", 
"abr 11", "may 11", "jun 11", "jul 11", "ago 11"))) 

感謝很多的幫助和時間。

回答

1

首先,需要將數據轉換爲長格式,即每行一個值。這是很容易與reshape2包:

library(reshape2) 
dat2 <- melt(dat) 

其中dat是你的矩陣的名稱。

默認情況下,R會按字母順序排列因子水平。下一個命令恢復原始順序:

dat2 <- transform(dat2, X1 = factor(X1, colnames(dat)), 
         X2 = factor(X2, rownames(dat))) 

計算中的dat的最後一列中的值的總和:

finalVal <- sum(dat[ , ncol(dat)]) 

現在,新的數據幀dat2可以繪製。功能annotate用於顯示最後一欄上方的文本。

library(ggplot2) 
ggplot(dat2, aes(x = X2, y = value, fill = X1)) + 
    geom_bar(stat = "identity") + 
    annotate("text", x = ncol(dat) - .5, y = finalVal + 2000000, 
      label = finalVal, size = 3) 

的參數xannotatey允許定義文本的位置。在上面的代碼中,我使用- .5+ 2000000來微調位置。

enter image description here

+0

感謝親愛的@SvenHohenstein和是否可以在圖的最後一列中添加矩陣中的最後一個數?在這種情況下,我的矩陣會有更多的行和列(例如80x80矩陣),圖例將包含所有標籤,或者我必須更改任何參數以顯示所有標籤。 – Duck

+1

@Duck查看我答案的更新。 –

+0

哦,謝謝親愛的@SvenHohenstein和最後一點問題是否可以在圖的最後一列中添加例如該列中所有值的總和,而不是該圖中該列的最終值。 – Duck

0

請使用d3.js庫交互graphs.it會給你的API做了許多種具有良好的使用定製acordng到您需要的圖形。