I am trying to generate a histogram in rbokeh.
The direct approach ly_hist
leads to an unexpected counts (fig below, top). The indirect approach ly_bar
gives a x-axis that is not sorted by factor level (fig below, bottom).
rbokeh
ggplot2
gives the expected results.
code:
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)
Q: (1) how can I generate a histogram using rbokeh that produces the expected result (as in ggplot2) and (2) how can I get the x-axis to be sorted in the right order?