Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

Rbokeh barplot x-axis reordered by y-axis value #238

Open
yiugn opened this issue Jan 30, 2019 · 4 comments
Open

Rbokeh barplot x-axis reordered by y-axis value #238

yiugn opened this issue Jan 30, 2019 · 4 comments

Comments

@yiugn
Copy link

yiugn commented Jan 30, 2019

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.
@Atrebas
Copy link

Atrebas commented Feb 4, 2019

Question 1:
You have to pass the order explicitly in xlim. See #206.

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

Question 2:
Maybe a problem of version. I don't have the issue with rbokeh_0.5.0.

@yiugn
Copy link
Author

yiugn commented Feb 6, 2019

Thank you Atrebas, your comment was helpful, but what I want is to order x axis by y axis value in decreasing order.
For example, the first x axis data should be Trebi, Wisconsin No.38, because they have the greatest yield, and next No.457, and No.462 ...... and so on, like below

image

How can I do this?

@yiugn yiugn changed the title Rbokeh barplot reordered by x-axis Rbokeh barplot x-axis reordered by y-axis value Feb 6, 2019
@Atrebas
Copy link

Atrebas commented Feb 6, 2019

I thought my answer would be enough to guide you. Instead of using the levels as above, you just have to provide the order you want. How to compute this order is a general R question.
Below is an an example, using data.table for convenience.

library(data.table)
library(rbokeh)

barley    <- setDT(lattice::barley)
varieties <- barley[, sum(yield), by = variety][
                    order(-V1), as.character(variety)]

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

@yiugn
Copy link
Author

yiugn commented Feb 7, 2019

Thank you Atrebas, I've got a solution thanks to your help.
I simply converted columns to factors and set it xlim.
Now it works well!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants