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

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

最佳答案

如果我理解正确的话:
我将行名变成第一列

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

关于r - 如何在 R 中创建具有连续 x 轴的箱线图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39849459/

相关文章:

r - ggplot 标签为 "K"表示数千或 "M"表示数百万(维护 'comma' y 轴标签)

linux - 限制Linux下R中的内存使用

r - 绘图区域外的ggplot2 annotation_ticks

r - 调整ggplot对象中的图例标题位置,R

r - 结合 ggplot 和 plotrix

ggplot2 - ggplot2 的命名空间加载失败,网格的描述失败

r - 过滤属于预定义圆值的数据

r - ggplot2 scale_fill_gradient 不一致地消失

r - 向 scatter3d 图添加图例

opengl - OpenGL 绘图库