在R中绘制月天气数据

在R中绘制月天气数据,r,date,plot,curve,R,Date,Plot,Curve,我有一个每月的天气数据集,我想画一个折线图。 我的数据集在这里: 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

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

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,您不能有两个轴,但可以进行面切割并改善其外观。这是正确的方向吗?

无法重现。
str(weather.data2)
显示了什么?如果所有5个数字列都定义为
num
,并且第一个是class
Date
,那么它将给出您所需的第二个图表。也许你的日期栏是一个因素?@wlondr我无法复制。它为我画了彩色的线条。我确实在您的行`
weather.data2$date=as.date(as.character(weather.data$date),format=“%d/%m/%Y”)
中看到,您同时引用了
weather.data2
weather.data
,后者您没有提供,因此我将其改为
weather.data2
。这是你的问题吗?验证
类(weather.data2$date)==“date”
。您的第一个绘图看起来仍然是因素。请添加数据摘要(
摘要(weather.data2)
)。也许这会帮助我们理解数据类型是否有问题。如果对数据集名称执行str(),您可能会看到您的temp变量实际上是一个因子。