如何让ggplot ecdf以填充背景进行绘图 [英] How to get ggplot ecdf to plot with filled background

查看:73
本文介绍了如何让ggplot ecdf以填充背景进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的经验累积密度曲线填充背景,但似乎无法实现.

I'm trying to get my empirical cumulative density curves to have their backgrounds filled but can't seem to get it going.

我尝试了以下两种方法,第一种方法似乎改变了曲线的 alpha 而不是填充

I've tried the following two approaches, the first which appears to change the alpha of the curve and not the fill

ggplot( myDataFrame , aes( x=myVariable , fill=myFactor ) )                       +
geom_step      ( stat="ecdf" , aes( colour = myFactor  ) , alpha=1 )              +
coord_cartesian( xlim = c( 0 , quantile( myDataFrame$myVariable , prob=0.99 ) ) ) +
facet_grid     ( myFactor ~ . , scales="free_y" )

第二个似乎与上面的等价

The second appears to be equilavent to the above

ggplot( myDataFrame , aes( x=myVariable , fill=myFactor ) )                       +
stat_ecdf      ( aes( colour = myFactor ) ,alpha=0.2 )                            +
coord_cartesian( xlim = c( 0 , quantile( myDataFrame$myVariable , prob=0.99 ) ) ) +
facet_grid     ( myFactor ~ . , scales="free_y" )

我也想知道它是否会填充整个 xlim 中早期饱和的因子水平的 100%.

I wonder too if it will fill the 100% across the full xlim for factor levels which saturate early.

推荐答案

类似的事情?

library(ggplot2)
set.seed(1)
df <- data.frame(var=c(rnorm(1000),rpois(1000,25),rgamma(1000,25)),
                 fact=rep(c("N","P","G"),each=1000))
ggplot(df,aes(x=var,fill=fact))+
  stat_ecdf(aes(ymin=0,ymax=..y..),geom="ribbon")+
  stat_ecdf(geom="step")+
  facet_grid(fact~.,scales="free_y")

编辑:(对下面评论的回复).

Edit: (Response to comment below).

符号 ..y.. 提供对映射到 ggplot 的 y-aesthetic 的任何内容的访问.

The notation ..y.. provides access to whatever is mapped to ggplot's y-aesthetic.

stat_*(...) 函数创建了一个映射到 y 美学的隐式变量.对于 stat_ecdf(...),它是 ecdf:x 中小于或等于给定 x 的观察值的分数.ggplot 会自动将此内部变量映射到 y-aesthetic.

The stat_*(...) functions create an implicit variable that maps to the y-aesthetic. For stat_ecdf(...) it's the ecdf: the fraction of observations in x that are less than or equal to the given x. ggplot will automatically map this internal variable to the y-aesthetic.

但在本例中,我们使用的是色带几何体,它需要 ymaxymin 美学,而不是 y.因此,通过设置 ymax=..y..,我们告诉 ggplot 将 ymax-aesthetic 映射到映射到 y 的任何内容.

But in this case we are using the ribbon geometry, which requires ymax and ymin aesthetics, not y. So by setting ymax=..y.. we're telling ggplot to map the ymax-aesthetic to whatever is mapped to y.

这篇关于如何让ggplot ecdf以填充背景进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆