Roland - 1 year ago 86
R Question

# How I can make multiple pairs of boxplot in R?

I have a dataset with almost 4,000 observations, that contain 9 different groups. So I have the following variables

Group: 1,2,3,....,9

Sex: Male, Female

Weight: weight of each individual

What I want to do is make pairs of boxplot (Male, Female) for each group. So I will have 18 boxplots in this case.

How I can do it without make one subsetting data for each boxplot (

`subset()`
or
`which()`
) functions.

Besides that I have a litle problem with this data, there are some observations without weight, the cells are empty or with
`.`
a dot.

Here is a fictitious sample with 3 groups, where sex =1 mean woman and 2 male.

``````Group     Sex   Weight
1         1     140
1         2
1         2     160
1         1     154
1         1     127
2         2     182
2         2     192
2         1     .
2         1     147
2         1     129
3         1     124
3         2     182
3         1     .
3         2     141
3         1     148
``````

I never used this
`dput()`
function I don't know if it's right

``````dput(data)
structure(list(Group = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L), Sex = c(1L, 2L, 2L, 1L, 1L, 2L, 2L,
1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L), Weight = structure(c(6L, 1L,
11L, 10L, 4L, 12L, 13L, 2L, 8L, 5L, 3L, 12L, 2L, 7L, 9L), .Label = c("",
".", "124", "127", "129", "140", "141", "147", "148", "154",
"160", "182", "192"), class = "factor")), .Names = c("Group",
"Sex", "Weight"), class = "data.frame", row.names = c(NA, -15L
))
``````

With correct class (numeric,factor) assignment to data columns you could do:

``````library(ggplot2)

DF = structure(list(Group = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L), Sex = c(1L, 2L, 2L, 1L, 1L, 2L, 2L,
1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L), Weight = structure(c(6L, 1L,
11L, 10L, 4L, 12L, 13L, 2L, 8L, 5L, 3L, 12L, 2L, 7L, 9L), .Label = c("",
".", "124", "127", "129", "140", "141", "147", "148", "154",
"160", "182", "192"), class = "factor")), .Names = c("Group",
"Sex", "Weight"), class = "data.frame", row.names = c(NA, -15L
))

str(DF)
#'data.frame':  15 obs. of  3 variables:
# \$ Group : int  1 1 1 1 1 2 2 2 2 2 ...
# \$ Sex   : int  1 2 2 1 1 2 2 1 1 1 ...
# \$ Weight: Factor w/ 13 levels "",".","124","127",..: 6 1 11 10 4 12 13 2 8 5 ...

#The Weight column is currently of class factor that needs to converted to
#class numeric by first converting to character class and replacing "." by ""

DF\$Weight = as.numeric(gsub("[.]","",as.character(DF\$Weight)))

#Sex variable should be converted to factor,
#If 1 is considered as FeMale and 2 as Male

DF\$Sex = ifelse(DF\$Sex==1,"FeMale","Male")

DF\$Sex <- as.factor(DF\$Sex)

gg <- ggplot(DF, aes(x=Sex, y=Weight)) +
geom_boxplot() + facet_wrap(~Group) + ggtitle("Weight vs Sex for various Groups") +
theme(plot.title = element_text(size=14, face="bold"))
gg
``````

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download