我正在尝试使用facet_wrap()将几个共享公共图例的图形绘制在一张图上。这些图包含4个密度估计,每个估计都是使用geom_density()构建的。以下是数据的最简示例。对于每个估计器级别都会估计一个密度,对于xp的每个值,都会绘制一个不同的图。
> 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”的对象,但仍然没有成功。有人可以给我指点吗?
geom_density()
时出现了问题。尽管我尝试使用geom_line(stat='density')
,但问题仍然存在... - Pepin_the_sleepy