我制作了一个气候图,在那里我用geom_ribbon
来表示,降水低于温度的地方。
但是当我在一行上定义geom_ribbon
时,它想要将它的一半连接成一个,所以它会产生灰色“线”的片段,如图所示。
你有什么想法如何摆脱这个片段?
tit <- "Name"
subtit <- "280 m a.s.l., 8.5 °C, 500 mm"
abb<- c("J","F","M","A","M","J","J","A","S","O","N","D")
magnify <- 2
dat<-as.data.frame(1:12)
dat$'1:12' <- clim$code
names(dat) <- ("code")
dat$T<-c(-4.2,-0.6,8.6,19.0,28.4,34.8,38.4,38.2,29.6,18.6,8.0,-0.8)
dat$prec<-c(26,23,31,28,60,72,61,40,10,20,36,35)
dat$code <- factor(1:12, levels = 1:12)
dat$T <- dat$T * magnify
head(dat)
dat.new = cbind.data.frame(
code=seq(1,12,length=200),
sapply(dat[,c("prec","T")], function(T) approxfun(dat$code, T)(seq(1,12,length=200)))
)
#subsetting data
cut_max <- (dat.new$prec>=dat.new$T)
cut_min <- (dat.new$prec<=dat.new$T)
sub_max <- dat.new[cut_max, ]
sub_min <- dat.new[cut_min, ]
绘图:
#plotting
library(ggplot2)
ggplot(dat.new, aes(x = code)) +
geom_ribbon(data = sub_max, aes(ymin = T, ymax = prec), alpha = 0.5, fill = "grey25") +
#geom_ribbon(data = sub_min, aes(ymin = T, ymax = prec), fill = "red", alpha = 0.2) +
geom_line(aes(y = prec, group = 1), colour = "blue", size = 1.5, alpha = 1) +
geom_line(aes(y = T, group = 1), colour = "red", size = 1.5, alpha = 1) +
expand_limits(y = 0, ymax = 200) +
geom_line(aes(y = 0, group = 1), size = 0.5) +
theme_bw() +
theme(plot.title = element_text(lineheight=.8, face="bold"))+
scale_y_continuous(name = 'Precipitation (mm)',
sec.axis = sec_axis(trans = ~ . * (1/magnify), name = 'Temperature (°C)')) +
theme(legend.position = "none",
panel.grid.minor.y = element_blank(),
panel.grid.minor.x = element_blank(),
axis.title.x = element_blank(),
text=element_text(size=16, family="Comic Sans MS"))+
scale_x_continuous(breaks=1:12, labels=abb)+
labs(title = tit, subtitle = subtit)
答案 0 :(得分:1)
您可以设置&#34;群组&#34;这将绘制在一起,通过设置group
美学,将它们与其他组的成员分开。通常,这用于为单独的单元/个体制作许多相同的线(或类似的)。在您的情况下,您希望它适用于您尝试绘制的图形的单独部分。
由于您的数据没有任何自然组,因此您需要创建它们。在这里,我使用dplyr
对数据进行一些操作,以创建将在其中包含组的sub_max
的新版本。首先,我创建了一个列,说明我们是否保留该行。接下来,我设置一个标志,以显示我们要突出显示的部分何时开始,以及我们不的下一部分何时要突出显示开始。然后,我根据该标志设置组(以便一个部分中的所有行共享相同的组号。最后,我只保留我们想要突出显示的行。
newSubMax <-
dat.new %>%
mutate(toKeep = prec > T
, changed = toKeep != lag(toKeep, default = toKeep[1])
, group = cumsum(changed)) %>%
filter(toKeep)
现在,我们只需将您的旧geom_ribbon
替换为新数据,然后将group = group
添加到aes
映射中。现在就是这条线:
geom_ribbon(data = newSubMax
, aes(ymin = T
, ymax = prec
, group = group)
, alpha = 0.5, fill = "grey25")
整个情节代码是:
ggplot(dat.new, aes(x = code)) +
geom_ribbon(data = newSubMax
, aes(ymin = T
, ymax = prec
, group = group)
, alpha = 0.5, fill = "grey25") +
#geom_ribbon(data = sub_min, aes(ymin = T, ymax = prec), fill = "red", alpha = 0.2) +
geom_line(aes(y = prec, group = 1), colour = "blue", size = 1.5, alpha = 1) +
geom_line(aes(y = T, group = 1), colour = "red", size = 1.5, alpha = 1) +
expand_limits(y = 0, ymax = 200) +
geom_line(aes(y = 0, group = 1), size = 0.5) +
theme_bw() +
theme(plot.title = element_text(lineheight=.8, face="bold"))+
scale_y_continuous(name = 'Precipitation (mm)',
sec.axis = sec_axis(trans = ~ . * (1/magnify), name = 'Temperature (°C)')) +
theme(legend.position = "none",
panel.grid.minor.y = element_blank(),
panel.grid.minor.x = element_blank(),
axis.title.x = element_blank(),
text=element_text(size=16, family="Comic Sans MS"))+
scale_x_continuous(breaks=1:12, labels=abb)+
labs(title = tit, subtitle = subtit)
给