使用 cut 为变量创建组,我将如何在 ggplot 中使用它?

Used cut to create groups for a variable, how would I use it in ggplot?

我使用 cut 函数为变量创建 bin,结果是这样的:

var <- structure(c(1L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 3L, 2L), .Label = c("[0,3]", 
"(3,40]", "(40,738]"), class = "factor")

但是我如何在 ggpplot 中使用它?我不清楚该怎么做,因为不再有数据框了。

plot(var)

基本上我想重新创建上面的直方图但是在 ggplot 中。

这是一个可复制的版本:

qv <- round(quantile(iris$Sepal.length))
d <- cut(iris$Sepal.length, qv[!duplicated(qv)], include.lowest=TRUE)

来自 ggplot2 手册:

geom_bar() makes the height of the bar proportional to the number of cases in each group

您可以使用以下代码:

ggplot2::ggplot(data = data.frame(var = var)) +
  geom_bar(aes(x=var))

这将产生相当于基础 R 的输出。