我正在尝试在 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%。
十分感谢你的帮助 :-)