我的数据看起来像这样:
共有10,000行,每行代表一个城市和1998-01至2013-9年度以来的所有月份:
RegionName| State| Metro| CountyName| 1998-01| 1998-02| 1998-03
New York| NY| New York| Queens| 1.3414| 1.344| 1.3514
Los Angeles| CA| Los Angeles| Los Angeles| 12.8841| 12.5466| 12.2737
Philadelphia| PA| Philadelphia| Philadelphia| 1.626| 0.5639| 0.2414
Phoenix| AZ| Phoenix| Maricopa| 2.7046| 2.5525| 2.3472我想要能够做自1998年以来的任何城市或多个城市的所有月份的地块。
我试过了,但我得到了一个错误。我甚至不确定我是不是在尝试这样做。任何帮助都将不胜感激。谢谢。
forecl <- ts(forecl, start=c(1998, 1), end=c(2013, 9), frequency=12)
plot(forecl)
Error in plots(x = x, y = y, plot.type = plot.type, xy.labels = xy.labels, :
cannot plot more than 10 series as "multiple"发布于 2013-11-01 06:03:10
你可以试试
require(reshape)
require(ggplot2)
forecl <- melt(forecl, id.vars = c("region","state","city"), variable_name = "month")
forecl$month <- as.Date(forecl$month)
ggplot(forecl, aes(x = month, y = value, color = city)) + geom_line()发布于 2013-11-01 07:35:55
为了补充@JLLagrange的答案,如果有太多的城市并且颜色很难区分,你可能想通过facet_grid()传递city。
ggplot(forecl, aes(x = month, y = value, color = city, group = city)) +
geom_line() +
facet_grid( ~ city)发布于 2013-11-01 06:31:55
您能否提供一个数据示例,例如dput(head(forecl))、,然后再将转换为时间序列对象?这个问题也可能与ts对象有关。
无论如何,我认为有两个问题。
首先,数据是宽格式的。我不确定您的列名,因为它们应该以字母开头,但在任何情况下,一般的想法是这样做:
test <- structure(list(
city = structure(1:2, .Label = c("New York", "Philly"),
class = "factor"), state = structure(1:2, .Label = c("NY",
"PA"), class = "factor"), a2005.1 = c(1, 1), a2005.2 = c(2, 5
)), .Names = c("city", "state", "a2005.1", "a2005.2"), row.names = c(NA,
-2L), class = "data.frame")
test.long <- reshape(test, varying=c(3:4), direction="long")其次,我认为你试图同时绘制太多的城市。尝试:
plot(forecl[, 1])或
plot(forecl[, 1:5])https://stackoverflow.com/questions/19717256
复制相似问题