2016-08-16 54 views
-1

首先,這裏是我的數據幀:無法在同一個GGplot上繪製多個數據集及其置信區間。數據幀包括

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

注:變量是獨立的數據幀不同。

+0

請提供實際數據作爲數據框不只是張貼數字! –

+2

並且示例代碼中的變量名稱與數據中的變量名稱不匹配。花點心思問一個問題,如果你想從這裏幫助你的人那裏獲得努力! –

回答

0

當然,您可以將您的情節構建爲一系列圖層,就像您在問題中隱含的那樣。對於您可以使用下面的代碼:

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

,並獲得這樣的: plot1

然而,reshaphing數據集到"tidy",或長格式,有一定的優勢。例如,你可以映射預測的起源成一種顏色,預測到線類型所產生的情節類型:

plot2

可以實現,使用下面的代碼:

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

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

,您仍然可以通過將自己的數據幀回寬(R)格式使用你的情節絲帶

希望這會有所幫助!

+0

謝謝你的徹底迴應!如果我有任何問題,我會給你一個鏡頭,讓你知道。 – madsthaks

+0

假設我想按字母分隔字符串,而不是字符串長度3,例如,在字符串遇到第一個小寫字母后將字符串分開。我想用'([[:lower:]]'),但那不太好。 – madsthaks

+0

問題是'separate'會丟失匹配的模式。您可以用dplyr中的'mutate'函數替換'separate'函數,並將這些值提取到一個新變量:'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)' – donlelek

相關問題