6

我正在尝试使用 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”,但也没有运气。任何人都可以启发我吗?

4

1 回答 1

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_densitygeom_bar,它的工作原理

ggplot(data=esti,aes(x=value,colour=estimator))
+ geom_bar() +facet_wrap(~xp,scales='free_y')
于 2013-04-22T02:45:15.517 回答