ggplot2的时间序列:使用不同列的天数和小时数

时间:2017-03-10 10:40:24

标签: r plot ggplot2

我正在尝试使用ggplot绘制时间序列,将日期和时间存储在不同的数据框列中。我怎么能告诉ggplot考虑情节中的日期和时间?

我的数据看起来像这样

Date    Hour_min    Tair    Tflower Tbud
Day1    8:35    24,73   29,79   31,41
Day1    8:36    24,29   29,99   31,82
... ..  ..  ... ...
Day2    00:00   23,62   30,37   32,59

可以使用以下方法加载数据集的一小部分样本:

#Tagua <- read.table(file = "TIMESERIE_OTO32.txt", header = TRUE,dec = ",")
Tagua <- structure(
  list(
    Date = structure(c(1L, 1L, 2L, 2L), .Label = c("Day1", "Day2"), class = "factor"),
    Hour_min = structure(c(1L, 2L, 1L, 2L), .Label = c("8:35", "8:36"), class = "factor"),
    Tair = c(24.73, 24.29, 23.62, 24.29),
    Tflower = c(29.79, 29.99, 30.37, 29.99), 
    Tbud = c(31.41, 31.82, 32.59, 31.82)
  ),
  .Names = c("Date", "Hour_min", "Tair", "Tflower", "Tbud"),
  class = "data.frame",
  row.names = c(NA, -4L))

花的不同部分的天,小时和3温度。 我有2天1400分钟。

我写了这个剧本:

library(ggplot2)


ggplot(aes(x = (Hour_min), group=1), data = Tagua) +
  geom_line(aes(y = Tair, colour = "var1")) + 
  geom_line(aes(y = Tbud, colour = "var2")) + 
  geom_line(aes(y = Tflower, colour = "var3")) 

问题在于R从00:00到23(当然),而不考虑日期。

我该如何解决这个问题?

如果可能的话,我想设置与小时相对应的x轴刻度(例如2:00,3:00 ......)。

1 个答案:

答案 0 :(得分:0)

这可能不是最短的解决方案,但您可以一步一步地运行它,看看它是如何工作的。

library(lubridate)
library(dplyr)
library(tidyr)
library(ggplot2)

Tagua <- read.table(file = "TIMESERIE_OTO32.txt", header = TRUE, dec = ",")


Tagua_clean <- Tagua %>% 
  # Separate hours and minutes:
  separate(Hour_min, into = c("Hour", "Minute"), sep = ":") %>% 
  # Convert Day1 -> 0
  #         Day2 -> 1
  mutate(Day = as.numeric(gsub("Day", "", Date)) - 1) %>%
  # Create a Period:
  mutate(time_period = period(days = Day, hours = Hour, minutes = Minute)) %>%
  # Create a Date, using the beginning of the experiment (if you know it):
  mutate(Date = as.POSIXct("2017-01-01") + time_period) %>%
  # Option 2: Convert the time period to hours:
  mutate(Hours = as.numeric(time_period)/3600) %>%
  select(Date, Hours, Tair, Tflower, Tbud)


# Option 1: With real dates:
ggplot(aes(x = Date), data = Tagua_clean) + 
  geom_line(aes(y = Tair, colour = "var1")) +
  geom_line(aes(y = Tbud, colour = "var2"))+
  geom_line(aes(y = Tflower, colour = "var3"))


# Option 2: With hours:
ggplot(aes(x = Hours), data = Tagua_clean) + 
  geom_line(aes(y = Tair, colour = "var1")) +
  geom_line(aes(y = Tbud, colour = "var2"))+
  geom_line(aes(y = Tflower, colour = "var3"))

更新:每天将小时重新设置为0。这里我们使用日期,但我们会自定义它们的显示方式。

scale_x_datetime的参数date_labels可以设置为"%H"以显示当天的小时,或者可以设置为"Day %d \n Hour: %H"以结合日期和小时。有关更多格式选项,请参阅?strptime。如果您每小时都需要一个标签,则可以使用date_breaks来指定"1 hour"

ggplot(aes(x = Date), data = Tagua_clean) + 
  geom_line(aes(y = Tair, colour = "var1")) +
  geom_line(aes(y = Tbud, colour = "var2"))+
  geom_line(aes(y = Tflower, colour = "var3")) +
  scale_x_datetime(date_labels = "Day %d \n Hour: %H")

With Day/Hour