Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to facet plots #12

Open
henningsway opened this issue May 16, 2018 · 3 comments
Open

Add possibility to facet plots #12

henningsway opened this issue May 16, 2018 · 3 comments

Comments

@henningsway
Copy link

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?

@edgararuiz-zz
Copy link
Owner

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!

@mkirzon
Copy link

mkirzon commented May 23, 2019

I second this request :)

@mkirzon
Copy link

mkirzon commented Jun 22, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants