如何在R中创建连续x轴的箱形图?

时间:2016-10-04 10:02:37

标签: r plot ggplot2 boxplot

我想在R中制作一个盒子图,其中x轴是连续的。我想将每一行(样本)视为一个单独的箱形图。所以不同年龄的样品不应该拼在一起!我该怎么做?

这是数据的一个子集:

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 个答案:

答案 0 :(得分:1)

如果我理解正确:
我将rownames转换为第一列

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

然后用reshape2

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

使用ggplot2

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

enter image description here