如何在 ggplot2 中制作箱线图,为数据框/矩阵中的每一列制作一个框?
本质上,我想制作一个包含 5 个箱线图的图。每个框代表每列的 Min/1stQ/Mean/3rdQ/Max,特别是在使用 geom_boxplot()
的 ggplot2 库中。
我的困惑在于 geom_boxplot()
想要将变量作为 x
和 y
参数,但这不是我想要做的。
我希望 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您没有提供任何可重现的示例,我假设您有一个包含五列的数据框,如下所示:
您想要制作一个包含 5 个箱线图的图,每个箱线图代表每一列。您可能想得到这个。
但是,您希望使用 ggplot 获得类似的绘图。如果是这样,您需要转换
DF
,以便DF
的所有列名称都存储在单个列中,以便可以将其分配给x< /code>,ggplot 需要的。同样,所有相应的值应存储在另一列中,以便可以将其分配给 ggplot 所需的 y 。您可以使用
pivot_longer
来完成此转换。现在可以从
newDF
绘制 5 个箱线图:这是结果图:
Since you do not provide any reproducible example, I assume that you have a data frame with five columns like this:
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.
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 ofDF
are stored in a single column so that it can be assigned tox
, which ggplot requires. Likewise, all the corresponding values should be stored in another column so that it can be assigned toy
, which ggplot requires. You can do this transformation by usingpivot_longer
.It is now possible to plot 5 boxplots from
newDF
:Here is the resulted plot: