您可以使用ggplot2 和patchwork 执行此操作。
从加载库开始:
library(ggplot2)
library(patchwork)
现在让我们创建一些示例数据以供使用:
df <- airquality
df$Date <- seq(from = as.Date("1973-05-01"),
to = as.Date("1973-09-30"),
by = "1 day")
我们可以这样制作密度图
p1 <- ggplot(df, aes(y = Temp)) +
geom_density(fill = "blue", alpha = 0.2, color = "blue") +
theme_classic() +
theme(axis.ticks.x = element_line(color = "white"),
axis.text.x = element_text(color = "white"),
axis.title.x = element_text(color = "white"),
axis.line.x = element_line(color = "white")
)
主要的时间序列图是这样的:
p2 <- ggplot(df, aes(Date, Temp)) +
geom_line(color = "blue") +
theme_classic() +
theme(panel.background = element_rect(fill = "#fef8d6"),
axis.ticks.length.y = unit(0, "pt"),
axis.text.y = element_blank(),
axis.title.y = element_blank())
要绘制它们,我们可以这样做:
p1 + p2 + plot_layout(widths = c(1, 8))