#1、准备工作install.packages(“tidyverse”)library(nycflights13)library(tidyverse)2、对carat进行分箱,然后为每组生成一个箱线。使用了cut_width(x,width函数将x变量分成宽度为width的分箱。)

ggplot(data = diamonds, mapping = aes(x=carat,y=price))+
  geom_boxplot(mapping = aes(group = cut_width(carat,0.1)))

#3、近似的显示每个分箱中的数据点的数量,可以使用cut_number()函数

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