如何在ggplot2中使用geom_boxplot创建多个boxplots,并使它们成比例?

问题描述 投票:0回答:1

我知道在ggplot2的geom_boxplot中的varwidth = TRUE可以用来创建按比例的boxplots,这样每个boxplots就可以总结出一个图中的点的数量。然而,我很难保持比例的boxplot大小,同时还能生成多个图。

使用ggplot2中的钻石df,我试图重现下面的图像。vertical proportional geom_boxplot

我试过的方法是这样的。

ggplot(data = diamonds, mapping = aes(x = carat, y = price)) + geom_boxplot(mapping = aes(group = cut_number(carat, 20), varwidth = TRUE))

我试着用cut_number来实现多重图,用var-width来实现比例。我还想让boxplots垂直显示。我已经在这个问题上纠结了好几个小时,也在网上找过,但是没有用。有什么建议吗?

r ggplot2 boxplot
1个回答
0
投票

自从ggplot2 v3.3.0以来,图层的方向是自动检测的。在某些情况下,这种检测可能是不正确的。您可以通过将其作为参数添加到图层中来强制确定方向。

library(ggplot2)

ggplot(data = diamonds, mapping = aes(x = carat, y = price)) + 
  geom_boxplot(mapping = aes(group = cut_number(carat, 20)),
               orientation = "x")

创建于2020-05-01 重读包 (v0.3.0)

© www.soinside.com 2019 - 2024. All rights reserved.