根据因子级别着色ggplot2背景

时间:2016-07-22 21:32:49

标签: r ggplot2

我有一个随时间变化的数据集(Tb和Ta)(日期)。看起来像这样:

`                Date     Tb     Ta    Light
1 2015-02-15 01:13:00 36.103 22.751 nightime
2 2015-02-15 01:55:00 36.103 22.626 nightime
3 2015-02-15 02:37:00 35.605 22.626 nightime
4 2015-02-15 03:19:00 35.605 22.751 nightime
5 2015-02-15 04:01:00 36.103 23.001 nightime
6 2015-02-15 04:43:00 35.605 22.876 nightime`

我正在尝试使用不同阴影来绘制因子' Light'中的关卡。所以所有的点都是' nightime'在' Light'在白天'白天'会是白色的。像这样:

有没有办法让geom_rect()使用因子级别?我需要所有的点编码为' nightime'带有灰色背景的阴影......

我根据Using ggplot2 in R, how do I make the background of a graph different colours in different regions?

尝试了以下内容
ggplot() + geom_rect(data=tbdf, (aes(xmin=Date, 
xmax=Date, ymin=min(Tb), ymax=max(Tb), fill=Light))) +
  geom_line(data = tbdf, aes(x = Date, y = Tb)) +
  geom_line(data = tbdf, aes(x = Date, y = Ta), colour='grey')  +
  xlab('Date') +
  ylab('Temperature (°C)')

最终会出现Light的图例,但仍然是常见的灰色阴影:

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

由于缺乏样本数据,我创建了一些。

library(ggplot2)
library(dplyr)
daytime <- rep(rep(c("day", "night"), each = 12),10)
df <- data.frame(date = 1:length(daytime), daytime, value = rnorm(length(daytime)))

> head(df)
               date  daytime  value
1                  1     day -0.7016900
2                  2     day -0.5886091
3                  3     day -0.1962264
4                  4     day  1.3621115
5                  5     day -1.5810459
6                  6     day -0.6598885

然后我确定了每天和每晚的开始和结束。对于样本数据,这不是必需的,但我猜真实的数据并不那么简单。

period <- case_when(daytime != lead(daytime) & daytime == "day" ~ "endDay",
               daytime != lead(daytime) & daytime == "night" ~ "endNight",
               daytime != lag(daytime) & daytime == "day" ~ "beginDay",
               daytime != lag(daytime) & daytime == "night" ~ "beginNight")
period[1]              <- "beginDay"
period[length(period)] <- "endNight"

结合两者:

rect <- cbind(df,period)
names(rect)[1] <- "date"

并创建2个数据框,一个用于夜晚,一个用于白天,每个周期都有相应的x值。

rect_night <- na.omit(rect[rect$daytime == "night", ])[ ,-2:-3]
rect_night <- data.frame(start = rect_night[rect_night$period == "beginNight", 1], 
                         end  = rect_night[rect_night$period == "endNight", 1])

rect_day <- na.omit(rect[rect$daytime == "day", ])
rect_day <- data.frame(start = rect_day[rect_day$period == "beginDay", 1], 
                         end  = rect_day[rect_day$period == "endDay", 1])

将所有内容放在一个情节中。

ggplot(alpha = 0.3) +
  geom_rect(data = rect_night,aes(xmin = start, xmax = end, ymin = -5, ymax = 5), fill = "grey") +
  geom_rect(data = rect_day,aes(xmin = start, xmax = end, ymin = -5, ymax = 5),  fill = "yellow") + 
  geom_line(data = df, aes(x = date, y = value))

enter image description here

答案 1 :(得分:1)

似乎是Date格式的一个问题。将它从2015-02-15 01:13:00格式更改为数字(例如42050.05)效果很好。这是我使用的代码

ggplot() + geom_rect(data=tbdf, (aes(xmin=Date-0.5, xmax=Date+0.5, 
ymin=min(Ta)-0.5, ymax=max(Tb)+0.5, fill=factor(Light)))) +
scale_fill_manual(values=c("grey", "white"), guide=FALSE) +
geom_line(data = tbdf, aes(x = Date, y = Tb), size=0.8) +
geom_line(data = tbdf, aes(x = Date, y = Ta), colour='grey50', size=0.8)  +
ylab('Temperature (°C)')

这给了我这个 enter image description here

很好地清理。