Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot: 2 facets with multiple lines

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")
like image 439
AK88 Avatar asked Oct 20 '25 07:10

AK88


1 Answers

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")
like image 53
Roman Luštrik Avatar answered Oct 21 '25 21:10

Roman Luštrik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!