【问题标题】:Plotting half a dataset and not the entire record [closed]绘制半个数据集而不是整个记录 [关闭]
【发布时间】:2021-07-19 20:39:49
【问题描述】:

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

【问题讨论】:

  • 对于子集,x[x$year >= 1966 & x$year <= 1990,]subset(x, year >= 1966 & year <= 1990)(或 dplyr::filter(x, ...),因为您包括了 tidyverse)。对于绘图,您是否尝试过plot?这个问题太模糊,无法提供合理的建议。术语“图”可能意味着任何东西,包括直方图、密度图、散点图、线、面积图、条形图、小提琴图或许多其他东西之一,它取决于我们没有的东西:上下文数据类型
  • Lindiwe nkaBANE,您之前的问题都有某种形式的示例数据、示例代码或两者都有,我强烈建议您在此处也这样做(在问题结束之前)。

标签: r ggplot2 plot tidyverse


【解决方案1】:

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))

【讨论】:

  • 谢谢。它奏效了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
相关资源
最近更新 更多