如何在 R 中用数据帧的 3 列绘制三组箱线图?
How to do a three group boxplot in R with 3 columns of a dataframe?
我正在计算 3 个组的一些统计数据 - 所有组、男性组和女性组。我已将它们存储到一个名为 stats_df 的数据框中,每个组都作为一列 header,并将统计数据作为行数据。我需要做一个箱线图(我最近的尝试包含在代码中),将所有 3 个组都表示为框,但我似乎无法弄清楚,也没有在线教程有帮助。
all_stats <- c(all_mean, all_median, all_mode, all_25, all_50, all_75)
female_stats <- c(female_mean, female_median, female_mode, female_25, female_50, female_75)
male_stats <- c(male_mean, male_median, male_mode, male_25, male_50, male_75)
stats_df <- data.frame(all_stats, female_stats, male_stats)
boxplot(all_stats ~ male_stats,
data=stats_df,
main="Stats Boxplot",
xlab="Group",
ylab="Number")
您可以使用以下代码:
stats_df <- data.frame(all_stats = c(35.19, 32, 29, 26, 32, 50),
female_stats = c(36.23, 32, 32, 24, 32, 52),
male_stats = c(33.5, 32, 29, 28.5, 32, 39.5))
library(tidyverse)
stats_df %>%
ggplot() +
geom_boxplot(aes(x = "all_stats", y = all_stats)) +
geom_boxplot(aes(x = "female_stats", y = female_stats)) +
geom_boxplot(aes(x = "male_stats", y = male_stats)) +
xlab("Group") +
ylab("Number") +
ggtitle("Stats Boxplot")
输出:
我正在计算 3 个组的一些统计数据 - 所有组、男性组和女性组。我已将它们存储到一个名为 stats_df 的数据框中,每个组都作为一列 header,并将统计数据作为行数据。我需要做一个箱线图(我最近的尝试包含在代码中),将所有 3 个组都表示为框,但我似乎无法弄清楚,也没有在线教程有帮助。
all_stats <- c(all_mean, all_median, all_mode, all_25, all_50, all_75)
female_stats <- c(female_mean, female_median, female_mode, female_25, female_50, female_75)
male_stats <- c(male_mean, male_median, male_mode, male_25, male_50, male_75)
stats_df <- data.frame(all_stats, female_stats, male_stats)
boxplot(all_stats ~ male_stats,
data=stats_df,
main="Stats Boxplot",
xlab="Group",
ylab="Number")
您可以使用以下代码:
stats_df <- data.frame(all_stats = c(35.19, 32, 29, 26, 32, 50),
female_stats = c(36.23, 32, 32, 24, 32, 52),
male_stats = c(33.5, 32, 29, 28.5, 32, 39.5))
library(tidyverse)
stats_df %>%
ggplot() +
geom_boxplot(aes(x = "all_stats", y = all_stats)) +
geom_boxplot(aes(x = "female_stats", y = female_stats)) +
geom_boxplot(aes(x = "male_stats", y = male_stats)) +
xlab("Group") +
ylab("Number") +
ggtitle("Stats Boxplot")
输出: