简体   繁体   English

如何创建多个箱线图,同时使用 ggplot2 中的 geom_boxplot 使它们成比例?

[英]How do I create multiple boxplots whilst also making them proportional using geom_boxplot within ggplot2?

I understand that varwidth = TRUE within ggplot2's geom_boxplot can be used to create proportional boxplots so that each boxplot also summarises the number of points within a plot.我知道 ggplot2 的 geom_boxplot 中的 varwidth = TRUE 可用于创建比例箱线图,以便每个箱线图还总结了 plot 内的点数。 However, I am struggling to maintain proportional boxplot sizes whilst also producing multiple plots?但是,我正在努力保持成比例的箱线图大小,同时还产生多个图?

Using the diamonds df in ggplot2 I am trying to reproduce the below image.使用 ggplot2 中的钻石 df 我试图重现下图。垂直比例 geom_boxplot

What I have tried is this:我试过的是这样的:

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

I tried to use cut_number to achieve the multiple, and var-width for proportionality.我尝试使用 cut_number 来实现倍数,并使用 var-width 来实现比例。 I'd also like to have the boxplots show vertically.我还想让箱线图垂直显示。 I have been stumped on this for hours and have looked online but to no avail.我已经为此困扰了几个小时,并且在网上查看过,但无济于事。 Any tips?有小费吗?

Since ggplot2 v3.3.0, the orientation of layers are autodetected.自 ggplot2 v3.3.0 起,自动检测层的方向。 In some cases, this detection can be incorrect.在某些情况下,这种检测可能不正确。 You can force the orientation by adding it as an argument to a layer:您可以通过将其作为参数添加到图层来强制方向:

library(ggplot2)

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

Created on 2020-05-01 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 5 月 1 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM