ggplot:具有多条线的 2 个方面

ggplot: 2 facets with multiple lines

我希望有两个水平面和多行数据,类似于此:

         Date   variable      value
1  2016-08-04    sd6     0.01197055
2  2016-08-05    sd6     0.01188592
3  2016-08-08    sd6     0.01179797
4  2016-08-04   sd12     0.01263279
5  2016-08-05   sd12     0.01263080
6  2016-08-08   sd12     0.01263223
7  2016-08-04   sd24     0.01074460
8  2016-08-05   sd24     0.01074747
9  2016-08-08   sd24     0.01074515
10 2016-08-04  lcorr6    0.83422880
11 2016-08-05  lcorr6    0.83598720
12 2016-08-08  lcorr6    0.83666730
13 2016-08-04 lcorr12    0.83777470
14 2016-08-05 lcorr12    0.83803200
15 2016-08-08 lcorr12    0.83790820

我是否必须添加另一列来标识 sdlcorr,或者我可以不这样做就实现这一点吗?我想到的情节是这样的:

实际结构是:

structure(list(Date = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("2016-08-04", "2016-08-05", 
"2016-08-08"), class = "factor"), variable = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c("rollsd6", 
"rollsd12", "rollsd24", "rollcorr6", "rollcorr12"), class = "factor"), 
    value = c(0.011970547, 0.011885922, 0.011797967, 0.012632791, 
    0.012630795, 0.01263223, 0.010744599, 0.010747466, 0.010745155, 
    0.8342288, 0.8359872, 0.8366673, 0.8377747, 0.838032, 0.8379082
    )), row.names = c(NA, -15L), .Names = c("Date", "variable", 
"value"), class = "data.frame")

以下是提取有关 sdcorr 的信息并使用 facet_grid 绘制的方法。请注意,我使用了 scale = "free_y",这使得 facet 的可比性降低。

library(ggplot2)

xy <- structure(list(Date = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
                                        2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("2016-08-04", "2016-08-05", 
                                                                                    "2016-08-08"), class = "factor"), variable = structure(c(1L, 
                                                                                                                                             1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c("rollsd6", 
                                                                                                                                                                                                                 "rollsd12", "rollsd24", "rollcorr6", "rollcorr12"), class = "factor"), 
                     value = c(0.011970547, 0.011885922, 0.011797967, 0.012632791, 
                               0.012630795, 0.01263223, 0.010744599, 0.010747466, 0.010745155, 
                               0.8342288, 0.8359872, 0.8366673, 0.8377747, 0.838032, 0.8379082
                     )), row.names = c(NA, -15L), .Names = c("Date", "variable", 
                                                             "value"), class = "data.frame")

xy$group_variable <- gsub("\d+$", "", xy$variable)
xy$group_sd <- as.factor(gsub("[[:alpha:]]", "", xy$variable))
xy$Date <- as.Date(xy$Date, format = "%Y-%m-%d")

ggplot(xy, aes(x = Date, y = value)) +
  theme_bw() +
  geom_line() +
  facet_grid(group_variable ~ group_sd, scale = "free_y")