【问题标题】:Can i apply facet_wrap or facet_grid with the dbplot package?我可以将 facet_wrap 或 facet_grid 与 dbplot 包一起应用吗?
【发布时间】:2021-06-23 14:20:31
【问题描述】:

我想知道是否可以使用 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()

我最后的错误是:

错误:至少一层必须包含所有刻面变量:gear。

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

非常感谢您的帮助!

【问题讨论】:

    标签: r ggplot2 dbplyr


    【解决方案1】:

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

    如果您包含数据中存在的列,它会照常工作而不会出现任何错误。例如,使用facet_wrap 中的count 列,由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()
    

    【讨论】:

    • 你是对的,我同意,问题是我没有看到在 ggplot 之前合并 faceting 变量(ex.gear)的方法,在这种情况下使用 db_compute_bins 函数,所以它可以考虑到情节
    • 我不确定您将如何将gear 合并到绘图数据中。例如 - mtcars$gear 有 32 个值,但 db_cars %&gt;% db_compute_bins(mpg) 的输出只有 19 行。
    猜你喜欢
    • 2018-05-17
    • 1970-01-01
    • 2015-12-02
    • 2023-03-25
    • 2020-08-04
    • 2018-10-23
    • 2011-02-22
    • 2021-03-16
    • 2016-11-20
    相关资源
    最近更新 更多