简体   繁体   English

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

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

I am trying to plot two Cumulative Frequency curves in ggplot, and shade the cross over at a certain cut off. 我正在尝试在ggplot中绘制两条累积频率曲线,并以一定的截止度遮挡交叉。 I haven't been using ggplot for long, so I was hoping someone might be able to help me with this one. 我没有使用ggplot很久了,所以我希望有人可以帮助我。

The plot without filled regions, looks like this... 没有填充区域的情节看起来像这样... 北方和南方地区的累积频率

Which I have created using the following code... 我使用以下代码创建的...

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"))

What I would then like, would be to shade both curves for temperatures below 0.2 on the y axis. 然后我想要在y轴上为温度低于0.2的两条曲线着色。 Ideally I'd like to see the blue one shaded in blue, and the red one shaded in red. 理想情况下,我希望看到蓝色的阴影为蓝色,红色的阴影为红色。 Then, where they cross over in purple. 然后,他们穿过紫色的地方。

However, the closest I have managed is as follows... 但是,我管理的最接近的情况如下... 我第一次尝试让曲线下的区域 ] ]

Which I have achieved using the following additions to my code. 我使用以下代码实现了这一点。

# 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)

I've seen a few examples of people shading for a normal distribution density plot, which is where I have adapted my code from. 我已经看到了一些人为正态分布密度图着色的示例,这是我改编自其中的代码的地方。 But for some reason my boxes don't seem to want anything to do with the temperature curve. 但是由于某种原因,我的盒子似乎与温度曲线没有任何关系。

Please help! 请帮忙! I'm sure it's quite simple, I'm just really lost and have tried a few, producing less convincing results than these. 我敢肯定,这很简单,我真的迷路了,尝试了几次,所得出的令人信服的结果要少于这些。

Thank you so much for taking a look. 非常感谢您的关注。

PROBLEM SOLVED THANKS TO HELP BELOW... 问题已解决,可以帮助...

running suggested code from below 从下面运行建议的代码

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

gives... 给...

在此处输入图片说明

which is so very almost the solution I'm after, but with one final addition... like so 这几乎是我所追求的解决方案,但是最后添加了一个……

#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)

I get what I'm after... 我得到了我想要的... 在此处输入图片说明

The reason I set the data again is so that it only fills the lowest 20% of the two regions. 我再次设置数据的原因是,它仅填充了两个区域中最低的20%。

Thank you so much for the help :-) 非常感谢你的帮助 :-)

Looks like you're thinking about it in the right way. 看起来您正在以正确的方式考虑它。 With geom_ribbon i dont think you need to set data to anything else. 有了geom_ribbon我认为您无需将数据设置为其他任何内容。 Just set aes(ymin = 0, ymax = ecdf, fill = region) . 只需设置aes(ymin = 0, ymax = ecdf, fill = region) I think that should do it. 我认为应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM