2014-10-22 2 views
0

날짜가 두 열 (1980 년 1 월, 1980 년 2 월 ... 12 월) 인이 형식의 시계열 데이터 세트가 있으며 해당 온도입니다. 이 데이터 세트는 1980 년에서 2013 년까지입니다. 저는 몇 달 동안 ggplot에서 시계열을 부분 집합하고 음모를 꾸미려고합니다 (예 : 모든 2 월을 ggplot을 사용하여 플롯 할 수 있음). 다음을 시도하지만, Feb1가 비어ggplot2에서 개월 선택 및 플롯

Feb1 <- subset(temp, date ==5) 

내 데이터 세트의 구조는 다음과 같습니다

'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 ... 
+0

어떤 용도로 사용 되었습니까? – DatamineR

+0

네, 모두 고맙습니다. 똑같은 일을하는 여러 가지 방법을 보여주기 위해서! 귀하의 도움을 주시면 감사하겠습니다! – nee

답변

1

당신은 직접 새로운 데이터 프레임을 생성하지 않고 ggplot2의 일부를 플롯 할 수 있습니다

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)) 

enter image description here

1

나는 형식 "mm의 YY"에 온다 당신의 열이 'yearmon'클래스에있는 생각합니다. 나는 당신이 '날짜 == 5'에 의해 데이터를 부분 집합하는 방법에 대해 조금 혼란스러워합니다. 아래 나는 방법을 시도합니다. 어떤이에 대한

temp$month<-substr(temp$date,1,3) 
Feb1<-subset(temp,month=='Feb') 

#more elegant 

Feb1<-subset(temp,substr(temp$date,1,3)=='Feb') 
1

? :. RStudent의 솔루션을 기반으로

:

library(zoo) 

# Generating some data: 
df <- data.frame(date = as.yearmon("1980-01") + 0:407/12, val = rnorm(408)) 

library(ggplot2) 
ggplot(df[format(df$date,"%b")=="Jan", ], aes(x = as.Date(date), y = val))+ 
    geom_line() 
1

는 음모 개월로 분할 cycle를 사용 autoplot.zoo, 동물원에 데이터를 변환합니다. 아래에서는 네 가지 다른 방법을 보여줍니다. 먼저 우리는 1 월을 계획합니다. 그런 다음 매달 모든 달을 별도의 패널에 플롯 한 다음 매월 모든 달을 별도 패널로 모두 같은 패널에 표시합니다. 마지막으로 monthplot (ggplot2 아님)을 사용하여 한 패널에 모두 다른 방식으로 그려줍니다.

zz <- zoo(matrix(coredata(z), 40, 12, byrow=TRUE), unique(as.numeric(trunc(time(z))))) 

업데이트 : 추가 플롯 유형과 접근 방식을 개선

library(zoo) 
library(ggplot2) 

# test data 
set.seed(123) 
temp <- data.frame(date = as.yearmon(1980 + 0:479/12), value = rnorm(480)) 

z <- read.zoo(temp, FUN = identity) # convert to zoo 

# split into 12 series and cbind them together so zz480 is 480 x 12 
# Then aggregate to zz which is 40 x 12 

zz480 <- do.call(cbind, split(z, cycle(z))) 
zz <- aggregate(zz480, as.numeric(trunc(time(zz480))), na.omit) 

### now we plot this 4 different ways 
##################################### 

# 1. plot just January 
autoplot(zz[, 1]) + ggtitle("Jan") 

# 2. plot each in separate panel 
autoplot(zz) 

# 3. plot them all in a single panel 
autoplot(zz, facet = NULL) 

# 4. plot them all in a single panel in a different way (not using ggplot2) 
monthplot(z) 

참고 zz을 계산하는 다른 방법이 될 것이라고.

관련 문제