gpt4 book ai didi

r - ggplot2 中具有多个密度的多个图

转载 作者:行者123 更新时间:2023-12-04 10:07:39 24 4
gpt4 key购买 nike

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

最佳答案

由于我无法发表评论以支持 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')

每个 estimator/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_density来自 geom_bar , 有用
ggplot(data=esti,aes(x=value,colour=estimator))
+ geom_bar() +facet_wrap(~xp,scales='free_y')

关于r - ggplot2 中具有多个密度的多个图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10751646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com