2014-11-01 180 views
0

我已閱讀了ggplot2文檔網站和其他問題,但找不到解決方案。我正在嘗試將不同年齡段的一些數據可視化。我有點努力去做,但它看起來並不像我想要的那樣。帶填充ggplot2的堆積條形圖

這裏是我的陰謀代碼

p <- ggplot(suggestion, aes(interaction(Age,variable), value, color = Age, fill = factor(variable), group = Age)) 
p + geom_bar(stat = "identity")+ 
    facet_grid(.~Age)![The facetting separates the age variables][1] 

我的最終目標是要建立一個堆棧條形圖,這就是爲什麼我使用的填充,但它並沒有把TDX值在相應年齡組和年份。 (有時TDX值== DX值,但我想,當他們沒有可視化) Trying to fill TDX values into DX values

這裏的dput(suggestion)

structure(list(Age = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L), .Label = c("0-2", "3-9", "10-19", "20-39", "40-59", "60-64", 
"65+", "UNSP", "(all)"), class = "factor"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("Year.10.DX", "Year.11.DX", 
"Year.12.DX", "Year.13.DX", "Year.10.TDX", "Year.11.TDX", "Year.12.TDX", 
"Year.13.TDX"), class = "factor"), value = c(26.8648932910636, 
30.487741796656, 31.9938838749782, 62.8189679326958, 72.8480838120064, 
69.3044125928752, 36.9789457527416, 21.808001825378, 24.1073451428435, 
40.3305134762935, 70.4486116545885, 68.8342676191755, 63.9227718107745, 
34.6086468618636, 8.84033719571875, 13.2807072303835, 28.4781516422802, 
55.139497471546, 59.7230544500003, 67.9448927372699, 37.7293286937066, 
6.9507024051526, 17.4393054963572, 33.1485743479821, 61.198647580693, 
58.6845873573852, 48.0073013177248, 28.4455801248562, 26.8648932910636, 
19.8044453272475, 23.0189084635948, 53.7037832071889, 60.6516550126422, 
58.1573725886767, 27.0791868812255, 21.808001825378, 19.8146296425633, 
35.0587750051557, 62.3308555053346, 59.3299998610862, 56.5341245769817, 
27.7229319271878, 8.84033719571875, 13.2807072303835, 22.4081606349585, 
48.0252683906252, 52.7560684009579, 65.2890977685045, 32.4142337849399, 
6.9507024051526, 15.2833655677215, 24.5268503180754, 52.536784326675, 
51.4100599515986, 40.9609231655724, 18.1306673637441)), row.names = c(NA, 
-56L), .Names = c("Age", "variable", "value"), class = "data.frame") 
+0

您可以創建堆疊barplots用'geom_bar(位置=「補」)',但我不知道你爲什麼會想你已經做了這裏 - 所有的分層/小面後,你會結束每個酒吧一個顏色和相當無意義的 – rawr 2014-11-01 22:09:38

+0

嘿@rawr感謝您的評論。是的,我認爲你是正確的位置=「填充」沒有超級有用 我想填補的價值,因爲它有助於可視化,如果你被診斷爲疾病 - DX和DX的那些#可能會得到治療 - TDX – user3900661 2014-11-01 22:15:28

回答

1

目前還不清楚你的需要,但或許這是什麼。

ggplot(a,aes(x=variable,y=value,fill=Age)) + geom_bar(stat='identity')  
+facet_wrap(~Age) 

enter image description here

如果要單獨可視化的TDX和DX條目,我們需要改變數據框一點。

> head(a) 
Age variable value 
1 0-2 Year.10.DX 26.86489 
2 3-9 Year.10.DX 30.48774 
3 10-19 Year.10.DX 31.99388 
4 20-39 Year.10.DX 62.81897 
5 40-59 Year.10.DX 72.84808 
6 60-64 Year.10.DX 69.30441 

感興趣variable列是TDX/DX價值的年度和組合。我們將使用tidyr包將其分成兩列。

library(tidyr) 
library(dplyr) 
tidy_a<- a %>% separate(variable, into = c('nothing',"year",'label'), sep = "\\.") 

這實際上分裂variablelevels分爲三個組成部分,因爲我們各執.和字符.中的每個條目中出現兩次。

> head(tidy_a) 
Age nothing year label value 
1 0-2 Year 10 DX 26.86489 
2 3-9 Year 10 DX 30.48774 
3 10-19 Year 10 DX 31.99388 
4 20-39 Year 10 DX 62.81897 
5 40-59 Year 10 DX 72.84808 
6 60-64 Year 10 DX 69.30441 

所以列nothing是相當無用的,使用separate和分離的.的只是一個必然結果。現在,這將允許我們分別可視化TDX/DX。

ggplot(tidy_a,aes(x=year,y=value,fill=label)) + geom_bar(stat='identity') + facet_wrap(~Age) 

enter image description here

+0

謝謝@keegan,你是第二個解決方案看起來很棒。你知道我們如何修改它,以便TDX值填入相應的DX值嗎? 「我試圖填補價值,因爲它有助於形象化,如果你被診斷出患有疾病 - DX和那些DX#有多少人得到治療 - TDX」 – user3900661 2014-11-01 22:17:26

+0

嗯,我不確定你在這裏是什麼意思。我沒有看到任何叫做TDX的東西,只有DX。看到水平($變量)返回只是 「Year.10.DX」「Year.11.DX」「Year.12.DX」「Year.13.DX」 – keegan 2014-11-02 13:44:54

+0

我顯然把錯誤的dput,希望你可以看看我的意思 – user3900661 2014-11-02 15:02:06