João Martins
João Martins

Reputation: 25

plotting temperature Vs time in R

I would like to plot the semi-hourly temperatures of 3 places into one plot. This is how the files look like (this is a part of Noble2.csv; the other files are Noble3.csv and Noble6.csv):

    Time    Temp
1   08/20/14 02:05:02 PM    17.034
2   08/20/14 02:35:02 PM    16.749
3   08/20/14 03:05:02 PM    16.963
4   08/20/14 03:35:02 PM    16.820
5   08/20/14 04:05:02 PM    16.963
6   08/20/14 04:35:02 PM    17.153
7   08/20/14 05:05:02 PM    16.249
8   08/20/14 05:35:02 PM    15.652
9   08/20/14 06:05:02 PM    14.649
10  08/20/14 06:35:02 PM    13.906
11  08/20/14 07:05:02 PM    13.209
12  08/20/14 07:35:02 PM    12.316
13  08/20/14 08:05:02 PM    12.268
14  08/20/14 08:35:02 PM    12.243
15  08/20/14 09:05:02 PM    12.219
16  08/20/14 09:35:02 PM    12.171
17  08/20/14 10:05:02 PM    12.147
18  08/20/14 10:35:02 PM    12.122
19  08/20/14 11:05:02 PM    12.074 

Noble3.csv:

Time    Temp

1 08/20/14 02:06:59 PM 15.438 2 08/20/14 02:36:59 PM 13.882 3 08/20/14 03:06:59 PM 13.642 4 08/20/14 03:36:59 PM 13.353 5 08/20/14 04:06:59 PM 13.666 6 08/20/14 04:36:59 PM 12.074 7 08/20/14 05:06:59 PM 12.147 8 08/20/14 05:36:59 PM 12.219 9 08/20/14 06:06:59 PM 12.268 10 08/20/14 06:36:59 PM 12.292 11 08/20/14 07:06:59 PM 12.292 12 08/20/14 07:36:59 PM 12.268 13 08/20/14 08:06:59 PM 12.268 14 08/20/14 08:36:59 PM 12.243 15 08/20/14 09:06:59 PM 12.219 16 08/20/14 09:36:59 PM 12.171 17 08/20/14 10:06:59 PM 12.147 18 08/20/14 10:36:59 PM 12.122 19 08/20/14 11:06:59 PM 12.098

And Noble6.csv:

Time    Temp

1 08/19/14 04:59:27 PM 12.001 2 08/19/14 05:29:27 PM 12.050 3 08/19/14 05:59:27 PM 12.122 4 08/19/14 06:29:27 PM 12.147 5 08/19/14 06:59:27 PM 12.171 6 08/19/14 07:29:27 PM 12.195 7 08/19/14 07:59:27 PM 12.219 8 08/19/14 08:29:27 PM 12.195 9 08/19/14 08:59:27 PM 12.171 10 08/19/14 09:29:27 PM 12.122 11 08/19/14 09:59:27 PM 12.074 12 08/19/14 10:29:27 PM 12.025 13 08/19/14 10:59:27 PM 11.977 14 08/19/14 11:29:27 PM 11.929 15 08/19/14 11:59:27 PM 11.880 16 08/20/14 12:29:27 AM 11.832 17 08/20/14 12:59:27 AM 11.783 18 08/20/14 01:29:27 AM 11.734 19 08/20/14 01:59:27 AM 11.662 20 08/20/14 02:29:27 AM 11.613

First, I tried to put the time column with a time format with the following commands:

> NB2<-strptime(Noble2$Time, "%m/%d/%y %I:%M:%S %p")  
> NB3<-strptime(Noble3$Time, "%m/%d/%y %I:%M:%S %p")  
> NB6<-strptime(Noble6$Time, "%m/%d/%y %I:%M:%S %p")  

then I tried to put the temperature values as a list to be able to use the function plot:

> NB2T<-Noble2$Temp  
> NB3T<-Noble3$Temp  
> NB6T<-Noble6$Temp  

just to check how it looks (before trying to put the 3 lines within one graph) I tried to plot NB2 Vs NB2T:

plot(NB2,NB2T,type="l",col="red")

this is what I got: this - which makes no sense. I should have gotten something like this (this one was made in excel).

I haven't got to the phase of adding the three lines to one graph, but could you point me into the right direction to get a correct graph first and how to add the other two lines to the graph?

Upvotes: 1

Views: 4782

Answers (3)

Metrics
Metrics

Reputation: 15458

library(ggplot2)
library(scales) # to access breaks/formatting functions
ggplot(aes(x = Time, y = Temp), data = Noble2) + geom_line()
last_plot()+scale_x_datetime(breaks = date_breaks("30 mins"))

If you want the dynamic graph, you can proceed as follows:

library(dygraphs)
library(xts)
x1<-read.csv("Noble2.csv",na.strings = c("NA", ""))
x2<-x1[complete.cases(x1),]
x2$Time<-strptime(x2$Time, "%m/%d/%y %I:%M:%S %p") 
#convert to xts object before using dygraph
x3<- xts(x2[,-1], order.by=x2[,1]) 
dygraph(x3[1:7625,]) %>% # last two observations still give the NA's (not sure why)
dyRangeSelector(height = 20)

Upvotes: 4

andybega
andybega

Reputation: 1437

Tried the first three rows of your data, plot looks fine.

Maybe the problem is with parsing the CSV files?

NB2 <- structure(list(sec = c(2, 2, 2), min = c(5L, 35L, 5L), hour = c(14L, 
 14L, 15L), mday = c(20L, 20L, 20L), mon = c(7L, 7L, 7L), year = c(114L, 
 114L, 114L), wday = c(3L, 3L, 3L), yday = c(231L, 231L, 231L), 
     isdst = c(1L, 1L, 1L), zone = c("EDT", "EDT", "EDT"), gmtoff = c(NA_integer_, 
     NA_integer_, NA_integer_)), .Names = c("sec", "min", "hour", 
 "mday", "mon", "year", "wday", "yday", "isdst", "zone", "gmtoff"
 ), class = c("POSIXlt", "POSIXt"))

NB2T <- c(17.034, 16.749, 16.963)

plot(NB2, NB2T, type="l")

enter image description here

To add the other series to your plot with base graphics, you can use lines, e.g.:

plot(NB2, NB2T, type="l")
lines(NB3, NB3T, color="red")

Or with ggplot2, e.g. look here

Upvotes: 0

Mark
Mark

Reputation: 4537

Try this:

Noble2$Time = as.POSIXct(Noble2$Time,format="%m/%d/%y %I:%M:%S %p")
plot(Temp~Time,data=Noble2,type='l',col='red')

You want to make sure you're casting your datetimes at POSIXct types, not POSIXlt types.

Upvotes: 1

Related Questions