Rbokeh条形图通过X轴重新排序

时间:2019-01-30 07:33:36

标签: r bar-chart rbokeh

这是Rbokeh中表示的barplot的简单示例。

library(rbokeh)

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

结果显示如下

enter image description here

问题1) 我想绘制条形图,并按收益率降序在x轴上重新排序

我知道在ggplot中使用“重新排序”功能可以做到这一点,但是不知道如何在Rbokeh中做到这一点。

我该怎么做?

问题2)  运行上面的代码,我可以看到此错误消息,这是什么意思,如何解决此问题?

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.

1 个答案:

答案 0 :(得分:1)

对于第一个问题:您可以通过在xlim *中指定顺序来控制分类轴的顺序。但是首先您应该将“品种”分组。我做到了:

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

然后,生成图:

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)

对于第二个问题,也许您可​​以参考this