Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在数据框中绘制具有多个表示日期的变量的时间序列图_R_Date_Time Series_Lubridate - Fatal编程技术网

R 在数据框中绘制具有多个表示日期的变量的时间序列图

R 在数据框中绘制具有多个表示日期的变量的时间序列图,r,date,time-series,lubridate,R,Date,Time Series,Lubridate,我试图绘制一个时间序列类型图,其中日期在x轴上,平均温度值在y轴上。我使用的数据框有两个单独的变量表示日期: year包含整数值,例如1941、1942等 month是一个因子变量,有12个级别包含“一月”、“二月”、“十二月” 这是一个使用dput的数据集的剪辑,很抱歉,我不知道如何格式化才能在这里查看它 结构(列表年=c(1941L,1941L,1942L,1942L,1942L,1942L),月=c(11L, 12升、1升、2升、3升),标签=c(“一月”、“二月”、“三月”, “四月”、

我试图绘制一个时间序列类型图,其中日期在x轴上,平均温度值在y轴上。我使用的数据框有两个单独的变量表示日期:

year
包含整数值,例如1941、1942等
month
是一个因子变量,有12个级别包含“一月”、“二月”、“十二月”

这是一个使用dput的数据集的剪辑,很抱歉,我不知道如何格式化才能在这里查看它

结构(列表年=c(1941L,1941L,1942L,1942L,1942L,1942L),月=c(11L, 12升、1升、2升、3升),标签=c(“一月”、“二月”、“三月”, “四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”, “11月”、“12月”,class=“factor”)表示=c(6.9,6.5, 4.3,2.9,6.3),row.names=c(NA,5L),class=c(“天气数据”, “data.frame”)) 到目前为止,我已经尝试将
year
month
转换为字符变量,将两个字符变量粘贴到单个字符变量
yrmonth
,然后将此变量从字符类型转换为日期类型,以便
plot()
函数能够以正确的格式打印日期

#将整数和因子变量转换为字符变量

df$month假设您使用lubridate软件包,那么构建日期要简单得多:

df$date = paste(rep(15, nrow(df)), df$month, df$year, sep="/")
# check that date column contains valid dates as strings
df$date = dmy(df$date)
# check that dates have been converted correctly

为了获得有效日期,我将日期设置为每个月的15号。

假设您使用lubridate软件包,那么构建日期要简单得多:

df$date = paste(rep(15, nrow(df)), df$month, df$year, sep="/")
# check that date column contains valid dates as strings
df$date = dmy(df$date)
# check that dates have been converted correctly
为了获得有效日期,我将日期设置为每月15日