在ggplot2中选择并绘制月份 [英] Selecting and plotting months in ggplot2
本文介绍了在ggplot2中选择并绘制月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这种格式的时间序列数据集,其中包含两列日期(例如1980年1月,1980年2月... 2013年12月)以及相应的温度。这个数据集是从1980年到2013年。我试图在ggplot中分别绘制几个月的时间序列并绘制时间序列(例如我只想要所有二月份,以便我可以使用ggplot绘制它)。尝试以下操作,但Feb1是空的
I have a time series dataset in this format with two columns date (e.g Jan 1980, Feb 1980...Dec 2013) and it's corresponding temperature. This dataset is from 1980 to 2013. I am trying to subset and plot time series in ggplot for the months separately (e.g I only want all Feb so that I can plot it using ggplot). Tried the following, but the Feb1 is empty
Feb1 <- subset(temp, date ==5)
我的数据集的结构是:
The structure of my dataset is:
'data.frame': 408 obs. of 2 variables:
$ date :Class 'yearmon' num [1:359] 1980 1980 1980 1980 1980 ...
$ temp: int 16.9 12.7 13 6 6.0 5 6 10.9 0.9 16 ...
推荐答案
这个怎么样?:
library(zoo)
# Generating some data:
df <- data.frame(date = as.yearmon("1980-01") + 0:407/12, val = rnorm(408))
# Subsetting to get a specific month:
df.sub <- subset(df, format(df$date,"%b")=="Jan")
# The actual plot:
ggplot(df.sub) + geom_line(aes(x = as.Date(date), y = val))
这篇关于在ggplot2中选择并绘制月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文