简体   繁体   中英

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. 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.垂直比例 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. 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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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