按 x 轴重新排序的 Rbokeh 条形图
Rbokeh barplot reordered by x-axis
这是一个用 Rbokeh 表示的条形图的简单示例。
library(rbokeh)
# total yield per variety
figure() %>%
ly_bar(variety, yield, data = lattice::barley, hover = TRUE) %>%
theme_axis("x", major_label_orientation = 90)
结果如下
问题 1)
我想绘制条形图,按产量降序在 x 轴上重新排序
我知道在 ggplot 中使用 'reorder' 函数可以很简单地做到这一点,但不知道如何在 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.
对于第一个问题:您可以通过在xlim
*中指定顺序来控制分类轴的排序。但首先你应该分组 "variety"。我做到了:
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)
第二个问题,或许可以参考。
这是一个用 Rbokeh 表示的条形图的简单示例。
library(rbokeh)
# total yield per variety
figure() %>%
ly_bar(variety, yield, data = lattice::barley, hover = TRUE) %>%
theme_axis("x", major_label_orientation = 90)
结果如下
问题 1) 我想绘制条形图,按产量降序在 x 轴上重新排序
我知道在 ggplot 中使用 'reorder' 函数可以很简单地做到这一点,但不知道如何在 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.
对于第一个问题:您可以通过在xlim
*中指定顺序来控制分类轴的排序。但首先你应该分组 "variety"。我做到了:
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)
第二个问题,或许可以参考