hafen / trelliscopejs

TrelliscopeJS R Package
https://hafen.github.io/trelliscopejs
Other
263 stars 37 forks source link

Using cogs function with nested data frame #15

Closed mwhitaker closed 7 years ago

mwhitaker commented 7 years ago

Thank you for a wonderful package! I am trying to use your example and add a cog column, but it's ignored in the final output:

ggplot2::mpg %>%
  group_by(manufacturer, class) %>%
  nest() %>%
  mutate(mean_city_mpg=cogs(data, ~ cog(mean(.x$cty), desc = "Mean city mpg"))) %>%
  mutate(panel = panels(data, ~ figure(xlab = "City mpg", ylab = "Highway mpg") %>%
      ly_points(cty, hwy, data = .x))) %>%
  trelliscope(name = "city_vs_highway_mpg", nrow = 1, ncol = 2)

Could you let me know how to apply cognostics to nested data frames?

Kind regards, Michael

hafen commented 7 years ago

Thanks! Here's a few tweaks to your code so that your additional cognostics show up:

ggplot2::mpg %>%
  group_by(manufacturer, class) %>%
  nest() %>%
  mutate(
    additional_cogs = cogs(data,
      ~ data_frame(mean_city_mpg = cog(mean(.x$cty), desc = "Mean city mpg"))),
    panel = panels(data, ~ figure(xlab = "City mpg", ylab = "Highway mpg") %>%
      ly_points(cty, hwy, data = .x))) %>%
  trelliscope(name = "city_vs_highway_mpg", nrow = 1, ncol = 2)

(note that you could separate the mutate() into two different calls as in the code you supplied if you'd like)

The key here is that for trelliscope() to pick up cognostics, they either need to be atomic columns of the data frame, or a list-column containing data frames. If a list-column data frame has multiple rows (such as the data column you get after calling nest()), it will try to compute some "automatic cognostics" for each of these. You can disable this with auto_cog = FALSE. If it finds any list-columns of single-row data frames, it will just use whatever is in that data frame as-is as cognostics. So in your example, all that was missing was casting the result of your mutate as a data frame. Note that you can name the result of your mutate call to cogs() to be whatever you want - what matters is what you name the columns of the resulting data frame.

I apologize this particular case isn't particularly well-documented right now. I haven't put this on CRAN yet because I want to make sure the tidy pipeline is as intuitive as possible. If you have suggestions or thoughts about ways you would expect it to work, let me know.

mwhitaker commented 7 years ago

Thanks @hafen that works. The only thing not showing up for me is desc = "Mean city mpg" in the Labels. One suggestion would be to add this example to cogs just like you did for panels.

I have been changing my workflow to use list-columns and it's great to see you using that approach. Models and now plot objects all in one neat structure!

hafen commented 7 years ago

I added an example and made it so that nested cog attributes are preserved (so your description should be honored now).

Yes, I agree, the plots as list-columns makes a lot of sense!

mwhitaker commented 7 years ago

Thanks Ryan. Works great on my end!