【问题标题】:Having trouble plotting multiple data sets and their confidence intervals on the same GGplot. Data Frame included在同一个 GGplot 上绘制多个数据集及其置信区间时遇到问题。包括数据框
【发布时间】:2016-12-22 08:08:24
【问题描述】:

首先,这是我的数据框:

> df.combined
      MLSupr   MLSpred    MLSlwr    BPLupr    BPLpred     BPLlwr
1   1.681572  1.392213  1.102854  1.046068  0.8326201  0.6191719
2   3.363144  2.784426  2.205708  2.112885  1.6988250  1.2847654
3   5.146645  4.232796  3.318946  3.201504  2.5999694  1.9984346
4   6.930146  5.681165  4.432184  4.368555  3.6146180  2.8606811
5   8.713648  7.129535  5.545422  5.480557  4.5521112  3.6236659
6  10.497149  8.577904  6.658660  6.592558  5.4896044  4.3866506
7  12.280651 10.026274  7.771898  7.681178  6.3907488  5.1003198
8  14.064152 11.474644  8.885136  8.924067  7.4889026  6.0537381
9  15.847653 12.923013  9.998373 10.125539  8.5444783  6.9634176
10 17.740388 14.429805 11.119222 11.327011  9.6000541  7.8730970
11 19.633122 15.936596 12.240071 12.620001 10.7425033  8.8650055
12 21.525857 17.443388 13.360919 13.821473 11.7980790  9.7746850
13 23.535127 19.010958 14.486789 15.064362 12.8962328 10.7281032
14 25.544397 20.578528 15.612659 16.307252 13.9943865 11.6815215
15 27.553667 22.146098 16.738529 17.600241 15.1368357 12.6734300
16 29.562937 23.713668 17.864399 18.893231 16.2792849 13.6653384
17 31.572207 25.281238 18.990268 20.245938 17.4678163 14.6896948
18 33.581477 26.848807 20.116138 21.538928 18.6102655 15.6816033
19 35.590747 28.416377 21.242008 22.891634 19.7987969 16.7059597
20 37.723961 30.047177 22.370394 24.313671 21.0352693 17.7568676

所以,如您所见,我有预测值及其 95% CI 的上限和下限。我想在同一个图中为 MLS 和 BPL 绘制线条及其丝带,但我不太确定如何。 现在,对于单个数据集,我正在使用这个命令:

ggplot(BULISeason, aes(x = 1:length(BULISeason$`Running fit`), y = `Running fit`)) + 
  geom_line(aes(fill = "black")) +
  geom_ribbon(aes(ymin = `Running lwr`, ymax = `Running upr`, fill = "red"),alpha = 0.25)

注意:独立数据框的变量不同。

【问题讨论】:

  • 请提供实际数据作为数据框,而不仅仅是发布的数字!
  • 并且示例代码中的变量名称与数据中的变量名称不匹配。如果您想从这里帮助您的人那里得到努力,请花一些力气提出问题!

标签: r ggplot2 confidence-interval


【解决方案1】:

当然,您可以将绘图构建为一系列层,就像您在问题中暗示的那样。为此,您可以使用以下代码:

ggplot(data = df.combined) +
geom_ribbon(aes(x = x, ymin = MLSlwr, ymax = MLSupr), 
            fill = "blue", alpha = 0.25) +
geom_line(aes(x = x, y = MLSpred), color = "black") +
geom_ribbon(aes(x = x, ymin = BPLlwr, ymax = BPLupr), 
            fill = "red", alpha = 0.25) +
geom_line(aes(x = x, y = BPLpred), color = "black")

并获得如下内容:

但是,将数据集重新整形为 "tidy" 或长格式具有一些优势。例如,您可以将预测的原点映射为颜色,并将预测的类型映射为结果图中的线型:

您可以使用以下代码实现:

library(tidyr)

tidy.data  <- df.combined %>% 
  # add id variable
  mutate(x = 1:20) %>% 
  # reshape to long format
  gather("variable", "value", 1:6) %>% 
  # separate variable names at position 3
  separate(variable, 
           into = c("model", "line"), 
           sep = 3, 
           remove = TRUE)

# plot
ggplot(data = tidy.data, aes(x        = x, 
                             y        = value, 
                             linetype = line, 
                             color    = model)) + 
  geom_line() + 
  scale_linetype_manual(values = c("dashed", "solid", "dashed"))

您仍然可以通过将数据框扩展回宽 (r) 格式来在绘图中使用功能区:

# back to wide
wide.data <- tidy.data %>% 
  spread(line, value)

# plot with ribbon
ggplot(data = wide.data, aes(x = x, y = pred)) +
  geom_ribbon(aes(ymin = lwr, ymax = upr, fill = model), alpha = .5) +
  geom_line(aes(group = model))

希望这会有所帮助!

【讨论】:

  • 感谢您的详尽回复!我会试一试,如果我有任何问题,请告诉您。
  • 假设我想按大小写而不是长度 3 来分隔字符串,例如,一旦字符串碰到第一个小写字母,就将其分开。我想使用 '([[:lower:]])' 但效果不佳。
  • 问题是separate 会丢弃匹配的模式。您可以将 separate 函数替换为 dplyr 中的 mutate 函数并将值提取到新变量:mutate(model = paste(stri_extract_all_regex(variable, "[[:upper:]]*")[[1]], collapse = ""), line = paste(stri_extract_all_regex(variable, "[[:lower:]]*")[[1]], collapse = "")),您将需要 library(dplyr)library(stringi)
猜你喜欢
  • 1970-01-01
  • 2018-03-05
  • 2013-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
相关资源
最近更新 更多