0

Im wondering if it´s possible to make a plot with the dbplot package and apply facet_grid or facet_wrap, im not sure if the package supports that or if im doing something wrong, here is the main idea that i have:

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()

The error that i have at the end is:

Error: At least one layer must contain all faceting variables: gear.

Plot is missing gear Layer 1 is missing gear

Thanks a lot for your help!

1 Answers1

1

The error has nothing to do with dbplot package, you are getting the error because there is no gear column in the data after db_cars %>% db_compute_bins(mpg).

If you include the column which is present in the data it works as usual without any error. For example, using the count column in facet_wrap which is generated by 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

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • You are right and i agree, the thing is that i don´t see the way to incorporate the faceting variable (ex.gear) before the ggplot, in this case with the db_compute_bins function so it can take it into account for the plot – sebastian fanchi Jun 23 '21 at 15:17
  • I am not sure how will you incorporate `gear` into the plotting data. For eg - `mtcars$gear` has 32 values but output of `db_cars %>% db_compute_bins(mpg)` is only 19 rows. – Ronak Shah Jun 24 '21 at 02:10