4

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"))

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!

Joshua Barnes
  • 43
  • 1
  • 6
  • You provide `a` which is not used, and you don't provide `X` which is used. What is `X`? – Stéphane Laurent Feb 21 '19 at 22:19
  • Thanks for bringing this to my attention. I originally typed it out as the date vector 'X', but changed it because it was a bit confusing. – Joshua Barnes Feb 23 '19 at 00:04
  • 2
    when I plot you graphic, I see each bin with two dates. you could use `plot_ly(x = a, type = "histogram", nbinsx = 20) %>% layout( title = "Volume", xaxis = list(title = "Date"), yaxis = list(title = "Number of Calls"))` to adjust the number of bins – MLavoie Feb 23 '19 at 17:56
  • That's exactly what I was looking for. Thank you! – Joshua Barnes Feb 23 '19 at 20:52

1 Answers1

5

Following @MLavoie 's response, I wanted to answer this with an example that others can easily use when for instance plotting two overlapping histograms.

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

And here is the result of the code: Finished histogram

BONUS:

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):

log_histogram

Martin
  • 401
  • 6
  • 15