一起绘制多个地区的每周时间序列
Plotting a weekly time series for multiple districts together
我有一个这样的数据框,其中包含印度 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
然而,我想要的是在 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")
这可能有助于绘图部分。我使用了 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
我有一个这样的数据框,其中包含印度 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
然而,我想要的是在 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")
这可能有助于绘图部分。我使用了 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