【问题标题】:R: Align plots with different x rangesR:对齐具有不同 x 范围的图
【发布时间】:2013-02-19 12:57:26
【问题描述】:

我有两个数据框dataAdataB,它们都包含一个time 和一个value 列。时间列密切相关,但不相同。现在,我用 ggplot 生成两个图,例如:

plotA <- ggplot(dataA) + geom_line(aes(x = time, y = value))
plotB <- ggplot(dataB) + geom_line(aes(x = time, y = value))

如何使用grid.arrange 或类似函数来垂直显示两个图,从而使 x 轴标签和网格线对齐?

【问题讨论】:

  • 请通过添加示例数据使您的代码可重现。
  • post 有帮助吗?看起来可能是重复的。
  • "时间列密切相关,但不相同。"我正在寻求对齐值,而不是绘图区域。
  • 它不会“绘制”区域。它将图表的左端在两者之间对齐,这就是我认为您在说“x 轴 标签和网格线 align”时想要的。

标签: r ggplot2 dataframe


【解决方案1】:

您可以使用构面来对齐图。

首先,需要合并两个数据集:

dataAB <- rbind(dataA[c("time", "value")], dataB[c("time", "value")])

新列表示原始数据集:

dataAB$ind <- c(rep("A", nrow(dataA)), rep("B", nrow(dataB)))

剧情:

library(ggplot2)
ggplot(dataAB) + 
  geom_line(aes(x = time, y = value)) +
  facet_wrap( ~ ind, ncol = 1, scales = "free_y")

【讨论】:

  • 哇,比我预期的要好得多...谢谢
猜你喜欢
  • 2016-11-03
  • 2023-04-09
  • 2022-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 2022-08-09
  • 2021-10-17
相关资源
最近更新 更多