根据数据集中的另一个变量设置背景颜色 [英] Setting background color according to another variable in dataset

查看:151
本文介绍了根据数据集中的另一个变量设置背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的数据看起来像这:

  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绘制了几​​条鸟的纬度,并根据它们所处的位置为不同颜色线的不同部分着色。为了改变背景我已经阅读过使用geom_rect在在R中使用ggplot2,我如何使图形的背景不同颜色在不同的地区?,但在这个问题上,男人只需要一堆矩形,我需要365。



有没有人知道任何其他方式做这个?如果我可以在接近分点的日期使默认背景更透明,那也是有用的。

你没有' t提供了一个这样看起来如何理想的图像,那么如何像这样:

  ggplot(data = SO)+ 
geom_rect(aes(xmin = date,xmax = date + 1,ymin = min(纬度),ymax = max(纬度),
fill = eqx.effect))+
geom_line(aes x =日期,y =纬度),颜色=黄色)


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.

解决方案

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

这篇关于根据数据集中的另一个变量设置背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆