在R中的plotly直方图函数中自定义bin宽度 [英] Customizing bin widths in plotly's histogram function in R
问题描述
我有一个日期和每天通话量的数据集.当我使用plotly R软件包对它们进行绘图时,除其中的1个以外,所有其他每个日期都被分隔到不同的bin中.但是,这一棘手的数据子集却将垃圾箱分为2天间隔,这并不是非常有用的信息.我敢肯定这是一个简单的解决方法,但是我不太确定如何更改垃圾箱宽度.
I have a dataset that dates and call volume per day. When I plotted them using the plotly R package, all except for 1 of them had each date separated into a different bin. However, this one tricky subset of the data instead grouped bins into 2 day intervals, which isn't very useful information. I'm sure it's an easy fix, but I'm not quite sure how to change the bin width.
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的交互式可视化内容.谢谢!
This is a sample of the data and code I used. I know how to change the bin widths in ggplot2 and the standard hist() function, but plotly's interactive visualizations are what I am trying to capture here. Thank you!
推荐答案
在@MLavoie的回答之后,我想用一个示例来回答这个问题,其他人在绘制两个重叠的直方图时可以很容易地使用它.
Following @MLavoie 's response, I wanted to answer this with an example that others can easily use when for instance plotting two overlapping histograms.
要添加的重要直方图属性为 nbinsx = 30
,如下所示.
The important histogram attribute to add is nbinsx = 30
as shown below.
# 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轴上的对数刻度可能会很有用.可以通过对代码进行以下更改来完成此操作:
Sometimes a log scale on the y axis can be useful. This can be done with the following change to the code:
# 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
这是具有对数刻度的代码的结果(在这种情况下不是特别有用):
And here is the result of the code with the log scale (which in this case isn't particularly helpful):
这篇关于在R中的plotly直方图函数中自定义bin宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!