rbar-chartrbokeh

Rbokeh barplot reordered by x-axis


Here is a simple example of barplot expressed in Rbokeh.

library(rbokeh)

# total yield per variety
figure() %>%
  ly_bar(variety, yield, data = lattice::barley, hover = TRUE) %>%
  theme_axis("x", major_label_orientation = 90)

Result are shown as below

enter image description here

Question 1) I want to plot bars, reordered on x-axis by yield in descending order

I know that there's simple way of doing this in ggplot with 'reorder' function, but have no idea how to do this in Rbokeh.

How can I do this?

Question 2) Running the code above, I can see this error message, what does this mean and how can I solve this problem?

Warning messages:
1: In structure(x, class = unique(c("AsIs", oldClass(x)))) :
  Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
  Consider 'structure(list(), *)' instead.
2: In structure(x, class = unique(c("AsIs", oldClass(x)))) :
  Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
  Consider 'structure(list(), *)' instead.
3: In structure(x, class = unique(c("AsIs", oldClass(x)))) :
  Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
  Consider 'structure(list(), *)' instead.
4: In structure(x, class = unique(c("AsIs", oldClass(x)))) :
  Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
  Consider 'structure(list(), *)' instead.
5: In structure(x, class = unique(c("AsIs", oldClass(x)))) :
  Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
  Consider 'structure(list(), *)' instead.
6: In structure(x, class = unique(c("AsIs", oldClass(x)))) :
  Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
  Consider 'structure(list(), *)' instead.
7: In structure(x, class = unique(c("AsIs", oldClass(x)))) :
  Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
  Consider 'structure(list(), *)' instead.

Solution

  • For the first question: you can control the ordering of categorical axes by specifying the order in the xlim*. But first you should group "variety". I did:

    barley_data <- lattice::barley %>% 
      group_by(variety) %>% 
      summarise(yield = sum(yield))
    

    Then, generate the plot:

    figure(xlim = barley_data$variety[order(-barley_data$yield)]) %>%
      ly_bar(variety, yield, data = barley_data, hover = TRUE) %>%
      theme_axis("x", major_label_orientation = 90)
    

    For the second question, perhaps you can refer to this.