2014-11-01 155 views
-3

我試圖爲不同的子組堆疊直方圖。這裏是我的data.table:在R中疊加直方圖:空光柵

> 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],但都導致相同的錯誤 - 這是不可googleable。我的數據有什麼問題/我需要適應什麼?

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

也許,但令人驚訝的是,錯誤信息現在正在改變?我建議你在詢問之前閱讀ggplot2文檔,如果你按照你中的2個鏈接提問,答案很簡單。 – agstudy 2014-11-01 01:18:03

+0

這是不可重複的。請考慮修改您的問題,使其完全可重複,並確定結果應該是什麼樣子。 – 2014-11-01 08:14:03

+0

在沒有數據的情況下知道發生了什麼有點困難,但嘗試使用'fill = factor(year)'而不是'fill = year'。 – jlhoward 2014-11-01 18:50:05

回答

1

錯誤消息提示連續組變量。離散它解決了這個問題:

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