trelliscopejs
trelliscopejs copied to clipboard
Sort & Display Trellis By Factor Level Not Alphabetical
Is there currently a way to sort and display our trelliscope so that it sorts by factor level and not alphabetically? I currently have a dataset that I would like to show by day of the week. I initialize the factors of my data like so:
stdnt$Days <- factor(stdnt$Days,levels = c("M","T","W","R","F","S"))
But when I run my ggplot2 code and load up the trellis plot, it still orders the plots by alphabetical order. (i.e. ("F","M","R","S","T","W"))
. I notice the blue icon in the bottom left corner displays:
A->Z Date
Is this a feature that is hardcoded in or is there a way around this? Thanks for your time.
Currently unfortunately this isn't supported but I see it as a top priority use case and I'll look into it. It will take quite a bit of refactoring on the javascript side of things, but it should be doable and is definitely a good thing to support. I can't promise how soon I'll have a solution. I suppose as a temporary workaround you could add another variable for day of week that is numeric and sort on that but show the text label under the panels.
Hi Ryan, I am trying to implement your workaround. I am almost there, but am stuck at showing the proper text label. Any idea how to do that?
library(trelliscopejs)
library(ggplot2)
library(dplyr)
library(tidyr)
my_order <- c("subcompact", "compact", "midsize",
"minivan", "pickup", "suv", "2seater")
mpg %>%
mutate(reclass = factor(class, levels = my_order),
class = as.integer(reclass)) %>%
group_by(class) %>%
nest() %>%
mutate(panel = map_plot(data,
~ ggplot(data = .x, aes(displ, hwy)) +
geom_point() +
xlab("Engine displacement, litres") +
ylab("Highway miles per gallon") +
xlim(1, 7) + ylim(10, 60) +
theme_light())) %>%
trelliscope(name = "MPG custom panel order")
Here's an example based on yours:
mpg %>%
mutate(class = factor(class, levels = my_order),
class_index = as.integer(class)) %>%
group_by(class, class_index) %>%
nest() %>%
mutate(panel = map_plot(data,
~ ggplot(data = .x, aes(displ, hwy)) +
geom_point() +
xlab("Engine displacement, litres") +
ylab("Highway miles per gallon") +
xlim(1, 7) + ylim(10, 60) +
theme_light())) %>%
trelliscope(name = "MPG custom panel order",
state = list(sort = list(sort_spec("class_index"))),
nrow = 1, ncol = 7, height = 800, width = 350)
The basic idea here is that you have the variable class
that you want to show as a label, but also the variable class_index
that you want to sort on by default. Default sorting can be set using the under-documented state
argument.
Wonderful. Very useful workaround.
Thanks for sharing your solution.