bokeh/rbokeh

respect order of factors in barplots

Opened this issue · 3 comments

Currently, ly_bar does not respect the order of a factor when plotting (the way that for instance ggplot is doing it).
For example:

df <- data.frame(f=c("d", "c", "b", "a"), d=c(3,4,5,6))
df$f <- factor(df$f, levels=c("d", "c", "a", "b"))

figure() %>% ly_bar(df$f, df$d)

This plots the bars in the order of a,b,c,d (alphabetical order), but I'd expect the order to be d,c,a,b.

Is there any other way how the order of categorical values can be defined?

hafen commented

Sorry for the delay on this - been out of town.

I agree that factor ordering should be honored by default in the axes and this will be addressed in the future (feel free to leave this issue open).

For now, you can control the ordering of categorical axes by specifying the order in the xlim or ylim arguments.

For example:

library(rbokeh)

df <- data.frame(f = c("d", "c", "b", "a"), d = c(3, 4, 5, 6))
df$f <- factor(df$f, levels = c("d", "c", "a", "b"))

figure(xlim = c("d", "c", "a", "b")) %>% ly_bar(df$f, df$d)

this works great - thanks for the answer! I wasn't aware of the fact that the ordering can be specified directly via the xlim and ylim arguments (for categorical variables). You might consider adding this also to the documentation - I guess this is useful for many people.
I'll leave the issue open for now (as you suggested).

Yes, good to know indeed. This is an incredibly useful tip.