一起绘制多个地区的每周时间序列

时间:2016-01-11 08:22:27

标签: r plot time-series timeserieschart

我有一个这样的数据框,其中包含印度100个地区52周的信息。下面附有一个样本数据框,但这延伸到52周和100个区(例如Viala,Barontha等)。

    "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

我希望将其表示为时间序列数据框并绘制它。 我使用了以下代码:

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

但是我收到了以下错误

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

我看到另一个用户遇到类似错误并使用给定的答案

修改了我的代码
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)

时间序列被绘制但不是根据我想要的而且也有错误

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

Image of the time series I got

然而,我想要的是绘制y轴上的数字与x轴上的相应周数,并为每个区域绘制一条线或图。如果有人可以提供帮助

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

1 个答案:

答案 0 :(得分:1)

这可能有助于绘图部分。我使用了ggplot,因为ts.plot似乎无法绘制超过10个。然后,这只是一个问题,即以正确的格式绘制数据。

#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