我有一个每月的天气数据集,我想绘制一个折线图。
我的数据集在这里:

weather.data2:

    date         mtemp       mrh         ah1         ah2        vaporpressure
1   31/01/2008  15.95161    74.96774    10.463958   10.376739   12.60586
2   29/02/2008  13.32759    71.96552    8.506296    8.457573    10.32157
3   31/03/2008  19.98065    76.00000    13.461108   13.301972   16.07004
4   30/04/2008  23.06667    85.06667    17.884817   17.612111   21.20251
5   31/05/2008  25.34194    82.96774    19.904886   19.548480   23.47831
6   30/06/2008  26.67000    88.13333    22.655861   22.217597   26.65403
7   31/07/2008  28.37097    82.16129    23.216533   22.715155   27.21262
8   31/08/2008  28.38387    79.45161    22.520920   22.034029   26.39536
9   30/09/2008  28.96667    74.56667    21.834234   21.345684   25.55925
10  31/10/2008  26.50000    77.03226    19.685226   19.308482   23.16607
11  30/11/2008  21.94667    65.33333    13.473522   13.271739   15.98306
12  31/12/2008  18.43548    63.38710    10.184461   10.081156   12.20581
13  31/01/2009  15.32258    63.87097    8.663397    8.597653    10.45324
14  28/02/2009  20.51071    81.28571    14.778456   14.596660   17.62418
15  31/03/2009  19.69032    83.09677    14.448571   14.280276   17.25859
16  30/04/2009  22.02333    77.13333    15.350085   15.134001   18.23880
17  31/05/2009  25.53548    78.29032    19.013323   18.669040   22.41749
18  30/06/2009  28.14333    81.36667    22.795169   22.309445   26.72967
19  31/07/2009  29.04839    80.77419    23.784844   23.249975   27.83724
20  31/08/2009  29.43226    79.96774    24.035433   23.482366   28.10789
21  30/09/2009  28.82667    78.46667    22.788483   22.282172   26.68366
22  31/10/2009  26.16774    73.06452    18.258184   17.917379   21.50479
23  30/11/2009  20.48000    72.20000    13.498049   13.315853   16.06684
24  31/12/2009  17.31290    78.06452    11.815604   11.705578   14.19231

这是我的情节:
weather.data2$date=as.Date(as.character(weather.data$date),format="%d/%m/%Y")
windows(width=7*1.5,height=12/2)
par(mar=c(4,4,2,5))
plot(weather.data2$date,weather.data2$ah1,ylim=c(-2,30),type='l',col="blue", xlab="month", ylab=NA)
par(new=TRUE)
plot(weather.data2$date,weather.data2$ah2,ylim=c(-2,30),type='l',col="green", xlab="", ylab=NA)
par(new=TRUE)
plot(weather.data2$date,weather.data2$mtemp,ylim=c(-2,30),type='l',col="red", xlab="", ylab=NA)
par(new=TRUE)
plot(weather.data2$date,weather.data2$mrh,ylim=c(-2,100),type='l',col="orange", axes=F, xlab=NA, ylab=NA)
axis(side=4)
mtext(side=4,line=3,"Relative Humiditiy (%)")
par(new=TRUE)
plot(weather.data2$date,weather.data2$vaporpressure,ylim=c(-2,30),type='l',col="steelblue", xlab="", ylab=NA)
mtext(side=2,line=3,"Temperature (C)/Vapour Pressure (mb)/Absolute humidity(g/m^3)")

legend("bottomright", c("Relative Humidiity","Temperature","Vapour Pressure","Absolute Humidity 1","Absolute Humidity 2"),lty=1,col = c("orange","red","steelblue","blue","green"),bty="n")
legend("bottomleft",c("Household Contact Enrollment Date"),pch=19,col=c("red"),bty="n")

但是当我绘制它时,它看起来像这样...

我希望它看起来像这样,而不是完全一样(这是一条拟合到每日平均值的黄土回归量,这就是为什么我计算每月平均值,以便希望它看起来比下面的平均值更好)

最佳答案

以下内容对您有帮助吗?

x$date=as.Date(x$date, format='%d/%m/%Y')
library(reshape2)
library(ggplot2)
x=melt(x,id='date',value.name='VALUE',variable.name='FACTOR')
x$VALUE=as.numeric(x$VALUE)
ggplot(x, aes(date, VALUE, group=FACTOR, color=FACTOR))+geom_line()

使用ggplot2,您不能有两个轴,但是可以进行切面并改善其外观。这是正确的方向吗?

关于r - 在R中绘制每月天气数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25710111/

10-12 19:52