箱形图的因素有R

箱形图的因素有R

问题描述:

鉴于数据箱形图的因素有R

Step  A  B  C  D  E  F  G  I  J 
1 1 0.158 0.011 0.099 6.504 5.914 0.000 0.100 0.330 0.000 
2 2 0.345 0.016 0.102 6.050 5.285 0.000 0.102 0.316 0.001 
3 1 0.324 0.015 0.100 7.146 6.426 0.000 0.101 0.293 0.000 
4 2 0.264 0.015 0.099 5.864 5.202 0.000 0.101 0.296 0.000 
5 1 0.346 0.022 0.101 5.889 5.027 0.000 0.101 0.411 0.000 
6 2 0.397 0.022 0.130 6.061 5.311 0.000 0.131 0.220 0.000 
7 1 0.337 0.015 0.048 7.417 6.839 0.000 0.110 0.129 0.000 
8 2 0.362 0.016 0.143 5.726 4.951 0.001 0.144 0.268 0.000 
9 1 0.178 0.011 0.099 5.831 5.290 0.000 0.100 0.261 0.000 

d < - read.table('sample.txt', header=T)给我一个数据帧,boxplot(d$A ~ d$Step)产生一个合理的图形,但我似乎无法得到在同一图中的所有情节。像boxplot(d ~ d$Step)事情是我所期待的工作,但我得到以下错误:

Error in model.frame.default(formula = d ~ d$Step) : 
    invalid type (list) for variable 'd' 

我试着制作步骤的一个因素d$Step <- as.factor(d$Step),但似乎没有任何效果。

我们可以tidyverse

library(tidyverse) 
gather(d, Var, Val, -Step) %>% 
     mutate(Step=factor(Step)) %>% 
     ggplot(., aes(x=Var, y = Val, fill=Step)) + 
      geom_boxplot() + 
      scale_fill_manual(values = c("red", "blue")) 

enter image description here

+0

不能用bas来完成e R图形?我会研究'tidyverse' –

+0

@RobPaisley ggplot更容易 – akrun

做到这一点的方法是在每个自己的规模基础R绘制这些,像这样

par(mfrow=c(3,3)) 
for(i in 2:10) { 
    boxplot(d[,i] ~ d$Step, main=names(d)[i]) } 

enter image description here