Plotting temporal data day and hour together

112 views Asked by At

I have a frame with temperature data and the time. It looks like this:

area day hour temperature
d1 09_11_2013 01H0min0s 18,723
d1 09_11_2013 04H0min0s 17,558
d1 09_11_2013 07H0min0s 16,963
d1 09_11_2013 10H0min0s 22,753
d1 09_11_2013 13H0min0s 28,468
d1 10_11_2013 01H0min0s 21,318
d1 10_11_2013 04H0min0s 20,126
d1 10_11_2013 07H0min0s 19,151
d1 10_11_2013 10H0min0s 25,404
d1 10_11_2013 13H0min0s 29,890
i1 09_11_2013 01H0min0s 18,485
i1 09_11_2013 04H0min0s 17,368
i1 09_11_2013 07H0min0s 16,844
i1 09_11_2013 10H0min0s 24,171
i1 09_11_2013 13H0min0s 28,072
i1 10_11_2013 01H0min0s 21,246
i1 10_11_2013 04H0min0s 20,103
i1 10_11_2013 07H0min0s 19,151
i1 10_11_2013 10H0min0s 27,087
i1 10_11_2013 13H0min0s 28,518

In my example I have two areas and five temperatures per hour per day. I use this function (package ggplot2) but I would like to plot the column hour as well.

ggplot(data, aes(x=day, y=temperature, colour=area, group=area)) + geom_point()
1

There are 1 answers

0
user20650 On

I would create a time element that combines the date and hour. You can then use this as the x-axis, and use scale_x_datetime for more control over the breals and labels.

# Your data
dat <- read.table(header=T, text=
"area  day  hour    temperature
d1  09_11_2013  01H0min0s   18,723
d1  09_11_2013  04H0min0s   17,558
d1  09_11_2013  07H0min0s   16,963
d1  09_11_2013  10H0min0s   22,753
d1  09_11_2013  13H0min0s   28,468
d1  10_11_2013  01H0min0s   21,318
d1  10_11_2013  04H0min0s   20,126
d1  10_11_2013  07H0min0s   19,151
d1  10_11_2013  10H0min0s   25,404
d1  10_11_2013  13H0min0s   29,890
i1  09_11_2013  01H0min0s   18,485
i1  09_11_2013  04H0min0s   17,368
i1  09_11_2013  07H0min0s   16,844
i1  09_11_2013  10H0min0s   24,171
i1  09_11_2013  13H0min0s   28,072
i1  10_11_2013  01H0min0s   21,246
i1  10_11_2013  04H0min0s   20,103
i1  10_11_2013  07H0min0s   19,151
i1  10_11_2013  10H0min0s   27,087
i1  10_11_2013  13H0min0s   28,518", stringsAsFactors=F, dec=",")

# Create a data-time vector
dat$time <- as.POSIXct(
              paste(dat$day, gsub("[^0-9]+", ":", dat$hour)),
              format="%d_%m_%Y %H:%M:%S")

# Plot
library(ggplot2)

ggplot(dat, aes(x=time, y=temperature, colour=area)) + 
           geom_point() +
           theme(axis.text.x=element_text(size=10))

enter image description here