3

I'd like to have two horizontal facets with multiple lines for data similar to this:

         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

Do I have to add another column to identify sd and lcorr or can I achieve this without doing so? The plot that I have in mind is something like:

enter image description here

Actual structure is:

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")
AK88
  • 2,946
  • 2
  • 12
  • 31
  • 1
    Try `facet_grid` where you can specify each dimension. You will probably need to make a second variable where you will identify `sd` and `lcorr`, yes. – Roman Luštrik Aug 11 '17 at 12:57
  • thanks @RomanLuštrik. any idea how to extract only `sd` and `lcorr` from the column `variable`? – AK88 Aug 11 '17 at 13:00
  • 1
    Please provide your data in an easy-to-paste form (e.g. using `dput`) and I can show you. – Roman Luštrik Aug 11 '17 at 13:02
  • Done and done. Slightly different than the initial structure tho -- and let's extract `sd` and `corr`. – AK88 Aug 11 '17 at 13:05
  • OK, figured with `df$ID = ifelse(grepl("sd", df$variable), "sd", "corr")` – AK88 Aug 11 '17 at 13:13

1 Answers1

3

Here's how you can extract information about sd or corr and plot it using facet_grid. Notice that I used scale = "free_y" which makes facet less comparable.

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")
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197