Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.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_Plot_Ggplot2_Time Series_Subset - Fatal编程技术网

在R中绘制序列(时间序列)数据的子集

在R中绘制序列(时间序列)数据的子集,r,plot,ggplot2,time-series,subset,R,Plot,Ggplot2,Time Series,Subset,我有一个类似于下面的数据帧,称为df。我想绘制这些数据的子集,从2012年5月到2014年6月。我一直在使用plot函数来绘制整个数据帧,但是,当我将其拆分为plot的子集时,无论选择数据的哪一部分,我都会得到相同的plot Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 2011 NA

我有一个类似于下面的数据帧,称为df。我想绘制这些数据的子集,从2012年5月到2014年6月。我一直在使用plot函数来绘制整个数据帧,但是,当我将其拆分为plot的子集时,无论选择数据的哪一部分,我都会得到相同的plot

     Jan       Feb       Mar       Apr       May       Jun       Jul       Aug       Sep       Oct       Nov       Dec
2011        NA        NA        NA        NA        NA        NA 13.724575 13.670017 13.782099 13.675788 13.442914 13.279969
2012 13.114463 13.021268 12.999545 12.990171 12.898018 12.611495 12.311641 12.126345 11.974871 12.019042 12.163618 12.304660
2013 12.374017 12.365795 12.323280 12.377747 12.462203 12.630298 12.780495 12.848942 12.806210 12.860463 12.838953 12.608965
2014 12.616257 12.720611 12.841626 12.897939 13.008535 13.136377 13.159393 13.290928 13.495218 13.636360 13.797778 13.827273
2015 13.662063 13.527596 13.568430 13.782818 13.889276 13.971303 14.181846 14.329937 14.385533 14.289386 14.222535 14.384618
2016 14.516674 14.759385 14.951384 14.763781 14.536779 13.978265 12.888989 11.612033 10.362592 9.205528  8.649027  8.662219
2017  8.614229  8.446361  8.239606  8.286693  8.498938  8.972903        NA        NA        NA        NA        NA        NA
我当前的代码与下面的代码类似(去掉了非基本元素,如x和y标签)


日期您需要获取数据的子集。此外,我会将数据帧格式从宽格式转换为长格式,这更易于使用,特别是用于绘图/可视化目的

#long format with many rows instead of many columns
library(reshape2)
long <- melt(df, id.vars = c("Year")) 

#add a column with actual dates instead year and month in different columns
long$date <- as.Date(with(long,paste(variable,"15",Year,sep = "-")), "%b-%d-%Y")  

#take the subset of the data for May 2012 to June 2014
Date1 <- as.Date("2012-05-01", "%Y-%m-%d")
Date2 <- as.Date("2014-06-30", "%Y-%m-%d")
subdf <- long[long$date < Date2 & long$date > Date1,]

#use ggplot2 as it provides us nice labels and theme for timeseries by default
library(ggplot2)
ggplot(data=subdf, aes(date,value)) + geom_line()

谢谢,这看起来很棒。接下来的一个问题是,在创建日期新列的行中,代码“long$date@dward4”是一个包含所有月份的长格式列(如我的答案末尾所示)
melt
创建一个名为
variable
的新列,该列具有列名(在本例中为月份)。查看
melt的输出(df,id.vars=c(“年”)
#long format with many rows instead of many columns
library(reshape2)
long <- melt(df, id.vars = c("Year")) 

#add a column with actual dates instead year and month in different columns
long$date <- as.Date(with(long,paste(variable,"15",Year,sep = "-")), "%b-%d-%Y")  

#take the subset of the data for May 2012 to June 2014
Date1 <- as.Date("2012-05-01", "%Y-%m-%d")
Date2 <- as.Date("2014-06-30", "%Y-%m-%d")
subdf <- long[long$date < Date2 & long$date > Date1,]

#use ggplot2 as it provides us nice labels and theme for timeseries by default
library(ggplot2)
ggplot(data=subdf, aes(date,value)) + geom_line()
df <- structure(list(Year = 2011:2017, Jan = c(NA, 13.114463, 12.374017, 
      12.616257, 13.662063, 14.516674, 8.614229), Feb = c(NA, 13.021268, 
      12.365795, 12.720611, 13.527596, 14.759385, 8.446361), Mar = c(NA, 
      12.999545, 12.32328, 12.841626, 13.56843, 14.951384, 8.239606
      ), Apr = c(NA, 12.990171, 12.377747, 12.897939, 13.782818, 14.763781, 
      8.286693), May = c(NA, 12.898018, 12.462203, 13.008535, 13.889276, 
      14.536779, 8.498938), Jun = c(NA, 12.611495, 12.630298, 13.136377, 
      13.971303, 13.978265, 8.972903), Jul = c(13.724575, 12.311641, 
      12.780495, 13.159393, 14.181846, 12.888989, NA), Aug = c(13.670017, 
      12.126345, 12.848942, 13.290928, 14.329937, 11.612033, NA), Sep = c(13.782099, 
      11.974871, 12.80621, 13.495218, 14.385533, 10.362592, NA), Oct = c(13.675788, 
      12.019042, 12.860463, 13.63636, 14.289386, 9.205528, NA), Nov = c(13.442914, 
      12.163618, 12.838953, 13.797778, 14.222535, 8.649027, NA), Dec = c(13.279969, 
      12.30466, 12.608965, 13.827273, 14.384618, 8.662219, NA)), .Names = c("Year", 
      "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", 
      "Oct", "Nov", "Dec"), class = "data.frame", row.names = c(NA, -7L))
head(long)

#   Year variable    value       date 
# 1 2011      Jan       NA 2011-01-15 
# 2 2012      Jan 13.11446 2012-01-15 
# 3 2013      Jan 12.37402 2013-01-15 
# 4 2014      Jan 12.61626 2014-01-15 
# 5 2015      Jan 13.66206 2015-01-15 
# 6 2016      Jan 14.51667 2016-01-15