【发布时间】:2021-01-22 17:27:56
【问题描述】:
我想用 ggplot 绘制一些时间序列分析的结果,将变量及其预测绘制在同一张图上,同时将误差绘制在下面的图上(类似于 plot.ts em> 工作,但我不能使用这个库)。
这是我的代码:
library(ggplot2)
library(cowplot)
set.seed(1111)
my_df = data.frame(
date = 1:150,
initial = c(runif(100, max=100), rep(NA, 50)),
predicted_intra = c(rep(50, 100), rep(NA, 50)),
predicted_extra = c(rep(NA, 100), 51:100),
err_intra = c(runif(100, max=100), rep(NA, 50))
)
my_colors = c("Init" = "grey30", "Predict" = "red")
p1 <- ggplot(my_df) + aes(x = date, y = predicted_intra, color="Predict") +
geom_line() +
geom_line(aes(y = predicted_extra, color="Predict")) +
geom_line(aes(y = initial, color="Init")) +
scale_color_manual(name = "", values = my_colors) +
ylab("Numberz")
p2 <- ggplot(my_df) +
aes(x = date, y = err_intra) + geom_line(color="red") +
ylab("Error")
plot_grid(p1, p2, nrow=2, rel_heights = c(2,1))
这给出了: The result of the code above
在传奇出现之前一切都很好。有没有办法对齐两个图表的“日期”轴?
【问题讨论】: