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

Allow sort with facets in barplots visualizer #170

Open
ColinConwell opened this issue Nov 8, 2021 · 2 comments
Open

Allow sort with facets in barplots visualizer #170

ColinConwell opened this issue Nov 8, 2021 · 2 comments

Comments

@ColinConwell
Copy link

ColinConwell commented Nov 8, 2021

A nice addition to the radiant.data visualizer would be to add the option to sort barplots even with the addition of facets.

I've written a preliminary version of this that's working decently, using tidytext's reorder_within() function.

Beginning at line 510 in radiant.data's visualize.R:

if ("sort" %in% axes && facet_row == "." && facet_col == ".") {
  if ("flip" %in% axes) {
    tmp <- arrange_at(ungroup(tmp), .vars = j)
  } else {
    tmp <- arrange_at(ungroup(tmp), .vars = j, .funs = desc)
  }
  tmp[[i]] %<>% factor(., levels = unique(.))
}

#modified code beginning here...
plot_list[[itt]] <- ggplot(tmp, aes_string(x = i, y = j))

if ("sort" %in% axes && (facet_row != "." || facet_col != ".")) {
  if (facet_row != '.' && facet_col != '.') sorting_vars <- sprintf('list(%s, %s)', facet_row, facet_col)
  if (facet_row != '.' && facet_col == '.') sorting_vars <- sprintf('list(%s)', facet_row)
  if (facet_row == '.' && facet_col != '.') sorting_vars <- sprintf('list(%s)', facet_col)

  i_sorted <- sprintf('tidytext::reorder_within(%s, %s, %s)', i, j, sorting_vars)
  plot_list[[itt]] <- ggplot(tmp, aes_string(x = i_sorted, y = j)) +
    tidytext::scale_x_reordered() + xlab(i)
}

plot_list[[itt]] <- plot_list[[itt]] + {
  if (fill == "none") {
    geom_bar(stat = "identity", position = "dodge", alpha = alpha, fill = fillcol)
  } else {
    geom_bar(stat = "identity", position = "dodge", alpha = alpha)
  }
}

not sure what the easiest way to incorporate this might be, but hope it's possible!

this would also be complimented, I think, by the addition of a 'Scale-x' option in visualize_ui.R, since this frees the scale and makes the plot much easier to digest.

@ColinConwell ColinConwell changed the title Allow sort with facets in visualize Allow sort with facets in barplots visualizer Nov 8, 2021
@vnijs
Copy link
Contributor

vnijs commented Nov 9, 2021

Interesting suggestion @ColinConwell. Can you share an simple R example and the plot that you get out? I'm interested in this option but would prefer to avoid adding a dependency on tidytext

@ColinConwell
Copy link
Author

ColinConwell commented Nov 9, 2021

Sure. Reprex below. (I use aes_string here only to maintain consistency with the code in your visualize function).

data <- get('mtcars') %>% rownames_to_column('model')

ggplot(data, aes_string('tidytext::reorder_within(model, mpg, list(cyl, vs))', 'mpg')) +
  facet_grid(as.formula('cyl~vs'), scales = 'free_y') +
  tidytext::scale_x_reordered() +
  geom_bar(stat = 'identity', position = 'identity') + coord_flip()

The plot that emerges looks like this:

reprex_facet_sort jpg

There are of course other ways of doing this, but they tend to involve creating rankings and then swapping the ranks with the names of the discrete axis labels. If that sounds preferable, I could post an example of that method, as well?

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

2 participants