2017-06-19 64 views
0

我試圖解決兩個問題來創建一個完整的圖(最好在ggplot中)。首先,我認爲我需要爲廣泛的數據開發一個for循環,以便我可以在單個繪圖上繪製多個系列作爲單獨的行。其次,我需要在單個系列的頂部繪製另一列(氣候數據,不同的y值)。我知道關於雙軸圖有很多爭論,但客戶要求這個特定的圖,我無法繞過它。在R ggplot寬格式中創建for循環

這是理想的曲線,產生(從2011 publication借來的) enter image description here

我的數據目前在寬格式,以及我的想法是創建一個for循環2:13列,以生產個人灰色每個系列的線。然後,我希望能夠選擇性地將頂部的某個月份(列14:25)添加爲紅色。如果我可以有選擇地抽出幾個月並將它們繪製在灰色數據系列上,我很樂意切換到長格式。以下是一小部分數據,實際數據集要大得多。

WideDF <- structure(list(Year = c("2008", "2009", "2010", "2011", "2012", 
"2013", "2014"), M3T1045 = c(153.821659726048, 123.081558884975, 
176.771512423309, 181.76302562778, 121.366484915564, 112.166318263156, 
83.430179685548), M3T2045 = c(1979.67428393397, 2304.69652323779, 
2038.57278760525, 1598.21780623635, 1698.10896695776, 1282.44883906715, 
1243.48492202174), M3T3045 = c(2059.61060104009, 2538.3591621577, 
2836.19881815023, 2529.3912848661, 2715.04505989801, 2137.18245498415, 
1914.61513277697), M3T4045 = c(3923.28951818649, 4180.76535402238, 
4214.78156108969, 4295.05332803458, 4522.22790268241, 4472.36745319943, 
4098.5309021999), M3T5045 = c(2018.8160905218, 1997.65271391178, 
2079.82809750919, 2248.83387563616, 2494.15706661903, 1355.10547024274, 
1353.37214047842), M3T6045 = c(4409.16623051298, 4276.51428713093, 
3831.60326756482, 3308.15549785341, 4045.82249844548, 4462.00875828256, 
3572.81219768132), M3U1045 = c(113.83650201514, 118.780241020127, 
114.984700722952, 133.421492210513, 205.600098647934, 122.476010617811, 
97.1844650220458), M3U2045 = c(337.787032910181, 376.487106438174, 
430.035628616836, 414.099557447903, 458.763256619945, 331.056861800248, 
238.741139683364), M3U3045 = c(320.507421943828, 322.708996668388, 
313.17219893837, 270.058319678974, 376.456328254947, 267.987391226506, 
296.912173018184), M3U4045 = c(1095.26781018262, 1138.4400696583, 
1401.22300738868, 1387.70010663431, 1658.03042540523, 1230.4459207838, 
987.787227627777), M3U5045 = c(125.346087984721, 127.324176091091, 
111.68607241898, 164.421987100271, 241.013673561191, 158.550165076745, 
157.942524508873), M3U6045 = c(774.63712785103, 824.750682887739, 
745.467515460673, 666.817790501942, 988.567171986328, 654.042598223899, 
559.472739395766), Jan = c(1.11, 3.38, 3.41, 1.26, 1.02, -1.39, 
1.71), Feb = c(2.32, 2.85, 3.09, 0.97, -1.52, -1.22, 1.55), Mar = c(2.47, 
2.1, 3.59, 1.31, -2.29, -1.62, 1.7), Apr = c(2.45, 2, 2.5, 1.96, 
-1.93, -2.14, 1.45), May = c(1.44, 1.97, 1.47, 2.12, -1.26, -1.35, 
1.48), Jun = c(1.77, 3.53, 1.86, 1.74, 2.06, 1.57, 1.73), Jul = c(2.07, 
4.66, 1.14, 1.1, 1.07, 2.28, 3.09), Aug = c(2.83, 4.74, -1.6, 
2.55, 1.08, 2.79, 3.05), Sep = c(4.04, 3.63, -1.22, 2.26, 1.5, 
3.48, 1.92), Oct = c(3.89, 4.21, 0.97, 2.7, 2.46, 2.31, 2.53), 
    Nov = c(4.09, 3.97, 1.28, 1.67, 1.12, 2.13, 2.16), Dec = c(4.21, 
    3.79, 2.05, 1.38, 1.51, 1.84, 2.71)), .Names = c("Year", 
"M3T1045", "M3T2045", "M3T3045", "M3T4045", "M3T5045", "M3T6045", 
"M3U1045", "M3U2045", "M3U3045", "M3U4045", "M3U5045", "M3U6045", 
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", 
"Oct", "Nov", "Dec"), row.names = 65:71, class = "data.frame") 

我已試過後here,但我會創建一個行代碼爲每個系列。

我也嘗試使用由Didzis here提供的for循環,但不斷得到每個組只包含1個觀測值的錯誤。這是我嘗試使用的代碼:

plotAllLayers(WideDF) 
fields <- names(WideDF[,1:13]) 
p <- ggplot(aes(x=Year), data=WideDF) 
for (i in 2:length(fields)) { 
loop_input = paste("geom_smooth(aes(y=",fields[i],",color='",fields[i],"'))", sep="") 
p <- p + eval(parse(text=loop_input)) 
} 
p <- p + guides(color = guide_legend(title = "",)) 
p 

任何幫助將不勝感激解決這個問題。

+0

爲什麼你堅持讓你的數據在寬格式?爲什麼不重塑長/高格式以使繪圖更容易。 – MrFlick

+0

正如我在帖子中提到的那樣,如果還有辦法將特定月份的氣候數據繪製在頂部,我很樂意重塑它。當我最初轉換爲長格式時,它繪製了我所有的數據(單個時間序列以及幾個月的氣候數據)。如果有一種方法可以避免這種情況發生並仍然產生上述圖表,我很樂意將其轉換爲long。 – KKL234

+0

我建議不要在同一圖上繪製兩個不同單位的數據系列,尤其是在任意縮放時。它傾向於暗示不正確的關係。 – AkselA

回答

0

你的數據很混亂,你不能真正使用你的變量M3T或M3U。 另外,你應該切換到長格式,並避免在ggplot2中做一個循環。

這裏是如何做到這一點的爲例:

library(tidyverse) 

coef_shrink <- 120 
month_picked <- month.abb[1] 

# convert to long format: 
longdf <- WideDF %>% 
    gather(key = var, value = measure 
     , -Year) 

# limit to the monthly variables: 
longdf_year <- longdf %>% 
    filter(var %in% month_picked) %>% 
    mutate(measure = measure * coef_shrink) 

# limit to the M* variables 
longdf_MTU <- longdf %>% 
    filter(!var %in% month.abb) 

# plot: 
ggplot(longdf_MTU, aes(x = Year, y = measure, group = var)) + 
    geom_line() + 
    geom_line(data = longdf_year, aes(x = Year, y = measure), color = "red")+ 
    scale_y_continuous(
    "var 1", 
    sec.axis = sec_axis(~ ./coef_shrink, name = "var 2") 
) 

enter image description here

+0

感謝您提供可能的答案。你能幫助解釋一下你的答案嗎?爲了澄清,我將2個獨立的數據框合併在一起,試圖按年份編譯我的所有數據(df1 = M3U和M3T列,df2 =月份列)。如果我可以將它們分開併疊加2個圖表,我很樂意這樣做。另外,爲什麼我不能使用我的M3U和M3T變量?它們是我的主要數據,並且我想用來在圖形中創建我的各條線。月份(1月 - 12月)在那裏,所以我可以選擇幾個月重疊M3U和M3T數據。 – KKL234

+1

ggplot2提供了一種繪製數據的方法,幾乎​​可以在你想從長格式的時刻開始繪製數據,並使用一堆圖層來創建繪圖。所以你可以合併多個ggplot圖形。基本上,你的情況沒有理由連接你的df。 – YCR

+0

另外,您可以添加第2軸: https:// stackoverflow。com/questions/3099219/plot-with-2-y-axis-one-y-axis-on-the-left-and-another-y-axis-on-the-right – YCR