【问题标题】:Combine Line and Headmap plots in R在 R 中结合线图和头图图
【发布时间】:2019-05-09 20:48:10
【问题描述】:

在研究如何绘制多个折线图时,我发现了以下论文:

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

它通过将每个折线图与一个通用的头图相结合,展示了如何显示大量时间序列数据的方法,结果看起来有点像这种表示:

我正在寻找一个 R 包(但找不到任何东西)或 ggplot 的一个很好的实现来达到相同的结果。所以我可以绘制很多 geom_lines 并为它们着色,但我不知道如何实际应用 headmap。

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

谢谢! 斯蒂芬

【问题讨论】:

  • See here 提出一个人们可以帮助解决的 R 问题。这包括一个数据样本、所有必要的代码,以及对你正在尝试做什么和什么没有奏效的清晰解释。现在这是一个非常广泛的问题,没有具体的任务。

标签: r ggplot2 package


【解决方案1】:
library(tidyverse)
datasets::ChickWeight # from Base R
ggplot(ChickWeight, aes(Time, weight, group = Chick)) + geom_line()

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

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)

如果您的数据具有稀疏的时间读数,插入您的行以获得更高的分箱分辨率可能会很有用:

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)

【讨论】:

  • 嗨,乔恩,感谢您提供出色(而且速度极快)的答案!
猜你喜欢
  • 1970-01-01
  • 2012-05-22
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 1970-01-01
  • 2023-02-07
  • 2012-03-09
相关资源
最近更新 更多