rbokeh
rbokeh copied to clipboard
Rbokeh barplot x-axis reordered by y-axis value
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
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.
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
.
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
How can I do this?
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)
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!