我正在尝试在 rbokeh 中生成直方图。
直接方法ly_hist
会导致意外计数(下图,顶部)。间接方法ly_bar
给出了一个未按因子水平排序的 x 轴(下图,底部)。
rbokeh
ggplot2
给出预期的结果。
代码:
library(data.table)
library(rbokeh)
library(ggplot2)
# generate data ==============
set.seed(123)
x = data.table(
hour = sample.int(n = 24, size = 100, replace = T)
)
# summarize
y = x[, .N, keyby = hour]
# ggplot ======================
theme_set(theme_bw())
g1 = ggplot(x) +
geom_histogram(aes(hour), bins = 24, fill = "steelblue", col = "white", alpha = 0.5 ) +
scale_x_continuous(breaks = seq(1, 24, 1))
g2 = ggplot(y) +
geom_bar(aes(hour, N), stat = "identity", fill = "steelblue", alpha = 0.5)
# rbokeh ==================
b1 = figure() %>%
ly_hist(hour, data = x, breaks = 24)
y[, hour := factor(hour)]
b2 = figure() %>%
ly_bar(hour, N, data = y)
问:(1)如何使用产生预期结果的 rbokeh 生成直方图(如在 ggplot2 中)和(2)如何让 x 轴以正确的顺序排序?