Facet time series

HELLO,
I want to create a faceted time series or all my time series to be on the same graph with the y axis labeled as "car intensity", the xlab as "time" , legend showing the individual car, for example, "2,10,16,22" in one graph, next set of cars "3,11,17,23" in another graph and the final set in the 3rd graph "4,12,18,24". If a line graph can substitute the same graph output that is also fine. The first column of the data is the time "1" and the car intensity is the "2,10,16,22" for the first graph, "3,11,17,23" for the next and "4,12,18,24" for the 3rd graph.

I created individual graphs for the 3 graphs and tried using par() but it shrinks the graphs into lines.

colnames(data) <- c(1:1861)
#> Error in `colnames<-`(`*tmp*`, value = 1:1861): attempt to set 'colnames' on an object with less than two dimensions


datapasta::df_paste(head(data, 20)[, c(1,2,3,4,10,11,12,16,17,18,22,23,24)])
#> Error in unclass(x)[...]: subscript out of bounds


data.frame(
  check.names = FALSE,
                          `1` = c(-10,-9.5,
                                  -9,-8.5,-8,-7.5,-7,-6.5,-6,-5.5,-5,
                                  -4.5,-4,-3.5,-3,-2.5,-2,-1.5,-1,-0.5),
                          `2` = c(-211.61328125,604.0595703125,1379.2294921875,
                                  2038.73876953125,2553.833984375,2894.51171875,
                                  2959.97802734375,2641.3974609375,
                                  1930.20361328125,914.689453125,-366.2734375,
                                  -1899.146484375,-3592.6533203125,-5365.5068359375,
                                  -7246.955078125,-9299.81640625,-11457.705078125,
                                  -13524.01171875,-15385.00390625,
                                  -17070.005859375),
                          `3` = c(952.904296875,13.1982421875,-1079.62158203125,
                                  -2253.3828125,-3488.69287109375,-4773.6533203125,
                                  -6050.5986328125,-7237.23828125,
                                  -8335.248046875,-9423.7451171875,-10528.4208984375,
                                  -11574.427734375,-12431.482421875,
                                  -13036.7197265625,-13461.890625,-13797.7578125,
                                  -14045.1875,-14139.125,-14058.3349609375,
                                  -13865.103515625),
                          `4` = c(712.947265625,301.94091796875,-81.090087890625,
                                  -349.46728515625,-472.5947265625,-463.00732421875,
                                  -374.3544921875,-243.77978515625,
                                  -51.1552734375,230.17041015625,658.130859375,
                                  1267.3017578125,2010.0712890625,2795.23046875,
                                  3553.76318359375,4323.6337890625,5165.396484375,
                                  6083.3212890625,7091.21875,8220.83984375),
                         `10` = c(2709.34130859375,3149.55224609375,3289.40576171875,
                                  3152.71826171875,2810.41552734375,
                                  2388.66357421875,1981.74548339844,1571.44274902344,
                                  1116.50122070313,650.555969238281,
                                  213.704376220703,-206.142333984375,-634.929443359375,
                                  -1061.41967773438,-1452.56420898438,
                                  -1799.29174804688,-2131.35424804688,-2485.56518554688,
                                  -2857.73510742188,-3209.7216796875),
                         `11` = c(3671.6572265625,2823.46630859375,1924.05859375,
                                  966.05322265625,-3.07177734375,-992.118530273438,
                                  -2028.09521484375,-3005.29370117188,
                                  -3832.77197265625,-4541.802734375,-5155.244140625,
                                  -5637.2177734375,-5908.79150390625,
                                  -5892.7587890625,-5541.02734375,-4881.6259765625,
                                  -3990.47998046875,-2935.74755859375,
                                  -1783.95983886719,-480.456176757813),
                         `12` = c(1099.54638671875,449.3603515625,31.7216796875,
                                  -176.67333984375,-126.19921875,152.1953125,
                                  480.310546875,737.37060546875,931.1318359375,
                                  1133.4052734375,1428.36181640625,
                                  1869.55297851563,2473.44921875,3248.67358398438,
                                  4199.32373046875,5278.0029296875,6400.5908203125,
                                  7468.81494140625,8377.72265625,9052.796875),
                         `16` = c(-1071.40869140625,-1801.21301269531,-2293.45849609375,
                                  -2573.5986328125,-2733.87744140625,
                                  -2861.203125,-2966.48193359375,-2965.1923828125,
                                  -2768.06469726563,-2382.484375,
                                  -1898.98559570313,-1375.55346679688,-752.824462890625,
                                  46.832275390625,1016.4599609375,2093.45458984375,
                                  3174.3046875,4155.47216796875,
                                  4999.5361328125,5727.30078125),
                         `17` = c(-971.363525390625,-467.325439453125,-39.77978515625,
                                  282.186279296875,571.5029296875,
                                  881.49072265625,1189.89379882813,1443.15014648438,
                                  1645.10375976563,1839.93676757813,
                                  2050.70629882813,2312.7236328125,2647.95922851563,
                                  3010.140625,3344.90234375,3675.73388671875,
                                  4072.083984375,4540.4638671875,5049.83544921875,
                                  5531.4306640625),
                         `18` = c(-295.576416015625,-1155.1279296875,-1944.73767089844,
                                  -2598.01806640625,-3116.03369140625,
                                  -3562.58154296875,-3991.31494140625,
                                  -4384.37939453125,-4704.3583984375,-4880.3828125,
                                  -4841.7080078125,-4636.353515625,-4357.265625,
                                  -4048.0234375,-3744.73779296875,-3464.28295898438,
                                  -3220.84301757813,-3057.658203125,
                                  -2926.37841796875,-2654.81005859375),
                         `22` = c(-810.5146484375,2463.0888671875,5718.8486328125,
                                  8787.7509765625,11539.2646484375,13961.75,
                                  16120.009765625,17962.95703125,19370.076171875,
                                  20226.345703125,20479.830078125,20183.75,
                                  19403.51953125,18158.453125,16456.890625,
                                  14374.82421875,12091.8349609375,9762.8740234375,
                                  7410.6005859375,4952.67138671875),
                         `23` = c(1370.3681640625,3725.236328125,5685.6171875,
                                  7182.8642578125,8205.9482421875,8741.0625,
                                  8639.3251953125,7815.4541015625,6381.66015625,
                                  4540.2666015625,2504.59375,282.81640625,
                                  -2280.490234375,-5253.634765625,-8553.63671875,
                                  -11960.9609375,-15231.08203125,-18229.734375,
                                  -20900.109375,-23274.734375),
                         `24` = c(-1252.013671875,-3479.412109375,-5961.509765625,
                                  -8673.060546875,-11600.1259765625,-14705.75,
                                  -17875.5625,-20890.69140625,-23633.18359375,
                                  -26174.224609375,-28569.80859375,
                                  -30840.6328125,-32959.28515625,-34813.3515625,
                                  -36321.56640625,-37482.96484375,-38267.54296875,
                                  -38539.234375,-38126.73046875,-36863.39453125)
                )
#>        1           2            3          4         10           11         12
#> 1  -10.0   -211.6133    952.90430  712.94727  2709.3413  3671.657227 1099.54639
#> 2   -9.5    604.0596     13.19824  301.94092  3149.5522  2823.466309  449.36035
#> 3   -9.0   1379.2295  -1079.62158  -81.09009  3289.4058  1924.058594   31.72168
#> 4   -8.5   2038.7388  -2253.38281 -349.46729  3152.7183   966.053223 -176.67334
#> 5   -8.0   2553.8340  -3488.69287 -472.59473  2810.4155    -3.071777 -126.19922
#> 6   -7.5   2894.5117  -4773.65332 -463.00732  2388.6636  -992.118530  152.19531
#> 7   -7.0   2959.9780  -6050.59863 -374.35449  1981.7455 -2028.095215  480.31055
#> 8   -6.5   2641.3975  -7237.23828 -243.77979  1571.4427 -3005.293701  737.37061
#> 9   -6.0   1930.2036  -8335.24805  -51.15527  1116.5012 -3832.771973  931.13184
#> 10  -5.5    914.6895  -9423.74512  230.17041   650.5560 -4541.802734 1133.40527
#> 11  -5.0   -366.2734 -10528.42090  658.13086   213.7044 -5155.244141 1428.36182
#> 12  -4.5  -1899.1465 -11574.42773 1267.30176  -206.1423 -5637.217773 1869.55298
#> 13  -4.0  -3592.6533 -12431.48242 2010.07129  -634.9294 -5908.791504 2473.44922
#> 14  -3.5  -5365.5068 -13036.71973 2795.23047 -1061.4197 -5892.758789 3248.67358
#> 15  -3.0  -7246.9551 -13461.89062 3553.76318 -1452.5642 -5541.027344 4199.32373
#> 16  -2.5  -9299.8164 -13797.75781 4323.63379 -1799.2917 -4881.625977 5278.00293
#> 17  -2.0 -11457.7051 -14045.18750 5165.39648 -2131.3542 -3990.479980 6400.59082
#> 18  -1.5 -13524.0117 -14139.12500 6083.32129 -2485.5652 -2935.747559 7468.81494
#> 19  -1.0 -15385.0039 -14058.33496 7091.21875 -2857.7351 -1783.959839 8377.72266
#> 20  -0.5 -17070.0059 -13865.10352 8220.83984 -3209.7217  -480.456177 9052.79688
#>             16         17         18         22          23         24
#> 1  -1071.40869 -971.36353  -295.5764  -810.5146   1370.3682  -1252.014
#> 2  -1801.21301 -467.32544 -1155.1279  2463.0889   3725.2363  -3479.412
#> 3  -2293.45850  -39.77979 -1944.7377  5718.8486   5685.6172  -5961.510
#> 4  -2573.59863  282.18628 -2598.0181  8787.7510   7182.8643  -8673.061
#> 5  -2733.87744  571.50293 -3116.0337 11539.2646   8205.9482 -11600.126
#> 6  -2861.20312  881.49072 -3562.5815 13961.7500   8741.0625 -14705.750
#> 7  -2966.48193 1189.89380 -3991.3149 16120.0098   8639.3252 -17875.562
#> 8  -2965.19238 1443.15015 -4384.3794 17962.9570   7815.4541 -20890.691
#> 9  -2768.06470 1645.10376 -4704.3584 19370.0762   6381.6602 -23633.184
#> 10 -2382.48438 1839.93677 -4880.3828 20226.3457   4540.2666 -26174.225
#> 11 -1898.98560 2050.70630 -4841.7080 20479.8301   2504.5938 -28569.809
#> 12 -1375.55347 2312.72363 -4636.3535 20183.7500    282.8164 -30840.633
#> 13  -752.82446 2647.95923 -4357.2656 19403.5195  -2280.4902 -32959.285
#> 14    46.83228 3010.14062 -4048.0234 18158.4531  -5253.6348 -34813.352
#> 15  1016.45996 3344.90234 -3744.7378 16456.8906  -8553.6367 -36321.566
#> 16  2093.45459 3675.73389 -3464.2830 14374.8242 -11960.9609 -37482.965
#> 17  3174.30469 4072.08398 -3220.8430 12091.8350 -15231.0820 -38267.543
#> 18  4155.47217 4540.46387 -3057.6582  9762.8740 -18229.7344 -38539.234
#> 19  4999.53613 5049.83545 -2926.3784  7410.6006 -20900.1094 -38126.730
#> 20  5727.30078 5531.43066 -2654.8101  4952.6714 -23274.7344 -36863.395

library(ggplot2)
ts.plot(data[c(1,2,10,16,22)], col=c("#3f97ff", "#62fdff", "#a67729", "#4c4c50") )
#> Error in data[c(1, 2, 10, 16, 22)]: object of type 'closure' is not subsettable
ts.plot(data[c(1,3,11,17,23)], col=c("#3f97ff", "#62fdff", "#a67729", "#4c4c50") )
#> Error in data[c(1, 3, 11, 17, 23)]: object of type 'closure' is not subsettable
ts.plot(data[c(1,4,12,18,24)], col=c("#3f97ff", "#62fdff", "#a67729", "#4c4c50") )
#> Error in data[c(1, 4, 12, 18, 24)]: object of type 'closure' is not subsettable

Created on 2022-06-13 by the reprex package (v2.0.1)

A few things:

  1. Take a look at the error message that you are getting when you try to plot your data:
    object of type closure is not subsettable
    That indicates that you never assigned any data to data, since there is an inbuilt function utils::data that comes loaded with R.

  2. It's not totally clear what elements you are trying to plot from your data frame. I would recommend reformatting your data according to tidy principles. For example, if you are trying to capture something about time, put time in its own column where each observation of time is its own row, instead of having time be represented in the column names. tidyr::pivot_longer and tidyr::pivot_wider can help with this.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.