首页 > 解决方案 > 绘制半个数据集而不是整个记录

问题描述

我有记录期在 1966 年到 2019 年之间的数据集。我想绘制 1966 年到 1990 年之间的图表。我会使用什么命令?

标签: rggplot2plottidyverse

解决方案


ggplot2有多种方法来限制轴限制。首先,我们需要一些示例数据:

expl <- data.frame(year = rep(1966:2021, 3),
                   value = c(replicate(3, cumsum(rnorm(56, sd = .1)))),
                   id = gl(3,56))

library(ggplot2)
ggplot(expl) +
  geom_line(aes(x = year, y = value, color = id)) +
  ggtitle("full plot")

限制轴限制的一种方法coord_cartesian

ggplot(expl) +
  geom_line(aes(x = year, y = value, color = id)) +
  ggtitle("restricted x axis") +
  coord_cartesian(xlim = c(1966, 1990))

推荐阅读