rbokeh icon indicating copy to clipboard operation
rbokeh copied to clipboard

respect order of factors in barplots

Open werner-rammer opened this issue 8 years ago • 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?

werner-rammer avatar Apr 03 '17 18:04 werner-rammer

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)

hafen avatar Apr 08 '17 00:04 hafen

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).

werner-rammer avatar Apr 08 '17 15:04 werner-rammer

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

lianos avatar Apr 08 '17 16:04 lianos