我如何在 R 中制作多对箱线图?
How I can make multiple pairs of boxplot in R?
我有一个包含近 4,000 个观测值的数据集,其中包含 9 个不同的组。所以我有以下变量
组: 1,2,3,....,9
性别:男,女
体重:每个人的体重
我想做的是为每个组制作成对的箱线图(男性、女性)。所以在这种情况下我将有 18 个箱线图。
如何在不为每个箱线图(subset()
或 which()
)函数制作一个子集数据的情况下做到这一点。
除了我对这些数据有一点问题外,还有一些观察结果没有权重,单元格为空或带有 .
个点。
这是一个包含 3 组的虚构样本,其中 sex =1 表示女性和 2 名男性。
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
没用过这个dput()
功能不知道对不对
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
))
通过对数据列的正确 class(数字、因子)分配,您可以执行以下操作:
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
我有一个包含近 4,000 个观测值的数据集,其中包含 9 个不同的组。所以我有以下变量
组: 1,2,3,....,9
性别:男,女
体重:每个人的体重
我想做的是为每个组制作成对的箱线图(男性、女性)。所以在这种情况下我将有 18 个箱线图。
如何在不为每个箱线图(subset()
或 which()
)函数制作一个子集数据的情况下做到这一点。
除了我对这些数据有一点问题外,还有一些观察结果没有权重,单元格为空或带有 .
个点。
这是一个包含 3 组的虚构样本,其中 sex =1 表示女性和 2 名男性。
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
没用过这个dput()
功能不知道对不对
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
))
通过对数据列的正确 class(数字、因子)分配,您可以执行以下操作:
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