简体   繁体   English

如何在 R 中创建具有连续 x 轴的箱线图?

[英]How to Create Boxplots with a Continuous x axis in R?

I would like to make a boxplot in R in which the x axis is continuous.我想在 R 中制作一个箱线图,其中 x 轴是连续的。 And I want to treat each row (sample) as a separate box plot.我想将每一行(样本)视为一个单独的箱线图。 So samples with a different age should not be binned together!所以不同年龄的样本不应该合并在一起! How do I do this?我该怎么做呢?

This a subset of the data:这是数据的子集:

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")

If I understood correctly:如果我理解正确:
I turn the rownames into the first column我把行名变成第一列

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

Then melt it with reshape2然后用reshape2融化它

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

And plot it using ggplot2并使用ggplot2绘制它

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM