r - 在 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 的交互式可视化。谢谢!
解决方案
在@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
这是带有对数刻度的代码的结果(在这种情况下并不是特别有用):
推荐阅读
- mysql - 服务器时区值“Ostereuropeische Zeit”无法识别
- accessibility - WCAG 2.0(AA 级) - 输入元素错误 - 没有输入代码
- c++ - 将文件内容存储到 unordered_map 仅存储最后的项目和值
- laravel - 使用护照访问令牌获取用户数据 - Laravel
- hibernate - Hibernate Entity 类的构造函数
- java - 我有一个充当搜索栏的editText,如果用户没有在搜索栏上输入任何内容,我如何隐藏recyclerView 项目
- html - Bootstrap 4 将侧边栏折叠成导航栏下拉菜单
- flutter - 将字符串添加到列表时的奇怪行为
- python - 使用 Python 进行重定向
- java - 在netbeans中以jFrame形式设计未加载