trelliscopejs icon indicating copy to clipboard operation
trelliscopejs copied to clipboard

Let facet_trelliscope() display cognostic descriptions

Open richierocks opened this issue 4 years ago • 1 comments

If I make a plot manually, using the nest+map approach, I can see the cognostic descriptions in the Labels tool.

Here's the setup.

library(dplyr)
library(tidyr)
library(ggplot2)
library(trelliscopejs)

mpg_selected <- mpg %>% 
  select(cty, hwy, manufacturer)

Here's the plot.

plot_1_panel <- function(data) {
  ggplot(data, aes(cty, hwy)) +
    geom_point()
}

get_cogs <- function(data) {
  tibble(
    cty_mean = cog(mean(data$cty), desc = mpg_labels$cty),
    hwy_mean = cog(mean(data$hwy), desc = mpg_labels$hwy)
  )
}

mpg_selected %>% 
  group_by(manufacturer) %>% 
  nest() %>% 
  mutate(
    panel = map_plot(data, plot_1_panel),
    cogs = map_cog(data, get_cogs)
  ) %>% 
  trelliscope(name = "Cognostics manually specified")

And here's the Labels pane.

Screenshot 2020-04-26 20 58 50


This is a lot of code just to get label descriptions. I want to just define some columns in the data frame as cognostics

mpg_with_cog <- mpg_selected %>% 
  mutate(
    cty = cog(cty, desc = mpg_labels$cty),
    hwy = cog(hwy, desc = mpg_labels$hwy),
    manufacturer = cog(hwy, desc = mpg_labels$manufacturer)
  )

then use facet_trelliscope().

ggplot(mpg_with_cog, aes(cty, hwy)) +
  geom_point() +
  facet_trelliscope(
    vars(manufacturer),
    name = "Cognostics specified",
    desc = "But the labels have turned strange"
  )

Unfortunately, this seems to make a mess of the labels.

Screenshot 2020-04-26 20 59 49

It feels like there ought to be a way to give the labels descriptions while using facet_trelliscope(). Is this possible? Otherwise, is this feature feasible?

Possibly related issue: https://github.com/hafen/trelliscopejs/issues/59

richierocks avatar Apr 27 '20 01:04 richierocks

To me it appears that the only difference between the two is that cty is not automatically included when using facet_trelliscope(), right? Otherwise, the labels look the same in both examples.

cty doesn't appear because it varies within manufacturer. facet_trelliscope() will automatically include any column as a cognostic that does not vary within the faceting variables.

So to get a cognostic for cty, I'm guessing you would want the mean of cty as the metric to sort by / label, etc. Here's an example:

mpg_with_cog <- mpg_selected %>% 
  mutate(
    cty_mean = cog(mean(cty), desc = mpg_labels$cty),
    hwy_mean = cog(mean(hwy), desc = mpg_labels$hwy),
    manufacturer = cog(manufacturer, desc = mpg_labels$manufacturer)
  )

ggplot(mpg_with_cog, aes(cty, hwy)) +
  geom_point() +
  facet_trelliscope(
    vars(manufacturer),
    name = "Cognostics specified",
    desc = "But the labels have turned strange"
  )

hafen avatar May 01 '20 22:05 hafen