使用 R 中的 ggplot 在累积频率图下填充交叉

Filling cross over under a Cumulative Frequency plot using ggplot in R

我正在尝试在 ggplot 中绘制两条累积频率曲线,并在某个截止点处遮蔽交叉点。我使用 ggplot 的时间不长,所以我希望有人可以帮助我解决这个问题。

没有填充区域的图,看起来像这样...

我使用以下代码创建的...

library(ggplot2) # required 

north <- rnorm(3060, mean=277,sd=3.01) # to create synthetic data 
south <- rnorm(3060, mean=278, sd=3.26) # in place of my real data. 

#placing in dataframe
df_temp <- data.frame(temp=c(north,south), 
    region=c(rep("north",length=3060),rep("south",length=3060)))

#manipulating into cdf, as I've seen in other examples
temp.regions <- ddply(df_temp, .(region), summarize,
                          temp = unique(temp),
                          ecdf = ecdf(temp)(unique(temp)))

# feeding into ggplot. 
 ggplot(temp.regions,aes(x=temp, y=ecdf, color = region)) + 
      geom_line(aes(x=temp,color=region))+
      scale_colour_manual(values = c("blue","red"))

然后我想要的是为 y 轴上低于 0.2 的温度遮蔽两条曲线。理想情况下,我希望看到蓝色阴影为蓝色,红色阴影为红色。然后,他们在紫色交叉的地方。

不过,我管理的最接近的如下... ]

这是我通过对代码添加以下内容实现的。

# creating a dataframe with just the temperatures for below 0.2
# to try and aid control when plotting
temp.below <- temp.regions[which(temp.regions$ecdf<0.2),]

# plotting routine again. 
ggplot(temp.regions, aes(x=temp, y=ecdf, color = region)) + 
  geom_line(aes(x=temp,color=region))+
  scale_colour_manual(values = c("blue","red"))+
# with additional line for shading.
  geom_ribbon(data=temp.below,
              aes(x=temp,ymin=0,ymax=0.2), alpha=0.5)

我见过一些人为正态分布密度图着色的例子,这是我改编代码的地方。但出于某种原因,我的盒子似乎不想与温度曲线有任何关系。

请帮忙!我敢肯定这很简单,我只是真的迷路了并且尝试了一些,产生的结果不如这些令人信服。

非常感谢您的浏览。

由于以下帮助,问题已解决...

运行 下面的建议代码

geom_ribbon(aes(ymin=0,ymax=ecdf, fill=region), alpha=0.5)

给...

这几乎是我所追求的解决方案,但最后添加了一个...就像这样

#geom_ribbon(aes(ymin=0,ymax=ecdf, fill=region), alpha=0.5)
geom_ribbon(data=temp.below, aes(ymin=0,ymax=ecdf, fill=region), alpha=0.5)

我得到了我想要的...

我重新设置数据的原因是它只填充了两个区域中最低的20%。

非常感谢您的帮助:-)

看来您的考虑是正确的。 使用 geom_ribbon 我认为您不需要将数据设置为其他任何内容。只需设置 aes(ymin = 0, ymax = ecdf, fill = region)。我认为应该这样做。