如何在 ggplot2 中制作箱线图,为数据框/矩阵中的每一列制作一个框?

发布于 2025-01-11 17:31:04 字数 257 浏览 0 评论 0原文

本质上,我想制作一个包含 5 个箱线图的图。每个框代表每列的 Min/1stQ/Mean/3rdQ/Max,特别是在使用 geom_boxplot() 的 ggplot2 库中。

我的困惑在于 geom_boxplot() 想要将变量作为 xy 参数,但这不是我想要做的。

我希望 X 轴代表列,Y 轴代表每个列的 Min/1stQ/Mean/3rdQ/Max。

Essentially, I want to make one plot that has 5 boxplots in it. Each box representing the Min/1stQ/Mean/3rdQ/Max of each column, specifically in the ggplot2 library using geom_boxplot().

My confusion lies in the fact that geom_boxplot() wants to take variables as x and y parameters, but that's not what I want to do.

I want the X-axis to represent the columns, and the Y-axis to represent the Min/1stQ/Mean/3rdQ/Max of each of them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必在意 2025-01-18 17:31:04

由于您没有提供任何可重现的示例,我假设您有一个包含五列的数据框,如下所示:

DF <- mtcars[,c(1,2,5,6,7)]
head(DF)
#                  mpg cyl drat    wt  qsec
#Mazda RX4         21.0   6 3.90 2.620 16.46
#Mazda RX4 Wag     21.0   6 3.90 2.875 17.02
#Datsun 710        22.8   4 3.85 2.320 18.61
#Hornet 4 Drive    21.4   6 3.08 3.215 19.44
#Hornet Sportabout 18.7   8 3.15 3.440 17.02
#Valiant           18.1   6 2.76 3.460 20.22

您想要制作一个包含 5 个箱线图的图,每个箱线图代表每一列。您可能想得到这个。

boxplot(DF, xlab = "column_names", ylab = "column_values", las = 1)
grid()

输入图片这里的描述

但是,您希望使用 ggplot 获得类似的绘图。如果是这样,您需要转换 DF,以便 DF 的所有列名称都存储在单个列中,以便可以将其分配给 x< /code>,ggplot 需要的。同样,所有相应的值应存储在另一列中,以便可以将其分配给 ggplot 所需的 y 。您可以使用pivot_longer来完成此转换。

library(tidyverse)
newDF <- DF %>% 
         pivot_longer(col= everything(), 
                      values_to = "column_values", 
                      names_to = "column_names")

newDF
# A tibble: 160 x 2
#   column_names column_values
#   <chr>                <dbl>
# 1 mpg                  21   
# 2 cyl                   6   
# 3 drat                  3.9 
# 4 wt                    2.62
# 5 qsec                 16.5 
# 6 mpg                  21   
# 7 cyl                   6   
# 8 drat                  3.9 
# 9 wt                    2.88
#10 qsec                 17.0 
# ... with 150 more rows

现在可以从 newDF 绘制 5 个箱线图:

newDF %>% ggplot(aes(x = column_names, y = column_values)) +
 geom_boxplot(fill = "grey") + theme_bw()

这是结果图:

在此处输入图像描述

Since you do not provide any reproducible example, I assume that you have a data frame with five columns like this:

DF <- mtcars[,c(1,2,5,6,7)]
head(DF)
#                  mpg cyl drat    wt  qsec
#Mazda RX4         21.0   6 3.90 2.620 16.46
#Mazda RX4 Wag     21.0   6 3.90 2.875 17.02
#Datsun 710        22.8   4 3.85 2.320 18.61
#Hornet 4 Drive    21.4   6 3.08 3.215 19.44
#Hornet Sportabout 18.7   8 3.15 3.440 17.02
#Valiant           18.1   6 2.76 3.460 20.22

You want to make one plot that has 5 boxplots, each of which represents each column. It is likely that you want to get this.

boxplot(DF, xlab = "column_names", ylab = "column_values", las = 1)
grid()

enter image description here

However, you want to get a similar plot by using ggplot. If that's true, you need to transform DF so that all of the column names of DF are stored in a single column so that it can be assigned to x, which ggplot requires. Likewise, all the corresponding values should be stored in another column so that it can be assigned to y, which ggplot requires. You can do this transformation by using pivot_longer.

library(tidyverse)
newDF <- DF %>% 
         pivot_longer(col= everything(), 
                      values_to = "column_values", 
                      names_to = "column_names")

newDF
# A tibble: 160 x 2
#   column_names column_values
#   <chr>                <dbl>
# 1 mpg                  21   
# 2 cyl                   6   
# 3 drat                  3.9 
# 4 wt                    2.62
# 5 qsec                 16.5 
# 6 mpg                  21   
# 7 cyl                   6   
# 8 drat                  3.9 
# 9 wt                    2.88
#10 qsec                 17.0 
# ... with 150 more rows

It is now possible to plot 5 boxplots from newDF:

newDF %>% ggplot(aes(x = column_names, y = column_values)) +
 geom_boxplot(fill = "grey") + theme_bw()

Here is the resulted plot:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文