【问题标题】:Customizing bin widths in plotly's histogram function in R在 R 中的 plotly 直方图函数中自定义 bin 宽度
【发布时间】:2019-02-21 21:36:32
【问题描述】:

我有一个数据集,其中包含每天的日期和通话量。当我使用 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 的交互式可视化。谢谢!

【问题讨论】:

  • 您提供未使用的a,您未提供已使用的XX 是什么?
  • 感谢您提请我注意。我最初将它输入为日期向量'X',但因为它有点混乱而改变了它。
  • 当我绘制图形时,我看到每个 bin 都有两个日期。你可以使用plot_ly(x = a, type = "histogram", nbinsx = 20) %&gt;% layout( title = "Volume", xaxis = list(title = "Date"), yaxis = list(title = "Number of Calls")) 来调整垃圾箱的数量
  • 这正是我想要的。谢谢!

标签: r histogram r-plotly bins


【解决方案1】:

在@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

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

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多