2011-10-05 145 views
4

昨天我问了一个关于使用ggplot2(R ggplot2 ploting hourly data)绘制每小时数据的问题,并找到了解决方案,这要归功于收到的答案。然后我用geom_line()成功绘制了温度数据。R ggplot2 geom_bar轴限制

我试图应用相同的geom_bar来说明与酒吧降水数据。但我无法设置y轴限制。我尝试了不同的东西,但找不到确切的点。

我的数据是这样的:

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;10 
01/06/14 10:50:00;250.7;4.3;28.3;24.9;12 
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;20 
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;16 
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 

,这是代码我想

library(ggplot2) 

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

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

datos$PRECIP[is.na(datos$PRECIP)]=0 # to avoid NA values 

ggplot(data=datos,aes(x=dia, y=PRECIP)) + geom_bar(colour="blue",stat="identity") + 
ylab("Precipitación (l)") + xlab("Hora solar") + 
opts(title="Precipitacion acumulada horaria \n 2008-05-27 Burriana") + 
scale_x_datetime(limits=xlim) + 
scale_y_continuous(limits=c(0,50)) 

当设置一样,如我所料不工作y轴的限制。如果我删除y轴设置,我并不总是获得相同的“自动”限制

这是我得到的,当我没有设置y轴的限制。我想设置限制,所以酒吧更大(垂直)。 enter image description here

在此先感谢

编辑:编辑以表明已经为我工作的代码。

感谢Andrie,克里斯和布莱恩

# Setting axis limits 
xlim = as.POSIXct(c("2008-05-27 00:00:00", "2008-05-27 23:50:00")) 
ylim = trunc(max(datos$PRECIP) + 10) 

# Ploting 
ggplot(data=datos,aes(x=dia, y=PRECIP)) + geom_bar(colour="blue",stat="identity",fill="blue") + 
ylab("Precipitación (l)") + xlab("Hora solar") + 
opts(title="Precipitacion acumulada horaria \n DIA NOMBRE") + 
scale_x_datetime(limits=xlim ,format = "%H",major='hour') + scale_y_continuous(limits=c(0,ylim)) 

现在关键是要子集PRECIP数据发现ylim。现在我正在获取整个降水量数据系列的最大值,而不是所绘制日期的最大值。我必须决定什么更好,为系列中的所有日子设置相同的y限制,或者在任何一天更改坐标轴。

再次感谢

+0

你明白了什么是什么?你希望得到? – Andrie

+1

我试过了你的代码。它不可重现,因为它无法在x轴代码中找到对象'xlim'。错误可能在那里,而不是在你的y轴限制中。据我所知,y轴限制应该工作(它在我的机器上工作)。 – Andrie

+1

我也试过你的代码(因为没有定义'xlim',所以放弃'scale_x_datetime',因为这是个问题],并且得到了一个合理的缩放(0到20)y轴。只是猜测,但我猜这只是数据的一个子集;也许你有一个极端的PRECIP值影响了规模但没有被绘制?什么是'max(datos $ PRECIP)'? –

回答

6

@Andrie是正确的。看起来你缺少xlim

使用你的代码之上,但除去scale_x_datetime

ggplot(data=datos,aes(x = dia, y = PRECIP)) + 
    geom_bar(colour = "blue",stat = "identity") + 
    ylab("Precipitación (l)") + 
    xlab("Hora solar") + 
    opts(title = "Precipitacion acumulada horaria \n 2008-05-27 Burriana") + 
    scale_y_continuous(limits = c(0,50)) 

我得到的情节: enter image description here

+1

感谢ChrisC,当我向安德里评论时,我错过了在这里写下xlim,但它已用geom_line()为我工作。也许NA值有问题...但是它们应该从PRECIP中移除。我曾尝试使用原始数据系列的不同子集,一天,一个月,一年,但无法找到y限制的确切方式。我明天会在工作中检查你的答案。 – pacomet