在 R 中使用 ggplot2.zoo 将线图添加到带有动物园对象的现有图
Add line plot to existing plot with zoo objects using ggplot2.zoo in R
我有 100 年的时间序列气候数据。
使用基本图,我可以使用以下代码绘制年度平均值,然后是 10 年平均值:
plot(smoothprcp1yrs, col = "GREY")
lines(smoothprcp10yrs, col = "RED")
使用ggplot.zoo,绘制年度平均值的代码如下所示:
ggplot(aes(x=Index,y=Value),data=fortify(smoothMaxtmp1yrs,melt=TRUE))+geom_line()
以smoothMaxtmp1yrs
为对象。
是否可以使用 ggplot.zoo 在顶部添加另一行?
为了使其可重现,假设我的数据如下所示:
x.Date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-"))
x <- zoo(rnorm(5), x.Date)
xlow <- x - runif(5)
xhigh <- x + runif(5)
z <- cbind(x, xlow, xhigh)
y.Date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-"))
y <- zoo(rnorm(5), y.Date)
ylow <- y - runif(5)
yhigh <- y + runif(5)
a <- cbind(y, ylow, yhigh)
R 基本图如下:
plot(x)
lines(y, col = "red")
ggplot.zoo 看起来像:
ggplot(aes(x = Index, y = Value), data = fortify(x, melt = TRUE)) +
geom_line() + xlab("Index") + ylab("x")
绘制 y:
ggplot(aes(x = Index, y = Value), data = fortify(y, melt = TRUE)) +
geom_line() + xlab("Index") + ylab("x")
如何在 x 上添加 y?
一种方法是提前将 fortify
的输出组合 (rbind
) 为 data.frame
:
df <- do.call("rbind", list(fortify(y, melt = TRUE), fortify(x, melt = TRUE)))
head(df)
# Index Series Value
# 1 2003-02-01 y -0.4688883
# 2 2003-02-03 y 1.2038141
# 3 2003-02-07 y 1.2194155
# 4 2003-02-09 y 0.9252823
# 5 2003-02-14 y 0.3187009
# 6 2003-02-01 x 0.2150564
然后你可以将Series
传递给group
和color
美学:
ggplot(data = df, aes(x = Index, y = Value)) +
geom_line(aes(group = Series, color = Series))
要使用 ggplot 图形将 x 和 y 绘制在一起,请合并动物园系列,然后使用 autoplot.zoo
。参数 facet = NULL
告诉它不要为每个系列创建单独的面板。
library(zoo)
library(ggplot2)
autoplot(merge(x, y), facet = NULL) + ylab("Temperature")
我有 100 年的时间序列气候数据。 使用基本图,我可以使用以下代码绘制年度平均值,然后是 10 年平均值:
plot(smoothprcp1yrs, col = "GREY")
lines(smoothprcp10yrs, col = "RED")
使用ggplot.zoo,绘制年度平均值的代码如下所示:
ggplot(aes(x=Index,y=Value),data=fortify(smoothMaxtmp1yrs,melt=TRUE))+geom_line()
以smoothMaxtmp1yrs
为对象。
是否可以使用 ggplot.zoo 在顶部添加另一行?
为了使其可重现,假设我的数据如下所示:
x.Date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-"))
x <- zoo(rnorm(5), x.Date)
xlow <- x - runif(5)
xhigh <- x + runif(5)
z <- cbind(x, xlow, xhigh)
y.Date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-"))
y <- zoo(rnorm(5), y.Date)
ylow <- y - runif(5)
yhigh <- y + runif(5)
a <- cbind(y, ylow, yhigh)
R 基本图如下:
plot(x)
lines(y, col = "red")
ggplot.zoo 看起来像:
ggplot(aes(x = Index, y = Value), data = fortify(x, melt = TRUE)) +
geom_line() + xlab("Index") + ylab("x")
绘制 y:
ggplot(aes(x = Index, y = Value), data = fortify(y, melt = TRUE)) +
geom_line() + xlab("Index") + ylab("x")
如何在 x 上添加 y?
一种方法是提前将 fortify
的输出组合 (rbind
) 为 data.frame
:
df <- do.call("rbind", list(fortify(y, melt = TRUE), fortify(x, melt = TRUE)))
head(df)
# Index Series Value
# 1 2003-02-01 y -0.4688883
# 2 2003-02-03 y 1.2038141
# 3 2003-02-07 y 1.2194155
# 4 2003-02-09 y 0.9252823
# 5 2003-02-14 y 0.3187009
# 6 2003-02-01 x 0.2150564
然后你可以将Series
传递给group
和color
美学:
ggplot(data = df, aes(x = Index, y = Value)) +
geom_line(aes(group = Series, color = Series))
要使用 ggplot 图形将 x 和 y 绘制在一起,请合并动物园系列,然后使用 autoplot.zoo
。参数 facet = NULL
告诉它不要为每个系列创建单独的面板。
library(zoo)
library(ggplot2)
autoplot(merge(x, y), facet = NULL) + ylab("Temperature")