【问题标题】:Overlaying histograms in R: Empty rasterR中的叠加直方图:空栅格
【发布时间】:2023-04-10 21:06:01
【问题描述】:

我试图为不同的子组堆叠直方图。这是我的数据表:

> head(result)
     persnr year     diff
1: 61961225 1994 22.52241
2: 62037645 1994 22.52241

结构如下:

> str(result)
Classes ‘data.table’ and 'data.frame':  25213 obs. of  3 variables:
 $ persnr: num  61961225 62037645 62181514 62499451 62649247 ...
 $ year  : int  1994 1994 1994 1994 1994 1994 1994 1994 1994 1994 ...
 $ diff  : num  22.5 22.5 22.5 22.5 22.5 ...
 - attr(*, ".internal.selfref")=<externalptr> 

我尝试创建重叠直方图,其中year 标识子组。我尝试遵循两个不同的答案:[1] 和 [2],但都导致了相同的错误 - 这是不可搜索的。我的数据有什么问题/我需要调整什么?

> ggplot(result, aes(x=diff, fill=year)) + geom_histogram(position='identity')
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
Error in grid.Call.graphics(L_raster, x$raster, x$x, x$y, x$width, x$height,  : 
  Empty raster

ggplot(result) + geom_histogram(aes(x=diff, fill=year), position='dodge')
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
Error in grid.Call.graphics(L_raster, x$raster, x$x, x$y, x$width, x$height,  : 
  Empty raster

【问题讨论】:

  • 也许,但令人惊讶的是错误消息现在正在改变?我建议您在提问之前阅读 ggplot2 文档,如果您按照问题中的 2 个链接进行操作,答案非常简单。
  • 这是不可重现的。请考虑修改您的问题,使其完全可重现,并明确目标应该是什么样的结果。
  • 没有您的数据很难知道发生了什么,但请尝试使用fill=factor(year) 而不是fill=year。
  • 很好,成功了。如果您发布,我可以接受。

标签: r ggplot2 histogram


【解决方案1】:

错误消息提示连续组变量。离散化它可以解决问题:

ggplot(result, aes(x=diff, fill=factor(year))) + geom_histogram(position='identity')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-21
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2013-12-03
    相关资源
    最近更新 更多