绘制温度Vs时间

时间:2015-02-16 15:35:13

标签: r time plot temperature

我想将3个地方的半小时温度绘制成一个地块。这就是文件的样子(这是Noble2.csv的一部分;其他文件是Noble3.csvNoble6.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

和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月8日08/19/14 08:29:27 12 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

首先,我尝试使用以下命令将时间列设置为时间格式:

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

然后我尝试将温度值作为列表,以便能够使用函数图:

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

只是为了检查它的外观(在尝试将3行放在一个图表中之前)我试图绘制NB2对NB2T:

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

这就是我得到的:this - 这没有任何意义。我应该得到像this这样的东西(这个是用excel制作的)。

我还没有完成将三行添加到一个图形的阶段,但是你能指出我正确的方向来获得正确的图形以及如何将其他两条线添加到图形中吗?

3 个答案:

答案 0 :(得分:4)

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

如果您需要dynamic图表,可以按以下步骤操作:

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)

答案 1 :(得分:1)

试试这个:

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

您希望确保在POSIXct类型而不是POSIXlt类型中投射日期时间。

答案 2 :(得分:0)

尝试了前三行数据,情节看起来不错。

问题可能在于解析CSV文件?

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

要使用基本图形将其他系列添加到绘图中,您可以使用lines,例如:

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

或使用ggplot2,例如look here