R ggplot2绘制每小时的数据

问题描述:

我想绘制ggplot2来自气象站的小时气象数据(这是我第一次使用ggplot)。我已设法绘制日常数据,但在缩减为小时数据时遇到了一些问题。数据文件看起来像:R ggplot2绘制每小时的数据

FECHA H_SOLAR;DIR_M;VEL_M;TEMP_M;HR;PRECIP 
01/06/14 00:50:00;314.3;1.9;14.1;68.0;-99.9 
01/06/14 01:50:00;322.0;1.6;13.3;68.9;-99.9 
01/06/14 02:50:00;303.5;2.1;12.3;70.9;-99.9 
01/06/14 03:50:00;302.4;1.6;11.6;73.1;-99.9 
01/06/14 04:50:00;306.5;1.2;10.9;76.4;-99.9 
01/06/14 05:50:00;317.1;0.8;12.6;71.5;-99.9 
01/06/14 06:50:00;341.8;0.0;17.1;58.8;-99.9 
01/06/14 07:50:00;264.6;1.2;21.8;44.9;-99.9 
01/06/14 08:50:00;253.8;2.9;24.7;32.2;-99.9 
01/06/14 09:50:00;254.6;3.7;26.7;27.7;-99.9 
01/06/14 10:50:00;250.7;4.3;28.3;24.9;-99.9 
01/06/14 11:50:00;248.5;5.3;29.1;22.6;-99.9 
01/06/14 12:50:00;242.8;4.7;30.3;20.4;-99.9 
01/06/14 13:50:00;260.7;4.9;31.3;17.4;-99.9 
01/06/14 14:50:00;251.8;5.1;31.9;17.1;-99.9 
01/06/14 15:50:00;258.1;4.6;32.4;15.3;-99.9 
01/06/14 16:50:00;254.3;5.7;32.4;14.0;-99.9 
01/06/14 17:50:00;252.5;4.6;32.0;14.1;-99.9 
01/06/14 18:50:00;257.4;3.8;31.1;14.9;-99.9 
01/06/14 19:50:00;135.8;4.2;26.0;41.2;-99.9 
01/06/14 20:50:00;126.0;1.7;23.5;48.7;-99.9 
01/06/14 21:50:00;302.8;0.7;21.6;53.9;-99.9 
01/06/14 22:50:00;294.2;1.1;19.3;67.4;-99.9 
01/06/14 23:50:00;308.5;1.0;17.5;72.4;-99.9 

我已经使用这个R命令绘制数据:

datos=read.csv("utiel.dat",sep=";",header=T,na.strings="-99.9") 

dia=as.Date(datos[,1],"%y/%m/%d")  # Crear índice dia 
veloc=zoo(datos[,c("VEL_M")],dia)  

gveloc=ggplot(data=datos,aes(dia,veloc)) 

gveloc + geom_point(colour="blue",cex=1) + ylab("Velocidad (km/h)") + xlab("Fecha") + opts(title="Velocidad media horaria") + scale_x_date(limits = as.Date(c("2007-01-01","2007-01-31")),format = "%Y-%m-%d") 

,并得到了与从单一的一天在同一x坐标的所有数据这个月图(即同一天,因为它可以预期)

Hourly wind data with ggplot2

我如何管理阅读不仅是一天,但使每个点都可以被绘制在它的时间自己的x /时间坐标?我觉得之前绘制的问题开始,但我怎么也找不到读取日期YY/MM/DD H:M:S

在此先感谢

解决方案:只是一个除了把代码工作我

datos$dia=as.POSIXct(datos[,1], format="%y/%m/%d %H:%M:%S") # Read date/time as POSIXct 

ggplot(data=datos,aes(x=dia, y=TEMP_M)) + 
    geom_point(colour="red") + 
    ylab("Temperatura (ºC)") + 
    xlab("Fecha") + 
    opts(title="Temperatura media") + 
    scale_x_datetime(limits=c(as.POSIXct('2008/02/01'), as.POSIXct('2008/02/02')) ,format = "%Y-%m-%d") 

希望它可以帮助别人,感谢Andrie和G.Grothendieck

+0

嗨,我看不到你的满意 – pacomet

as.Date只捕获日期元素。要捕获的时候,你需要使用as.POSIXct

重建数据:

zz <- tempfile() 
cat(" 
FECHA H_SOLAR;DIR_M;VEL_M;TEMP_M;HR;PRECIP 
01/06/14 00:50:00;314.3;1.9;14.1;68.0;-99.9 
01/06/14 01:50:00;322.0;1.6;13.3;68.9;-99.9 
01/06/14 02:50:00;303.5;2.1;12.3;70.9;-99.9 
01/06/14 03:50:00;302.4;1.6;11.6;73.1;-99.9 
01/06/14 04:50:00;306.5;1.2;10.9;76.4;-99.9 
01/06/14 05:50:00;317.1;0.8;12.6;71.5;-99.9 
01/06/14 06:50:00;341.8;0.0;17.1;58.8;-99.9 
01/06/14 07:50:00;264.6;1.2;21.8;44.9;-99.9 
01/06/14 08:50:00;253.8;2.9;24.7;32.2;-99.9 
01/06/14 09:50:00;254.6;3.7;26.7;27.7;-99.9 
01/06/14 10:50:00;250.7;4.3;28.3;24.9;-99.9 
01/06/14 11:50:00;248.5;5.3;29.1;22.6;-99.9 
01/06/14 12:50:00;242.8;4.7;30.3;20.4;-99.9 
01/06/14 13:50:00;260.7;4.9;31.3;17.4;-99.9 
01/06/14 14:50:00;251.8;5.1;31.9;17.1;-99.9 
01/06/14 15:50:00;258.1;4.6;32.4;15.3;-99.9 
01/06/14 16:50:00;254.3;5.7;32.4;14.0;-99.9 
01/06/14 17:50:00;252.5;4.6;32.0;14.1;-99.9 
01/06/14 18:50:00;257.4;3.8;31.1;14.9;-99.9 
01/06/14 19:50:00;135.8;4.2;26.0;41.2;-99.9 
01/06/14 20:50:00;126.0;1.7;23.5;48.7;-99.9 
01/06/14 21:50:00;302.8;0.7;21.6;53.9;-99.9 
01/06/14 22:50:00;294.2;1.1;19.3;67.4;-99.9 
01/06/14 23:50:00;308.5;1.0;17.5;72.4;-99.9 
", file=zz) 

datos=read.csv(zz, sep=";", header=TRUE, na.strings="-99.9") 

转换日期POSIXct和打印:

library(ggplot2) 

datos=read.csv(zz, sep=";", header=TRUE, na.strings="-99.9") 

datos$dia=as.POSIXct(datos[,1], format="%y/%m/%d %H:%M:%S") 

ggplot(data=datos,aes(x=dia, y=TEMP_M)) + 
    geom_path(colour="red") + 
    ylab("Temperatura (ºC)") + 
    xlab("Fecha") + 
    opts(title="Temperatura media") 

enter image description here

+0

好的回答安德里,谢谢。对于我发布的每日数据,它工作正常,但重点是我的数据跨越了十年。然后我需要设置set_x_scale选项来选择我想要绘制的时间段(一天到几个月)。我无法重现一个月的图表。是否有必要重新创建数据? – pacomet

+0

您必须将规模的限制指定为“POSIXct”日期,而不是“日期”。 – Andrie

+0

我应该如何将限制设置为POSIXct?可能类似于:'scale_x_date(limits = as.POSIXct(c(“2004-01-01”,“2005-01-01”)), format =“%Y-%m-%d”)'??谢谢 – pacomet

由于您使用的动物园我们可以使用read.zoo设置数据,z,然后使用对其进行绘图,xyplot.zoo或ggplot2的qplot。我们在下面显示全部三个。

Lines <- "FECHA H_SOLAR;DIR_M;VEL_M;TEMP_M;HR;PRECIP 
01/06/14 00:50:00;314.3;1.9;14.1;68.0;-99.9 
01/06/14 01:50:00;322.0;1.6;13.3;68.9;-99.9 
01/06/14 02:50:00;303.5;2.1;12.3;70.9;-99.9 
01/06/14 03:50:00;302.4;1.6;11.6;73.1;-99.9 
01/06/14 04:50:00;306.5;1.2;10.9;76.4;-99.9 
01/06/14 05:50:00;317.1;0.8;12.6;71.5;-99.9 
01/06/14 06:50:00;341.8;0.0;17.1;58.8;-99.9 
01/06/14 07:50:00;264.6;1.2;21.8;44.9;-99.9 
01/06/14 08:50:00;253.8;2.9;24.7;32.2;-99.9 
01/06/14 09:50:00;254.6;3.7;26.7;27.7;-99.9 
01/06/14 10:50:00;250.7;4.3;28.3;24.9;-99.9 
01/06/14 11:50:00;248.5;5.3;29.1;22.6;-99.9 
01/06/14 12:50:00;242.8;4.7;30.3;20.4;-99.9 
01/06/14 13:50:00;260.7;4.9;31.3;17.4;-99.9 
01/06/14 14:50:00;251.8;5.1;31.9;17.1;-99.9 
01/06/14 15:50:00;258.1;4.6;32.4;15.3;-99.9 
01/06/14 16:50:00;254.3;5.7;32.4;14.0;-99.9 
01/06/14 17:50:00;252.5;4.6;32.0;14.1;-99.9 
01/06/14 18:50:00;257.4;3.8;31.1;14.9;-99.9 
01/06/14 19:50:00;135.8;4.2;26.0;41.2;-99.9 
01/06/14 20:50:00;126.0;1.7;23.5;48.7;-99.9 
01/06/14 21:50:00;302.8;0.7;21.6;53.9;-99.9 
01/06/14 22:50:00;294.2;1.1;19.3;67.4;-99.9 
01/06/14 23:50:00;308.5;1.0;17.5;72.4;-99.9" 
cat(Lines, "\n", file = "data.txt") 

library(zoo) 
z <- read.zoo("data.txt", header = TRUE, sep = ";", na.strings = "-99.9", 
     tz = "", format = "%y/%m/%d %H:%M:%S") 
# move last 12 points into following day 
time(z)[13:24] <- time(z)[13:24] + 24 * 60 * 60 
xlim <- as.POSIXct(c("2001-06-14 00:00:00", "2001-06-14 12:00:00")) 

这里有3种方式绘制它:

1 - 经典的图形使用plot.zoo

# Create manual axis since classic graphic's default is not so good. 
# Axis might be ok for real data in which case manual axis setting can be omitted 
plot(z$VEL_M, type = "p", xlab = "X", ylab = "Y", col = "blue", xlim = xlim, 
    xaxt = "n") 
xaxis <- seq(xlim[1], xlim[2], by = "hour") 
axis(1, xaxis, as.POSIXlt(xaxis)$hour) 

2 - 点阵图形使用xyplot.zoo

library(lattice) 
xyplot(z$VEL_M, type = "p", xlab = "X", ylab = "Y", col = "blue", xlim = xlim) 

3 - GGPLOT2使用qplot

# unlike classic graphics and lattice graphics, zoo currently 
# does not have a specific interface but we can do this: 
library(ggplot2) 
qplot(time(z), z$VEL_M, xlab = "X", ylab = "Y") + 
    geom_point(colour = "blue") + 
    scale_x_datetime(limits = xlim) 

编辑:

人为了说明限制X轴变形例。

+0

嗨,谢谢你的回答。我已经尝试了ggplot2选项,并且对于整个数据集都正常工作,但在尝试设置x轴限制时,我无法生成图形。我尝试'scale_x_date(limits = as.Date(c(“2007-01-01”,“2007-01-31”)),format =“%Y-%m-%d”)'然后不出现任何点,x限制是明确的,但任何数据。 R表示“删除了包含缺失值(geom_point)的105192行”。任何想法? – pacomet

+0

嗨,再次。我已经接受了其他答案,因为它帮助我了解了POSIXct日期和时间格式购买你的产品也很有帮助。 – pacomet

+1

@pacomet。修改了示例来说明限制X轴。 –