首页 > 解决方案 > 在 R 中的 plotly 直方图函数中自定义 bin 宽度

问题描述

我有一个数据集,可以显示每天的日期和通话量。当我使用 plotly R 包绘制它们时,除了其中 1 个之外,所有这些都将每个日期分隔到不同的 bin 中。然而,这个数据的一个棘手的子集反而将 bin 分组为 2 天的间隔,这不是非常有用的信息。我确定这很容易解决,但我不太确定如何更改 bin 宽度。

a <- as.Date(c("2019-02-01", "2019-01-14", "2019-01-15", "2019-01-24", "2019-01-31", "2019-01-22","2019-01-14", "2019-01-25", "2019-02-06","2019-01-17", "2019-01-10", "2019-02-06","2019-01-15", "2019-01-17", "2019-01-28","2019-02-04", "2019-01-18","2019-01-15","2019-01-18", "2019-01-25", "2019-01-17","2019-01-30", "2019-01-25", "2019-01-23","2019-01-28", "2019-01-28", "2019-02-06","2019-02-04", "2019-01-24", "2019-01-30","2019-02-01", "2019-01-24", "2019-01-18","2019-01-22", "2019-02-06", "2019-01-17","2019-01-11", "2019-02-06", "2019-01-16","2019-01-31", "2019-02-04", "2019-01-23","2019-01-29", "2019-01-25", "2019-01-22","2019-02-05", "2019-02-01", "2019-01-28","2019-01-22", "2019-01-24", "2019-02-01","2019-01-23", "2019-01-30", "2019-02-05","2019-02-06", "2019-01-24", "2019-02-06","2019-01-30", "2019-01-28", "2019-01-16","2019-01-10", "2019-02-04", "2019-02-07","2019-02-01", "2019-02-04", "2019-01-17","2019-01-17", "2019-02-05", "2019-01-30","2019-02-04", "2019-02-01", "2019-02-01","2019-01-24", "2019-01-23", "2019-02-04","2019-02-04", "2019-01-23", "2019-02-04","2019-01-18", "2019-01-22", "2019-01-24","2019-01-17", "2019-01-22", "2019-02-06","2019-01-10", "2019-01-14", "2019-01-09","2019-02-05", "2019-01-11", "2019-01-17","2019-01-23", "2019-01-23", "2019-02-05","2019-01-11", "2019-02-04", "2019-01-28","2019-01-24", "2019-01-22", "2019-01-24","2019-01-18", "2019-01-31", "2019-02-04","2019-01-22", "2019-01-14", "2019-01-11","2019-01-11", "2019-01-28", "2019-02-01","2019-01-28", "2019-01-25", "2019-02-07","2019-01-24", "2019-02-06", "2019-01-15","2019-01-24", "2019-01-23", "2019-01-17","2019-01-24", "2019-01-24", "2019-01-23","2019-01-24", "2019-01-24", "2019-01-25","2019-01-24", "2019-01-24", "2019-01-28","2019-01-31" ,"2019-01-24", "2019-01-24","2019-01-22", "2019-01-24", "2019-01-17", "2019-01-24", "2019-01-22", "2019-01-23","2019-01-24", "2019-01-22", "2019-02-01","2019-01-14", "2019-01-23", "2019-01-30","2019-02-04", "2019-01-30", "2019-01-30","2019-02-04", "2019-02-04", "2019-01-30", "2019-01-30", "2019-01-30", "2019-01-30", "2019-01-29", "2019-01-31", "2019-01-25","2019-01-28" ,"2019-01-29")
plot_ly(x = a, type = "histogram") %>% layout( title = "Volume", xaxis = list(title = "Date"), yaxis = list(title = "Number of Calls"))

这是我使用的数据和代码的示例。我知道如何更改 ggplot2 和标准 hist() 函数中的 bin 宽度,但我想在这里捕捉到 plotly 的交互式可视化。谢谢!

标签: rhistogramr-plotlybins

解决方案


在@MLavoie 的回复之后,我想用一个其他人在绘制两个重叠直方图时可以轻松使用的示例来回答这个问题。

要添加的重要直方图属性nbinsx = 30如下所示。

# Add required packages
library(plotly)    

# Make some sample data
a = rnorm(1000,4)
b = rnorm(1000,6)

# Make your histogram plot with specified binsize set to 30 here
fig <- plot_ly(alpha = 0.6, nbinsx = 30)
fig <- fig %>% add_histogram(a, name = "first")
fig <- fig %>% add_histogram(b, name = "second")
fig <- fig %>% layout(barmode = "overlay", 
                      yaxis = list(title = "Frequency"),
                      xaxis = list(title = "Values"))

# Print your histogram 
fig

这是代码的结果: 完成的直方图

奖金:

有时,y 轴上的对数刻度可能很有用。这可以通过对代码进行以下更改来完成:

# Add required packages
library(plotly)    

# Make some sample data
a = rnorm(1000,4)
b = rnorm(1000,6)

# Make your histogram plot with specified binsize set to 30 here
fig <- plot_ly(alpha = 0.6, nbinsx = 30)
fig <- fig %>% add_histogram(a, name = "first")
fig <- fig %>% add_histogram(b, name = "second")
fig <- fig %>% layout(barmode = "overlay", 
                      yaxis = list(title = "Frequency", type = "log"),
                      xaxis = list(title = "Values"))

# Print your histogram 
fig

这是带有对数刻度的代码的结果(在这种情况下并不是特别有用):

log_histogram


推荐阅读