2012-05-25 38 views
6

我想在使用facet_wrap()共享一個常見圖例的圖表上獲得多個圖塊。這些圖包含4個使用geom_density()構造的密度估計值。這是數據外觀的一個簡單例子。估計每個估計量的一個密度,併爲每個xp值繪製一個不同的圖。ggplot2中有多個密度的多個圖塊

> esti 
    estimator  value   xp 
1  OLS Oracle 0.35757317 N= 10 T= 100 
2  OLS Oracle 0.50540655 N= 10 T= 100 
3  OLS Full 0.02276872 N= 10 T= 100 
4  OLS Full 0.53616020 N= 10 T= 100 
5   Lasso 0.00000000 N= 10 T= 100 
6   Lasso 0.30448578 N= 10 T= 100 
7 Adaptive Lasso 0.00000000 N= 10 T= 100 
8 Adaptive Lasso 0.49949267 N= 10 T= 100 
9  OLS Oracle 0.48392914 N= 10 T= 500 
10  OLS Oracle 0.53685915 N= 10 T= 500 
11  OLS Full 0.50565482 N= 10 T= 500 
12  OLS Full 0.61407003 N= 10 T= 500 
13   Lasso 0.38342782 N= 10 T= 500 
14   Lasso 0.52012928 N= 10 T= 500 
15 Adaptive Lasso 0.47951875 N= 10 T= 500 
16 Adaptive Lasso 0.53222172 N= 10 T= 500 

我可以構建一個情節與四點密度:

library('ggplot2') 
ggplot(data=esti,aes(x=value,colour=estimator)) + geom_density() 

兩個面板在每一個密度:

ggplot(data=esti,aes(x=value)) + geom_density() +facet_wrap(~xp,scales='free_y') 

但是兩人在一起不工作,結果發生錯誤:

> ggplot(data=esti,aes(x=value,colour=estimator)) + geom_density() +facet_wrap(~xp,scales='free_y') 
Error in UseMethod("scale_dimension") : 
    no applicable method for 'scale_dimension' applied to an object of class "NULL" 

我已經嘗試了不同的比例值,或者完全忽略它,沒有運氣。我試圖跟蹤哪個對象被應用到'scale_dimension',但沒有運氣。任何人都可以啓發我嗎?

+2

只是一個猜測,但可能也有一些做與事實上,你只計算每組兩個值的密度?當我使用更大的數據集創建類似的情節時,我不會得到那個錯誤。 – joran

+0

感謝您使用'geom_density()'時出現的問題。當我嘗試使用'geom_line(stat ='density')'雖然... –

+2

但問題仍然存在,但問題是一樣的,不是嗎?您仍然只計算兩個值的密度。 – joran

回答

2

因爲我不能離開,以第二joran的建議評論(即,我沒有足夠的聲譽),這裏的答案:

通過從

ggplot(data=esti,aes(x=value,colour=estimator)) + geom_density() 

ggplot(data=esti,aes(x=value,colour=estimator)) 
+ geom_density() +facet_wrap(~xp,scales='free_y') 

僅剩下每個估計器/ xp對的2個數據點。看起來,這還不足以計算密度。 例如,下面的代碼行工作(注data=rbind(esti,esti)

ggplot(data=rbind(esti,esti),aes(x=value,colour=estimator)) 
+ geom_density() +facet_wrap(~xp,scales='free_y') 

另外,如果通過geom_bar更換geom_density,它的工作原理

ggplot(data=esti,aes(x=value,colour=estimator)) 
+ geom_bar() +facet_wrap(~xp,scales='free_y')