简体   繁体   中英

Plotting with repeating x values in R

So I have the following dataset -

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

There are more data points, but to make things look a bit simpler I omitted them.

And to this dataset I have fit a GLM:

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

Basically, I would like to create a plot for this fitted values that this GLM produces that looks like this (this is actually the plot for the full data set,the black circles are the original data and the empty circles are the fitted data)

在此处输入图片说明

with the repeating x-values qrt on the x-axis.I'm assuming I'd have to use the predict() function and then plot the resulting values, but I've tried this and I get x-values on the x-axis going from 1 to 12 instead of repeating 1,2,3,4,1,2,3,4 etc. Also, how would you plot the original data over the fitted values, as in the plot above?

It is not difficult. Just use axis to control axis display:

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

阴谋

You don't need to use predict here. You have no gap / missing values in your quarterly time series, so the fitted values inside fitted model fit is all you need.

with ggplot:

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM