【问题标题】:A circular histogram in R shows incorrect valuesR 中的圆形直方图显示不正确的值
【发布时间】:2020-11-06 14:40:21
【问题描述】:

我正在尝试从 here 重新创建一个圆形图(此页面上的第一个图),但我刚刚得到的输出似乎不正确。 “最后一个”条(介于 23 和 0 之间)缺失,而“第一个”条(介于 0 和 1 之间)不成比例地高。更重要的是,条形图向左“移动”了一个单位,而在情节上方的网站上似乎很好。

这是我从该站点复制的代码。我所做的唯一区别是我从 geom_histogram() 中删除了 "width=2",否则它会引发错误,指出参数 width 已弃用。


library(lubridate)
library(ggplot2)   

set.seed(44)
N=500
events <- as.POSIXct("2011-01-01", tz="GMT") + 
              days(floor(365*runif(N))) + 
              hours(floor(24*rnorm(N))) +  
              minutes(floor(60*runif(N))) +
              seconds(floor(60*runif(N)))

hour_of_event <- hour(events)

eventdata <- data.frame(datetime = events, eventhour = hour_of_event)
# determine if event is in business hours
eventdata$Workday <- eventdata$eventhour %in% seq(9, 17)

ggplot(eventdata, aes(x = eventhour, fill = Workday)) + 
    geom_histogram(breaks = seq(0, 24), colour = "grey") + 
    coord_polar(start = 0) + theme_minimal() + 
    scale_fill_brewer() + ylab("Count") + 
    ggtitle("Events by Time of day") + 
    scale_x_continuous("", limits = c(0, 24), breaks = seq(0, 24), labels = seq(0, 24))

这是我得到的:

这是一个数据表。您可以看到第 23 小时的值应该是 17,而不是像我的情节中的 0。

table(eventdata$eventhour)
0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 
23 22 18 26 28 20 19 21 16 17 20 16 18 22 16 21 24 21 22 27 25 18 23 17

您知道为什么我的绘图没有显示正确的值以及如何解决这个问题吗?

【问题讨论】:

    标签: r ggplot2 plot histogram


    【解决方案1】:

    我根据this post提出这个解决方案:

    library(lubridate)
    library(ggplot2)
    
    set.seed(44)
    N=500
    events <- as.POSIXct("2011-01-01", tz="GMT") + 
      days(floor(365*runif(N))) + 
      hours(floor(24*rnorm(N))) +  
      minutes(floor(60*runif(N))) +
      seconds(floor(60*runif(N)))
    
    hour_of_event <- hour(events)
    
    eventdata <- data.frame(datetime = events, eventhour = hour_of_event)
    # determine if event is in business hours
    eventdata$Workday <- eventdata$eventhour %in% seq(9, 17)
    
    df <- data.frame(table(eventdata$eventhour),
                      business_hour = 0:23 %in% seq(9, 17))
    colnames(df)[1:2] <- c("hour", "value")
    
    
    ggplot(df, aes(hour, value, fill = business_hour)) +
      coord_polar(theta = "x", start = 0) +
      geom_bar(stat = "identity", width = .9)
    

    我希望它有所帮助。它不会告诉您为什么会遇到问题,但会为您提供可行的解决方案。

    【讨论】:

    • 谢谢。这不完全是我想要的,但如果只有酒吧在几个小时之间,而不是在它们之下,那将是完美的解决方案。
    • 我搜索了一段时间,但没有找到......对不起
    • 没问题,再次感谢。与此同时,无论如何我找到了我原来的情节的解决方案:)
    【解决方案2】:

    问题似乎是由geom_histogramscale_x_continuous 函数的参数引起的。

    而不是这个:

    geom_histogram(breaks = seq(0, 24), colour = "grey") +
    scale_x_continuous("", limits = c(0, 24), breaks = seq(0, 24), labels = seq(0, 24))
    

    应该是:

    geom_histogram(bins = 24, colour = "grey") +
    scale_x_continuous(breaks = seq(-0.5, 23.5), labels = seq(0, 24))
    

    我仍然有点困惑,为什么它只能以这种方式工作,但它最终可以工作......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-15
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-10
      • 2019-10-26
      相关资源
      最近更新 更多