ggplot2:由一行定义的更多单独的色带

时间:2017-01-16 17:56:09

标签: r ggplot2

我制作了一个气候图,在那里我用geom_ribbon来表示,降水低于温度的地方。

但是当我在一行上定义geom_ribbon时,它想要将它的一半连接成一个,所以它会产生灰色“线”的片段,如图所示。

你有什么想法如何摆脱这个片段?

enter image description here

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)

1 个答案:

答案 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)

enter image description here