-3

Im trying to stack histograms for different subgroups. Here is my data.table:

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

Here is the structure:

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

I try to create overlapping histograms, where year identifies the subgroups. I tried to follow two different answers: [1] and [2], but both lead to the same error - which is not googleable. Whats wrong with my data / what do I need to adapt?

> 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
Community
  • 1
  • 1
FooBar
  • 15,724
  • 19
  • 82
  • 171
  • maybe , but surprisingly the error message is changing now? I advise you to read the ggplot2 documentation before asking , if you follow the 2 links in you question the answer is really simple. – agstudy Nov 01 '14 at 01:18
  • This is not reproducible. Please consider amending your question to make it fully reproducible with a clear goal on what the result should look like. – Roman Luštrik Nov 01 '14 at 08:14
  • It's a bit difficult to know what's going on without your data, but try `fill=factor(year)` instead of `fill=year`. – jlhoward Nov 01 '14 at 18:50
  • Good catch, that did the trick. If you post that, I can accept it. – FooBar Nov 01 '14 at 18:59

1 Answers1

1

The error message hints at a continuous group variable. Discretizing it solves the issue:

ggplot(result, aes(x=diff, fill=factor(year))) + geom_histogram(position='identity')
FooBar
  • 15,724
  • 19
  • 82
  • 171