【问题标题】:How to plot a histogram from existing counts with uneven bin widths using ggplot如何使用ggplot从具有不均匀bin宽度的现有计数绘制直方图
【发布时间】:2017-12-06 17:58:33
【问题描述】:

我想从已经存在的类中创建一个直方图。我有这个数据集:

interval        counts
0 - 8.50        2577
8.51 - 10.00    1199
10.01 - 12.00   1878
12.01 - 14.00   637
14.01 - 16.00   369
16.01 - 18.00   98
18.00 - 20.00   308



library(ggplot2)

plot_tab5_lohn <- ggplot(DS18, aes(x=interval)) + geom_histogram(stat="count")
return(plot_tab5_lohn)})

确实会生成此图表:

我希望计数在 y 轴上,并且间隔必须是不同的宽度。我该怎么做?

编辑: 我已经做到了这一点: 使用此代码

DS18$interval <- factor(DS18$interval, levels = DS18$interval)
output$DS32 <- renderPlot({
plot_tab5_lohn <- ggplot(DS18, aes(x=interval, y = counts)) +
geom_col() + 
geom_point(color = "red") + 
geom_line(aes(group = 1), color = "red")
return(plot_tab5_lohn)
})

我希望条形与间隔本身一样宽。并且密度应该在 Y 轴上。那么这些区域的总和应该是 1 (100%)。 像这样link

【问题讨论】:

    标签: r ggplot2 histogram


    【解决方案1】:

    您可以提取边界,然后使用geom_rect 进行绘图:

    # Using dt from @www
    library(tidyr)
    dt2 <- separate(dt, interval, c('left', 'right'), sep = ' - ', convert = TRUE)
    ggplot(dt2) +
      geom_rect(aes(xmin = left, xmax = right, ymin = 0, ymax = counts),
                col = 1) +
      geom_line(aes(x = right + (left - right) / 2, y = counts),
                col = 'red')
    

    或者,您可以先将数据扩展为单个观察值,这也很容易让您绘制密度图:

    library(dplyr)
    library(tidyr)
    dt3 <- dt %>% 
      group_by(interval) %>% 
      do(data.frame(interval = rep.int(.$interval, .$counts), stringsAsFactors = FALSE)) %>% 
      separate(interval, c('left', 'right'), sep = ' - ', convert = TRUE) %>% 
      mutate(value = right + (left - right) / 2)
    breaks <- c(0, unique(dt3$right))
    
    ggplot(dt3, aes(value)) +
      geom_histogram(aes(y = ..density..), breaks = breaks, col = 1) +
      geom_freqpoly(aes(y = ..density..), breaks = breaks, col = 'red')
    

    【讨论】:

      【解决方案2】:

      我认为您需要的不是直方图,而是条形图。在这里,我展示了如何使用geom_col 创建条形图。请注意,在绘制数据之前,我使用factor 对每个类的条形进行排序。

      library(ggplot2)
      
      # Order the bar
      dt$interval <- factor(dt$interval, levels = dt$interval)
      # Create the bar plot
      ggplot(dt, aes(x=interval, y = counts)) + geom_col()
      

      数据

      dt <- read.table(text = "interval        counts
      '0 - 8.50'        2577
                       '8.51 - 10.00'    1199
                       '10.01 - 12.00'   1878
                       '12.01 - 14.00'   637
                       '14.01 - 16.00'   369
                       '16.01 - 18.00'   98
                       '18.00 - 20.00'   308",
                       header = TRUE, stringsAsFactors = FALSE)
      

      【讨论】:

      • 谢谢!我想要直方图来可视化间隔的密度,然后我想在直方图上覆盖一个频率多边形。从我的数据中你知道这是否可能吗?
      • @FragenSteller 我不确定如何覆盖频率多边形。您可能想发布一个带有可重现示例的新问题以及解决此问题的所需输出。
      • @FragenSteller 试试ggplot(dt, aes(x=interval, y = counts)) + geom_col() + geom_point(color = "red") + geom_line(aes(group = 1), color = "red")
      • 再次感谢!我更新了我的代码,现在看起来好多了。但仍然不是我想要的托比。我更新了我原来的帖子。你能看看吗?非常感谢你! :-)
      • 我认为 Axeman 提供了一个不错的解决方案。请考虑接受他的回答,而无需进一步修改您的原始问题。
      【解决方案3】:

      您可以使用 stat = "identity" 并添加 y 美学来获得您想要的图表:

      ggplot(DS18, aes(x=interval, y = counts)) + 
        geom_histogram(stat="identity")
      

      给你这个:

      【讨论】:

      • geom_col 是现代的捷径。任何线索如何用条形图做不同大小的箱子?
      猜你喜欢
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 2019-02-22
      相关资源
      最近更新 更多