在单个地块上绘制月度和年度平均值

时间:2015-06-13 12:54:26

标签: r plot ggplot2

我想绘制月度和年度温度。根据本论坛的一些例子,我收集了每月的年度数据。但是,聚合创建的数据按月和年排序。我如何从1995年1月开始在R base plot或ggplot中绘制这些数据?

tt<- rnorm(4018, 5, 8)
date<-seq(as.Date('1995-01-01'),as.Date('2005-12-31'),by = 1)
df<-data.frame(date,tt)
df$Month <- months(df$date)
df$Year <- format(df$date,format="%y")
df1<-aggregate(tt ~ Month + Year , df , mean)

1 个答案:

答案 0 :(得分:1)

在基地R中,您可以执行以下操作:

tt<- rnorm(4018, 5, 8)
date<-seq(as.Date('1995-01-01'),as.Date('2005-12-31'),by = 1)
df<-data.frame(date,tt)
df$Month <- months(df$date)
df$Year <- format(df$date,format="%y")
df1<-aggregate(tt ~ Month + Year , df , mean)

#make a date column
df1$date <- as.Date(paste('01', df1$Month, df1$Year), format='%d %B %y')

#plot tt over the dates made above
plot(df1$date, df1$tt)

enter image description here