ggplot2:如何在参考线上方和下方获取不同颜色的色带 [英] ggplot2: How to get different color ribbons above and below a reference line
问题描述
我有一个数据来制作一个 geom_ribbon
,然后是一个 geom_line
。现在,我想使红线上方的功能区的部分颜色不同。
h2>
剧情资料:
#simulate data
dat< - data.frame(1:12)
colnames(dat)< -c(code)
dat $ code< - month.abb
dat $ code< ; - 因子(dat $ code,levels = dat $ code)
dat $ Th <-c(42,44,53,64,75,83,87,84,78,67,55,45) (27,28,35,44,54,63,68,66,59,48,38,29)
dat $ prec <-c(3.03, 2.48,3.23,3.15,4.13,3.23,4.13,4.88,3.82,3.07,2.83,2.8)
dat $ prec < - dat $ prec * 16
dat
#plot
ggplot(data = dat,aes(x = code,ymin = Tl,ymax = Th))+
geom_ribbon(group = 1)+
geom_line(data = dat,aes (x = code,y = prec,group = 2),color =red,size = 3)+
expand_限制(y = 0)
创建两个功能区, T1
和 prec
之间的一个和 prec $ c $之间的一个c>和
Th
。在每种情况下,我们还需要解决 prec
分别低于或高于 Th
和 Tl
:
ggplot(dat,aes(x = code))+
geom_ribbon(aes(ymin = pmin(pmax(prec,Tl),Th),ymax = Th,group = 1),fill =blue)+
geom_ribbon ymax = pmax(pmin(prec,Th),T1),group = 2),fill =green)+
geom_line(aes(y = prec,group = 2),color =red = 3)+
expand_limits(y = 0)
但是请注意,情节不是很正确,因为数据的水平分辨率有限:
$ b $因此,让我们创建一个新的数据框架,在每个月之间将实际数据线性插值到一组点上,然后再次创建该图表:
dat.new = cbind.data.fram e(
code = seq(1,12,length = 200),
sapply(dat [,c(prec,Th,Tl)],function(T)approxfun dat $ code,T)(seq(1,12,length = 200)))
)
ggplot(dat.new,aes(x = code))+
geom_ribbon(aes(ymin = pmin(pmax(prec,Tl),Th),ymax = Th,group = 1),fill =blue)+
geom_ribbon(aes(ymin = T1,ymax = pmax pmin(prec,Th),T1),group = 2),fill =green)+
geom_line(aes(y = prec,group = 2),color =red,size = 3)+
expand_limits(y = 0)+
scale_x_continuous(breaks = 1:12,labels = month.abb)
I have a data to make a geom_ribbon
and then a geom_line
over it. And now, I would like to make the part of the ribbon above the red line, coloured differently.
Data data for the plot:
#simulate data
dat <- data.frame(1:12)
colnames(dat) <- c("code")
dat$code <- month.abb
dat$code <- factor(dat$code, levels = dat$code)
dat$Th <- c(42, 44, 53, 64, 75, 83, 87, 84, 78, 67, 55, 45)
dat$Tl <- c(27, 28, 35, 44, 54, 63, 68, 66, 59, 48, 38, 29)
dat$prec <- c(3.03, 2.48, 3.23, 3.15, 4.13, 3.23, 4.13, 4.88, 3.82, 3.07, 2.83, 2.8)
dat$prec <- dat$prec*16
dat
#plot
ggplot(data = dat, aes(x = code, ymin = Tl, ymax = Th)) +
geom_ribbon(group = 1) +
geom_line(data = dat, aes(x = code, y = prec, group = 2), colour = "red", size = 3) +
expand_limits(y = 0)
We can do this by creating two ribbons, one between Tl
and prec
and one between prec
and Th
. In each case, we also need to address the case where prec
is below or above, respectively, both Th
and Tl
:
ggplot(dat, aes(x = code)) +
geom_ribbon(aes(ymin=pmin(pmax(prec,Tl),Th), ymax=Th, group=1), fill="blue") +
geom_ribbon(aes(ymin=Tl, ymax=pmax(pmin(prec,Th),Tl), group=2), fill="green") +
geom_line(aes(y = prec, group = 2), colour = "red", size = 3) +
expand_limits(y = 0)
But note that the plot is not quite right because of the limited horizontal resolution of the data:
So let's create a new data frame that linearly interpolates the actual data at a bunch of points between each month and create the plot again:
dat.new = cbind.data.frame(
code=seq(1,12,length=200),
sapply(dat[,c("prec","Th","Tl")], function(T) approxfun(dat$code, T)(seq(1,12,length=200)))
)
ggplot(dat.new, aes(x = code)) +
geom_ribbon(aes(ymin=pmin(pmax(prec,Tl),Th), ymax=Th, group=1), fill="blue") +
geom_ribbon(aes(ymin=Tl, ymax=pmax(pmin(prec,Th),Tl), group=2), fill="green") +
geom_line(aes(y = prec, group = 2), colour = "red", size = 3) +
expand_limits(y = 0) +
scale_x_continuous(breaks=1:12, labels=month.abb)
这篇关于ggplot2:如何在参考线上方和下方获取不同颜色的色带的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!