2016-10-04 64 views
1

我想在R軸上做一個x軸連續的箱形圖。我想把每一行(樣本)作爲一個單獨的盒子圖。所以不同年齡的樣本不應該放在一起!我該怎麼做呢?如何在R中創建帶有連續x軸的BoxLots?

這數據的子集:

df <- structure(list(X1 = c(67.0785968921204, 45.5968692796599, 36.9528452430474, 
59.0160838607585, 50.0432724395945, 44.3381091105162, 57.9705240678336, 
52.5298724133533, 62.0004216014734, 54.1111551461596), X2 = c(66.4508598009841, 
46.9692156851792, 37.1419457255043, 60.0582991817961, 50.7717368681294, 
44.6962314013419, 57.5490276313784, 52.6394305113891, 62.9297233041122, 
56.8151766642767), X3 = c(66.4517425831512, 46.2946539468733, 
36.5946733567535, 59.2477934854157, 49.1558840130484, 44.7507905380111, 
59.1132983272444, 53.710627728232, 61.7923277906642, 57.5999862897015 
), X4 = c(66.1516449763315, 45.4660590159847, 37.2239262718906, 
59.2975530712561, 50.2546321578291, 44.7220452966667, 59.8879656465763, 
52.321734919241, 62.0802304973764, 56.6507005349853), X5 = c(66.1810558292955, 
46.3301985628267, 36.4487743101244, 60.054119632362, 49.1593136549535, 
44.5708909518076, 58.5865142665164, 52.5527273219855, 61.3749185309236, 
54.1823379401272), X6 = c(65.9530929286517, 45.5120010675769, 
36.7924160587984, 58.9428613519645, 50.3412809263164, 44.9671678827697, 
57.8718260182012, 51.8954544252633, 62.0173019998447, 56.3833840769146 
), X7 = c(66.3862581408135, 46.5872469340431, 36.7585555977493, 
58.1374309578563, 50.3399735165261, 44.5739565876491, 57.5245695195136, 
52.7613488669329, 61.2500297922529, 55.9202360622414), X8 = c(67.5577910713347, 
46.1891742544371, 36.4689522649804, 59.5271358261971, 49.6776114214636, 
44.1995317742719, 58.4881363877987, 52.1946266979144, 62.1149998459759, 
55.3748655464147), X9 = c(66.3943390258844, 45.843835703738, 
37.3485122393333, 59.8591304277037, 49.387883195468, 44.4283817056918, 
58.1874530826789, 53.5091378916001, 62.1187451212786, 55.3632760142297 
), X10 = c(66.9748072219828, 46.20735965374, 36.7069272963502, 
59.5035069226904, 48.8446530329762, 44.8753686249692, 58.0223695284058, 
53.2732448917674, 61.2509571513, 54.9615424261546), age = c(55, 
37, 31, 59, 49, 47, 69, 68, 69, 66)), .Names = c("X1", "X2", 
"X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10", "age"), row.names = c("GSM1051533_7800246024_R03C02", 
"GSM1051534_7800246024_R04C02", "GSM1051535_7800246024_R05C02", 
"GSM1051536_7800246024_R06C02", "GSM1051537_7800246085_R01C01", 
"GSM1051538_7800246085_R02C01", "GSM1051539_7800246085_R03C01", 
"GSM1051540_7800246085_R04C01", "GSM1051541_7800246085_R05C01", 
"GSM1051542_7800246085_R06C01"), class = "data.frame") 

回答

1

如果我正確理解:
我打開rownames送入第一塔

library(data.table) 
setDT(df, keep.rownames = TRUE)[] 

然後用reshape2

library(reshape2) 
df=melt(df,id.vars=c("rn","age")) 
熔化

並使用進行繪圖

library(ggplot2) 
ggplot(df,aes(x=age,y=value,group=rn))+geom_boxplot() 

enter image description here

+0

真棒,謝謝! – Ansjovis86

+0

問題!爲什麼有些箱子比其他箱子寬? – Ansjovis86

+0

它們應該全部具有相同的寬度,但是對於共享相同年齡的兩個樣本(69),它們以寬度減小的方式彼此相鄰顯示。 – Haboryme