dbplot icon indicating copy to clipboard operation
dbplot copied to clipboard

Add possibility to facet plots

Open henningsway opened this issue 7 years ago • 3 comments

Great package! :)

It would be nice to be able to facet plots computed in the database, e.g. via facet_wrap() or on a grouped table. Or is there a straightforward approach already that I am missing?

henningsway avatar May 16 '18 19:05 henningsway

You're right, at this time db_compute_count() handles one grouping only, although it's able to handle multiple calculations on that group, but that's not what you're asking for.

I'll look into it, thanks for the suggestion!

edgararuiz-zz avatar May 16 '18 20:05 edgararuiz-zz

I second this request :)

mkirzon avatar May 23 '19 23:05 mkirzon

I achieved this by simply allowing the user to pass in a grouped dataframe to the db_compute_boxplot function, rather than requiring the grouping variable. I'll also return the uncollected and grouped dataframe so it's

db_compute_boxplot <- function(data, var, coef = 1.5) {
  var <- enquo(var)
  res <- data
  
  if("tbl_spark" %in% class(data)) {
    res <- summarise(
      res,
      lower  = percentile_approx(!!var, 0.25),
      middle = percentile_approx(!!var, 0.5),
      upper  = percentile_approx(!!var, 0.75),
      max_raw = max(!!var, na.rm = TRUE),
      min_raw = min(!!var, na.rm = TRUE)
    ) 
  } else {
    res <- summarise(
      res,
      lower  = quantile(!!var, 0.25),
      middle = quantile(!!var, 0.5),
      upper  = quantile(!!var, 0.75),
      max_raw = max(!!var, na.rm = TRUE),
      min_raw = min(!!var, na.rm = TRUE)
    ) 
  }

  res <- mutate(res, iqr = (upper - lower) * coef)
  res <- mutate(
    res,
    min_iqr = lower - iqr,
    max_iqr = upper + iqr
  )
  res <- mutate(
    res,
    ymax = ifelse(max_raw > max_iqr, max_iqr, max_raw),
    ymin = ifelse(min_raw < min_iqr, min_iqr, min_raw)
  )
  return (res)
}

mkirzon avatar Jun 22 '19 16:06 mkirzon