Search code examples
rplottime-seriestimeserieschart

Plotting a weekly time series for multiple districts together


I have a dataframe like this which contains information of 52 weeks of 100 districts in India. A sample dataframe is attached below, however this extends to 52 weeks and 100 districts(Eg.-Viala, Barontha and so on).

    "Block" 01 Jan/ 05 Jan  06 Jan/ 12 Jan  13 Jan/ 19 Jan  20 Jan/ 26 Jan
        1   2   3   4
Viala   9   11  4   0
Barontha    0   0   0   0
Dasau   4   29  17  9
Kwanu   2   123 62  11

I wish to represent this as a time series data frame and also plot it. I used the following code:

stools=read.csv("~/stoolwithdehyd.csv",header=TRUE)
stools[is.na(stools)] <- 0
stooltimeseries <- ts(stools, frequency=52)
plot.ts(stooltimeseries)

But i got the following error

Error in plotts(x = x, y = y, plot.type = plot.type, xy.labels = xy.labels,  : 
cannot plot more than 10 series as "multiple"

I saw another user suffering from a similar error and modified my code using the given answer to

library(reshape2)    
mm = melt(stooltimeseries, id='id')
library(ggplot2)
ggplot(mm)+geom_line(aes(x=variable, y=value, group=id, color=id))
plot.ts(stooltimeseries)

The time series got plotted but not according to what I wanted and also got an error

Error in eval(expr, envir, enclos) : object 'variable' not found

Image of the time series I got

However what I want is to plot the numbers on the y axis with the corresponding weeks on the x axis and have one line or plot for every district. If anyone could help

dput(stools[1:4,1:5])

structure(list(Block. = structure(c(1L, 103L, 19L, 28L), .Label = c(" ", 
" Balawala", " Bhaniawala", " Doiwala", " Dudhli", " Herbetpur", 
" Raiwala", " Ranipokhari", " SPD Indira Nagar Colony", " UHP D.L Road", 
" UHP Dalanwala", " UHP Dobhalwala", " UHP Khurbura", " UHP Patel Nagar", 
"Ajabpur", "Asan Bag", "Ashtad", "Badripur", "Barontha", "Bhagwampur", 
"Bhatta", "Bhogpur", "Buraswa", "Byas Bhoor", "Chadroti", "Charba", 
"Chidderwala", "Dasau", "Dhaki", "Dhakrani", "Dhalipur", "Dharmawala", 
"Dilau", "Dwara Samoli", "Fatehpur", "garhi", "Gaziawala", "Gumaniwala", 
"Hakikat Rai Nagar", "Hariyawala khurd", "Jahdi", "Jakhan", "Jamnipur", 
"Jassowala", "Johdi", "Juddo", "Kamla", "Kanwali", "Kaulagarh", 
"Keinchiwala", "Kettri", "Khatar", "Khunna", "Korba", "Kunjagrant", 
"Kwansi", "Kwanu", "Lelta", "Mairavana", "Majra", "Majri", "Malsi", 
"Manthat", "Matiyawa", "MCH  Herbetpur", "MCH Rudrapur", "Mehuwala", 
"Mohana", "Naraya", "Nehrugram", "Pashchimwala", "Pelion ", "PHC Kalsi", 
"Pipaya", "Rajawala", "Rampur", "Rikhad", "Rural Health Center", 
"Sabhawala", "Sahaspur", "Sahiya", "Samalta", "Sauda Saroli", 
"Sawra", "Seinj", "Selaqui", "Sewala Kala", "Sherpur", "SPD Adhoiwala", 
"SPD Bhagat Singh Colony", "Sureu", "Telpura", "Thano", "Tyuni", 
"UHC Ajabpur", "UHC Kanwali/Seemadwar", "UHC Kishan Nagar", "UHC Majra", 
"UHC Rece Course", "UHP Ballupur", "UHP Dharampur", "UHP Reetha Mandi", 
"Viala", "Vitrali"), class = "factor"), X01.Jan..05.Jan = c(1, 
0, 0, 0), X06.Jan..12.Jan = c(2, 0, 0, 0), X13.Jan..19.Jan = c(3, 
0, 0, 0), X20.Jan..26.Jan = c(4, 0, 0, 0)), .Names = c("Block.", 
"X01.Jan..05.Jan", "X06.Jan..12.Jan", "X13.Jan..19.Jan", "X20.Jan..26.Jan"
), row.names = c(NA, 4L), class = "data.frame")

Solution

  • This might help for the plotting part. I've used ggplot as the ts.plot appears to not be able to plot more than 10. Then it's just a question of getting your data in the right format for plotting.

    #make new dataframe with week numbers as column headers
    stools2 <- stools[-1,]
    colnames(stools2) <- c("Block",stools[1,][-1])
    
    #now make things up, as all data are zero in your example
    set.seed(1)
    stools2[stools2==0] <- sample(1:12,sum(stools2==0),T)
    
    #melt the data
    library(reshape2)
    stools_melt <- melt(stools2, id.var="Block", variable.name="week")
    stools_melt$week <- as.numeric(stools_melt$week)
    
    #plot
    p1 <- ggplot(stools_melt, aes(x=week,y=value,group=Block, color=Block)) + geom_line()
    p1