0

我正在尝试解决两个问题来创建一个完整的图表(最好在 ggplot 中)。首先,我认为我需要为宽格式数据开发一个 for 循环,以便我可以将多个系列绘制为单个图上的单独行。

其次,我需要在单个系列的顶部绘制另一列(气候数据、不同的 y 值)。我知道关于双轴图有多种争论,但是客户要求提供这个特定的图,我无法绕过它。

这是生成的理想图表(借自2011 年的出版物在此处输入图像描述

我的数据目前是宽格式的,我的想法是为 2:13 列创建一个 for 循环,为每个系列生成单独的灰线。然后我希望能够有选择地在顶部添加红色的特定月份(列 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")

我试过关注这里的帖子,但我必须为每个系列创建一行代码。

我也尝试使用 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

任何帮助将不胜感激来解决这个问题。

4

1 回答 1

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

在此处输入图像描述

于 2017-06-19T14:54:20.120 回答