试试这个动物园经典图形、动物园格子图形和动物园 ggplot2 图形:
library(zoo)
z <- read.zoo(DT, split = 1, index = 2, FUN = identity)
Names <- read.table(text = names(z), sep = ".", col.names = c("screen", "col"))
plot(z, screen = Names$screen, col = Names$col) # also see note 3 below
library(lattice)
xyplot(z, screen = Names$screen, col = Names$col) # also see note 3 below
library(ggplot2) # also see note 4 below
m <- fortify(z, melt = TRUE)
m2 <- transform(m, screen = sub("\\..*", "", Series), col = sub(".*\\.", "", Series))
qplot(Index, Value, data = m2, col = col, geom = "line") + facet_wrap(~ screen)
注意事项
1) 如果我们只是想要单独的面板,那就是 plot(z)、xyplot(z) 和 autoplot(z)。
2) names(z) 和 Names 是:
> names(z)
[1] "V1.a" "V2.a" "V1.b" "V2.b" "V1.c" "V2.c"
> Names
screen col
1 V1 a
2 V2 a
3 V1 b
4 V2 b
5 V1 c
6 V2 c
3) 我们可以硬编码成这样(在这种情况下我们不需要Names):
plot(z, screen = 1:2, col = c(1, 1, 2, 2, 3, 3))
xyplot(z, screen = 1:2, col = c(1, 1, 2, 2, 3, 3))
4) 这是不涉及 z 的 ggplot2 替代方案。它使用 data.table 包将DT 转换为长格式,然后执行qplot:
long <- DT[, list(Series = names(.SD), Value = unlist(.SD)), by = list(type, time)]
qplot(time, Value, data = long, col = type, geom = "line") + facet_wrap(~ Series)