在R中合并线图和头图图

时间:2019-05-09 20:48:10

标签: r ggplot2 package

关于如何绘制多个折线图的研究中,我遇到了以下论文:

https://arxiv.org/pdf/1808.06019.pdf

通过将每个折线图与一个通用的标题图相结合,展示了一种显示大量时间序列数据的方法,结果看起来与该表示形式相等:

DenseLines

我正在寻找R包(但找不到任何东西)或ggplot的一个不错的实现,以实现相同的结果。因此,我能够绘制许多geom_lines并对其进行不同的着色,但是我不知道如何将headmap实际应用于它。

有人对我有提示/想法吗?

谢谢! 斯蒂芬

1 个答案:

答案 0 :(得分:2)

library(tidyverse)
datasets::ChickWeight # from Base R
ggplot(ChickWeight, aes(Time, weight, group = Chick)) + geom_line()

enter image description here

这里的争吵会计算每个时间段/重量桶中有多少读数,并标准化为每次“最常见读数的份额”。

ChickWeight %>%
  count(Time, weight = 10*floor(weight/10)) %>%
  complete(Time, weight = 10*0:30, fill = list(n = 0)) %>%
  group_by(Time) %>%
  mutate(share = n / max(n)) %>%  # weighted for num as % of max for that Time
  ungroup() %>%

  ggplot(aes(Time, weight, fill = share)) +
  geom_tile(width = 2) +
  scale_fill_viridis_c(direction = -1)

enter image description here

如果您的数据的时间读数稀疏,则对行进行插值以获取更高的分档分辨率可能会很有用:

ChickWeight %>%
  group_by(Chick) %>%
  arrange(Time) %>%
  padr::pad_int("Time", step = 0.5) %>%
  mutate(weight_approx = approx(Time, weight, Time)$y) %>%
  ungroup() %>%

  count(Time, weight_approx = 10*floor(weight_approx/10)) %>%
  complete(Time, weight_approx = 10*0:60, fill = list(n = 0)) %>%
  group_by(Time) %>%
  mutate(share = n / sum(n)) %>%   # Different weighting option
  ungroup() %>%

  ggplot(aes(Time, weight_approx, fill = share)) +
  geom_tile() +
  scale_fill_viridis_c(direction = -1)

enter image description here