2016-12-04 2 views
1

그래서 나는 다음과 같은 데이터 세트가 -R에서 X 값을 반복하여 플로팅

dat <- structure(list(cases = c(2L, 6L, 10L, 8L, 12L, 9L, 28L, 28L, 
36L, 32L, 46L, 47L), qrt = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
1L, 2L, 3L, 4L), date = c(83, 83.25, 83.5, 83.75, 84, 84.25, 
84.5, 84.75, 85, 85.25, 85.5, 85.75)), .Names = c("cases", "qrt", 
"date"), class = "data.frame", row.names = c(NA, -12L)) 


    cases qrt date 
    2 1  83.00 
    6 2  83.25 
    10 3  83.50 
    8 4  83.75 
    12 1  84.00 
    9 2  84.25 
    28 3  84.50 
    28 4  84.75 
    36 1  85.00 
    32 2  85.25 
    46 3  85.50 
    47 4  85.75 

가 더 많은 데이터 포인트,하지만 일이 내가 그들을 생략 조금 간단하게 보이게합니다.

그리고 나는 GLM에 맞게이이 데이터 집합에

: 기본적으로

fit <- glm(cases~date+qrt, family = poisson, data = dat) 

을, 나는이 GLM은 그 다음과 같습니다 생산이 장착 값에 대한 그래프를 만들 싶습니다 (이것은을 위해 실제로 플롯이다 전체 데이터 세트, 내가 가진 거라고 가정의 x axis.I'm에 QRT 반복 x 값에

enter image description here

) 검은 원은 원래 데이터는 다음과 빈 원은 피팅 된 데이터입니다 기능을 사용하려면 predict()을 사용하십시오. 이온 및 다음 결과 값을 음모,하지만이 시도하고 x 축에서 1에서 12로 이동하는 대신 12,3,4,1,2,3,4 등 반복하는 값을 가져옵니다. 또한 위의 그림에서와 같이 피팅 된 값 위에 원본 데이터를 플로팅하는 방법은 무엇입니까?

+0

불행하게도, 나는 코드가없는, 그냥이 이미지의 플롯과 같이 의미 있다고 알고 있습니다. – tattybojangler

답변

1

어렵지 않습니다. 그냥 축 표시를 제어 axis를 사용

## disable "x-axis" when `plot` fitted values 
## remember to set decent `ylim` for your plot 
plot(fit$fitted, xaxt = "n", xlab = "qtr", ylab = "cases", main = "GLM", 
    ylim = range(c(fit$fitted, dat$cases))) 
## manually add "x-axis", with "labels" and "las" 
axis(1, at = 1:12, labels = rep.int(1:4, 3), las = 2) 
## add original observed cases 
points(dat$cases, pch = 19) 

plot

현재 predict를 사용할 필요가 없습니다. 분기 별 시계열에 갭/누락 값이 없기 때문에 적합 모델 fit에 맞는 값만 있으면됩니다. ggplot와

0

:

df <- rbind(data.frame(index=as.factor(1:nrow(dat)), value=dat$cases, cases='actual'), 
      data.frame(index=as.factor(1:nrow(dat)), value=predict(fit, type='response'), cases='predicted')) 
library(ggplot2) 
ggplot(df, aes(index, value, color=cases)) + geom_point(cex=3) + 
    scale_color_manual(values=c('black', 'gray')) + 
    scale_y_continuous(breaks=seq(0, max(df$value)+5, 5)) + theme_bw() 

enter image description here

관련 문제