【问题标题】:Filling cross over under a Cumulative Frequency plot using ggplot in R在 R 中使用 ggplot 在累积频率图下填充交叉
【发布时间】:2015-08-06 15:26:19
【问题描述】:

我正在尝试在 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%。

非常感谢您的帮助:-)

【问题讨论】:

    标签: r ggplot2 fill cdf ecdf


    【解决方案1】:

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

    【讨论】:

    • 谢谢!我已经编辑了帖子,因为您的建议现在可以使用了:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 2021-03-19
    • 2012-10-16
    • 2017-12-25
    • 2019-10-12
    相关资源
    最近更新 更多