Setting background color according to another vari

2019-05-19 22:32发布

I am trying to show the effect equinoxes have on my data, a series of remotely-tracked birds for a whole year. In order to do that I want to plot latitude for each day of the year and have the equinox effect as a color grading in the background.

My data would look something like this:

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

So far I have managed to plot latitude for the whole year for several birds with geom_line and color different parts of the lines with different colors according to the location at which they are at. In order to change the background I have read of the use of geom_rect in Using ggplot2 in R, how do I make the background of a graph different colours in different regions? but in that question the man only needs a bunch of rectangles and I would need 365.

Does anyone know any other way of doing this? If I could make the default background more transparent on the dates closer to the equinoxes it would also be useful.

1条回答
可以哭但决不认输i
2楼-- · 2019-05-19 23:18

You didn't supply an image of how ideally this would look, so how about something like:

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

查看更多
登录 后发表回答