首页 > 解决方案 > 如何根据 r 中 cut 函数的 bin 为单个变量做 multiboxplot?

问题描述

我正在尝试根据 cut 函数中的 bin 数量为变量创建多框图

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.14e+04,7.43e+04]
  [6] (4.86e+04,6.14e+04] (3.57e+04,4.86e+04] (3.57e+04,4.86e+04] (2.29e+04,3.57e+04] (3.57e+04,4.86e+04]
 [11] (2.29e+04,3.57e+04] (3.57e+04,4.86e+04] (3.57e+04,4.86e+04] (3.57e+04,4.86e+04] (3.57e+04,4.86e+04]
.
.
[501] (2.29e+04,3.57e+04] (3.57e+04,4.86e+04] (3.57e+04,4.86e+04] (3.57e+04,4.86e+04] (3.57e+04,4.86e+04]
[506] (3.57e+04,4.86e+04]
7 Levels: (9.91e+03,2.29e+04] (2.29e+04,3.57e+04] (3.57e+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

标签: rvisualizationboxplotbinning

解决方案


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

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

推荐阅读