如何使用多个列和参数“split”创建一个框图 [英] How to create one box plot using multiple columns and argument "split"
问题描述
我需要使用三个数字列从data.frame创建一个框图,并使用分割参数将 paint 。我有一个大的data.frame,但我需要的是在下面的例子中:
paint< -c(blue ,black,red,blue,black,red,blue,black,red)
car1 <-c(100,138,123,143,
car2 <-c(111,238,323,541,328,363,411,238,313)
car3 <-c(432,123, 322,342,323,522,334,311,452)
data< -data.frame(paint,car1,car2,car3)
> data
paint car1 car2 car3
1蓝色100 111 432
2黑色138 238 123
3红色123 323 322
4蓝色143 541 342
5黑色112 328 323
6红色144 363 522
7蓝色343 411 334
8黑色112 238 311
9红色334 313 452
当我仅使用一列时,以下功能起作用:
boxplot(split(data [,2],data [,1]))
但是当我尝试使用三列创建一个boxplot 功能不起作用:
boxplot(split(data [,2:4],data [,1]))
数据变成长格式,然后绘制:
temp = reshape(data,direction =long,varying = 2:4 ,sep =)
boxplot(split(temp [,3],temp [,1]))
#boxplot(car〜paint,data = temp)###公式符号,阅读
或使用格子
:
library(lattice)
bwplot(car1 + car2 + car3〜paint,data = data)
我不知道为什么这个符号不能与base R的 boxplot
/ p>
更新
如果我错误地解释了你的原始问题(在阅读罗马的评论之后),这里是一个选项(再次使用 lattice
),它将boxplots carc code> car1 , car2
和 car3
侧。这使用第一个例子中创建的长格式数据 temp
:
bwplot(car〜paint | paste0(Car,time),data = temp)
img src =https://i.stack.imgur.com/0KAvP.pngalt =enter image description here>
I need create a box plot from a data.frame with three numeric columns, and use the argument split to separate the boxes by paint. I have a large data.frame, but what I need is in the example below:
paint<-c("blue", "black", "red", "blue", "black", "red", "blue", "black", "red")
car1<-c(100, 138, 123, 143, 112, 144, 343, 112, 334)
car2<-c(111, 238, 323, 541, 328, 363, 411, 238, 313)
car3<-c(432, 123, 322, 342, 323, 522, 334, 311, 452)
data<-data.frame(paint, car1, car2, car3)
>data
paint car1 car2 car3
1 blue 100 111 432
2 black 138 238 123
3 red 123 323 322
4 blue 143 541 342
5 black 112 328 323
6 red 144 363 522
7 blue 343 411 334
8 black 112 238 311
9 red 334 313 452
When I used only one column the following function works:
boxplot(split(data[,2], data[,1]))
But when I try to create a boxplot using three columns the function does not work:
boxplot(split(data[,2:4], data[,1]))
Thanks for the help and sorry for poor English.
Try putting your data into long form first and then plotting:
temp = reshape(data, direction="long", varying=2:4, sep="")
boxplot(split(temp[,3], temp[,1]))
# boxplot(car ~ paint, data=temp) ### Formula notation, easier to read
Or, use lattice
:
library(lattice)
bwplot(car1 + car2 + car3 ~ paint, data=data)
I'm not sure why that notation doesn't work with base R's boxplot
though.
Update
In case I misinterpreted your original question (after reading Roman's comment), here's an option (again using lattice
) that puts boxplots for car1
, car2
, and car3
separately but side-by-side. This uses the long-form data temp
created in the first example:
bwplot(car ~ paint | paste0("Car ", time), data = temp)
这篇关于如何使用多个列和参数“split”创建一个框图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!