将方块图应用于多个变量 [英] apply box plots to multiple variables
问题描述
set.seed(50)
data< - data.frame(年龄= c(rep(juv,10),rep(ad,10)),
sex = c(rep(m,10),rep(f,10)
size = c(rep(large,10),rep(small,10)),
length = rnorm(20),
width = rnorm(20),
年龄性别大小长宽高度
1 juv m大0.54966989 -0.34992735 0.10955641
2 juv m大-0.84160374 -0.58689714 -0.41341885
3 juv m large 0.03299794 -1.58987765 0.11179591
4 juv m large 0.52414971 1.68955955 -2.89232140
5 juv m large -1.72760411 0.56358364 0.09534935
6 juv m large -0.27786453 2.66763339 0.49988990
7 juv m large 0.36082844 0.35653495 0.94937215
8 juv m large -0.59091244 -0.36212039 -1.65840096
9 juv m large 0.97559055 0.56874633 -1.48161964
10 juv m large -1.44574 995 0.02867454 -0.49068623
11广告f小0.29520677 0.19902339 0.01475390
12广告f小0.55475223 -0.85142228 0.33763747
13广告f小-0.49863554 -1.13044947 -1.96590570
14广告f小0.19573384 0.59724896 -2.32077461
15广告f小-0.45554055 -1.09604786 0.99581082
16广告f小-0.36285547 0.01909655 1.16695158
17广告f小-0.15681338 0.41619898 -0.86517483
18广告f小 - 0.76525139 1.83967570 -1.39094651
19 ad f small -1.16601736 0.40618657 -1.33263085
20 ad f small -0.32342568 0.39322175 -0.13883976
通过调用一个函数,我想为每个长度
,宽度
和身高
对年龄
,性别
和尺寸
。所以输出结果应该是9个不同的地块(每个等级的9个地块应该包含2个盒子)。
exploreBoxplots< - function(dataframe,x.variables,y.variables){
library(plyr); library(ggplot2 )
x变量< - 列表(x.variables)
y变量< - 列表(y.variables)
llply(x变量,函数(x)ggplot(数据框,aes(x,y变量) ))+
geom_boxplot())
}
exploreBoxplots(data,c(data $ age,data $ sex,data $ size),c(data $ length,data $ width,data $ height))
...但是这会给出一个错误。如果你需要9个独立的地块,那么我的方法是首先将它作为变量存储起来
x变量< -names(data [,1:3])
y变量< ; -names(data [,4:6])
然后使用 expand.grid()
组合所有变量,然后将列转换为字符。
gg< -expand.grid(xVariables,yVariables)
gg< -data.frame(lapply(gg,as.character),stringsAsFactors = FALSE)
现在您可以使用 apply()
做出情节。重要的是使用 aes_string()
,因为x和y值将作为变量名提供。
apply(gg,1,function(x)ggplot(data,aes_string(x = x [1],y = x [2]))+ geom_boxplot())
I have this dataframe:
set.seed(50)
data <- data.frame(age=c(rep("juv", 10), rep("ad", 10)),
sex=c(rep("m", 10), rep("f", 10)),
size=c(rep("large", 10), rep("small", 10)),
length=rnorm(20),
width=rnorm(20),
height=rnorm(20))
age sex size length width height
1 juv m large 0.54966989 -0.34992735 0.10955641
2 juv m large -0.84160374 -0.58689714 -0.41341885
3 juv m large 0.03299794 -1.58987765 0.11179591
4 juv m large 0.52414971 1.68955955 -2.89232140
5 juv m large -1.72760411 0.56358364 0.09534935
6 juv m large -0.27786453 2.66763339 0.49988990
7 juv m large 0.36082844 0.35653495 0.94937215
8 juv m large -0.59091244 -0.36212039 -1.65840096
9 juv m large 0.97559055 0.56874633 -1.48161964
10 juv m large -1.44574995 0.02867454 -0.49068623
11 ad f small 0.29520677 0.19902339 0.01475390
12 ad f small 0.55475223 -0.85142228 0.33763747
13 ad f small -0.49863554 -1.13044947 -1.96590570
14 ad f small 0.19573384 0.59724896 -2.32077461
15 ad f small -0.45554055 -1.09604786 0.99581082
16 ad f small -0.36285547 0.01909655 1.16695158
17 ad f small -0.15681338 0.41619898 -0.86517483
18 ad f small -0.76525139 1.83967570 -1.39094651
19 ad f small -1.16601736 0.40618657 -1.33263085
20 ad f small -0.32342568 0.39322175 -0.13883976
By calling a function, I want to make boxplots of each of length
, width
and height
against each level of age
, sex
and size
. So the output should be 9 different plots (and each of the 9 plots should contain 2 'boxes' for each level.
I've tried this function:
exploreBoxplots <- function (dataframe, x.variables, y.variables) {
library(plyr); library(ggplot2)
xVariables <- list(x.variables)
yVariables <- list(y.variables)
llply(xVariables, function(x) ggplot(dataframe, aes(x, yVariables)) +
geom_boxplot())
}
exploreBoxplots(data,c(data$age, data$sex, data$size), c(data$length, data$width, data$height))
...but this gives an error. How can I get this function to make the 9 boxplots?
If you need 9 separate plots then my approach would be, first, store as variables names of x columns and y columns.
xVariables<-names(data[,1:3])
yVariables<-names(data[,4:6])
Then with expand.grid()
make combinations of all variables and then convert columns to characters.
gg<-expand.grid(xVariables,yVariables)
gg<-data.frame(lapply(gg, as.character), stringsAsFactors=FALSE)
Now you can use apply()
to make plots. Important is to use aes_string()
as x and y values will be supplied as variable names.
apply(gg,1,function(x) ggplot(data,aes_string(x=x[1],y=x[2]))+geom_boxplot())
这篇关于将方块图应用于多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!