根据数据集中的另一个变量设置背景颜色

时间:2014-09-28 22:26:38

标签: r background ggplot2

我试图展示昼夜平分点对我的数据的影响,一系列远程跟踪的鸟类整整一年。为了做到这一点,我想绘制一年中每一天的纬度,并在背景中将昼夜平分点效果作为颜色分级。

我的数据看起来像这样:

SO <- data.frame(date = seq(as.Date("2000/1/1"), by = "day", length.out = 365),
                 latitude = cumsum(rnorm(365)),
                 eqx.effect = c(rep(0,60),seq(1,20,1), seq(20,1,-1),rep(0,143),seq(1,20,1), seq(20,1,-1),rep(0,82)),
                 location = c(rep(1,100),rep(2,135), rep(3,130)))     

到目前为止,我已经设法绘制了全年的几个鸟类的纬度,其中geom_line和颜色根据它们所处的位置颜色的不同部分。为了改变背景,我已经阅读了Using ggplot2 in R, how do I make the background of a graph different colours in different regions?中geom_rect的使用,但在那个问题中,该人只需要一堆矩形,我需要365个。

有谁知道这样做的其他方式?如果我可以在接近昼夜平分点的日期使默认背景更透明,那么它也会很有用。

1 个答案:

答案 0 :(得分:3)

你没有提供一个理想的外观图像,所以如下:

ggplot(data=SO) +
   geom_rect(aes(xmin=date,xmax=date+1,ymin=min(latitude),ymax=max(latitude), 
      fill=eqx.effect)) +
   geom_line(aes(x=date,y=latitude),color="yellow")

enter image description here