如何基于R中的cut函数的bin对单个变量进行multiboxplot?

问题描述

我正在尝试根据剪切函数的仓数为变量创建多箱图

movie_reg %>% select(Collection) %>% pull() %>% cut(7) 

[1] (3.57e+04,4.86e+04] (3.57e+04,4.86e+04] (6.14e+04,7.43e+04] (6.14e+04,7.43e+04]
  [6] (4.86e+04,6.14e+04] (3.57e+04,4.86e+04] (2.29e+04,3.57e+04] (3.57e+04,4.86e+04]
 [11] (2.29e+04,4.86e+04]
.
.
[501] (2.29e+04,4.86e+04]
[506] (3.57e+04,4.86e+04]
7 Levels: (9.91e+03,2.29e+04] (2.29e+04,4.86e+04] (4.86e+04,6.14e+04] ... (8.71e+04,1e+05]

我不确定我将如何精确地将水平和相应的值传递到箱线图中。以下是我尝试过但出现错误内容

movie_reg %>% select(Collection) %>% pull() %>% cut(7) %>% Boxplot(aes(x=levels))
Error in sort.int(x,na.last = na.last,decreasing = decreasing,...) : 'x' must be atomic

解决方法

如果您提供一个可复制的示例会更好,因为现在很难为您提供帮助。我确实注意到您在boxplot函数中使用美学,但是美学是ggplot的一部分,而不是基本函数boxplot()。另外,在多箱图中,您需要提供x和y,所以也许您只需要一个条形图来显示每组的计数(只需要一个x)?

movie_reg %>% 
  select(Collection) %>% 
  pull() %>% 
  cut(7) %>% 
  ggplot(aes(x=levels)) + 
  geom_bar()
,

我认为您将基数R中的boxplot()与ggplot2中的geom_boxplot()混合在一起。无论如何,如果您的问题是关于从cut()获得的类别的可视化,则可以使用以下方式添加列:

movie_reg = data.frame(Collection = runif(100))
movie_reg %>% mutate(levels = cut(Collection,7))

使用箱线图:

boxplot(Collection ~ levels,data=movie_reg %>% mutate(levels = cut(Collection,7)),horizontal=TRUE,las=2,cex.axis=0.6)

enter image description here

或ggplot2:

movie_reg %>% mutate(levels = cut(Collection,7)) %>% ggplot(aes(x=levels,y=Collection)) + geom_boxplot() + coord_flip()

enter image description here