我可以将 facet_wrap 或 facet_grid 与 dbplot 包一起应用吗?

问题描述

我想知道是否可以使用 dbplot 包制作绘图并应用 facet_grid 或 facet_wrap,我不确定该包是否支持或者我做错了什么,这是我的主要想法:>

library(odbc)
library(tidyverse)
library(dbplyr)
library(dbplot)
library(DBI)

con <- dbConnect(Rsqlite::sqlite(),":memory:")

db_cars <- copy_to(con,mtcars,"cars")

db_cars %>% 
  db_compute_bins(mpg) %>% 
  ggplot() +
  geom_col(aes(mpg,count,fill = count)) +
  #Here is were i try to do the facet,i think that maybe it doesent work because i´m not computing 
  #gear,but i have tried to do it separately with a pipe  but still doesent work
  facet_wrap(~gear) +
  labs(title = "Mtcars - mpg distribution") +
  theme_minimal()

我最后的错误是:

错误:至少一层必须包含所有分面变量:齿轮。

情节缺少齿轮 第 1 层缺少齿轮

非常感谢您的帮助!

解决方法

该错误与 dbplot 包无关,您收到错误是因为 gear 之后的数据中没有 db_cars %>% db_compute_bins(mpg) 列。

如果您包含数据中存在的列,它会照常工作,不会出现任何错误。例如,使用由 count 生成的 facet_wrap 中的 db_compute_bins 列。

library(odbc)
library(tidyverse)
library(dbplyr)
library(dbplot)
library(DBI)

db_cars %>% 
  db_compute_bins(mpg) %>%
  ggplot() +
  geom_col(aes(mpg,count,fill = count)) +
  facet_wrap(~count) +
  labs(title = "Mtcars - mpg distribution") +
  theme_minimal()

enter image description here