Introduction to Plotly

Ann Arbor R User Group

Rami Krispin

September 14, 2017

Highlights

Introduction

Quick Poll

How many are familiar with:

Data Visualization

Data visualization is the art of exploring and explaining data using different graphical formats including interactive charts, statistical graphs, explanatory plots, etc. It can be divided into two main categories:

Key Data Visualization Packages in R

Motivation

Motivation

R base plot

plot(d$carat, d$price)

Motivation

With plotly

plot_ly(x = d$carat, y = d$price)

Motivation

And with some additional effort

plot_ly(d, x = ~carat, y = ~price, color = ~carat, name = "Diamonds Price", size = ~carat, text = ~paste("Clarity: ", clarity)) %>% 
  layout(title = "Diamonds Dataset - Price vs Carat", xaxis = list(title = "Carat"), yaxis = list(title = "Price"))

Motivation

Use the lasso option on the map!!!

## {
##   "visdat": {
##     "2318c50794272": ["function () ", "plotlyVisDat"]
##   },
##   "cur_data": "2318c50794272",
##   "attrs": {
##     "2318c50794272": {
##       "x": {},
##       "y": {},
##       "z": {},
##       "mode": "markers+lines",
##       "color": {},
##       "alpha": 1,
##       "sizes": [10, 100]
##     }
##   },
##   "layout": {
##     "margin": {
##       "b": 40,
##       "l": 60,
##       "t": 25,
##       "r": 10
##     },
##     "scene": {
##       "xaxis": {
##         "title": "pce"
##       },
##       "yaxis": {
##         "title": "psavert"
##       },
##       "zaxis": {
##         "title": "unemploy"
##       }
##     },
##     "xaxis": {
##       "domain": [0, 1]
##     },
##     "yaxis": {
##       "domain": [0, 1]
##     },
##     "hovermode": "closest",
##     "showlegend": false,
##     "legend": {
##       "y": 0.5,
##       "yanchor": "top"
##     }
##   },
##   "source": "A",
##   "config": {
##     "modeBarButtonsToAdd": [
##       {
##         "name": "Collaborate",
##         "icon": {
##           "width": 1000,
##           "ascent": 500,
##           "descent": -50,
##           "path": "M487 375c7-10 9-23 5-36l-79-259c-3-12-11-23-22-31-11-8-22-12-35-12l-263 0c-15 0-29 5-43 15-13 10-23 23-28 37-5 13-5 25-1 37 0 0 0 3 1 7 1 5 1 8 1 11 0 2 0 4-1 6 0 3-1 5-1 6 1 2 2 4 3 6 1 2 2 4 4 6 2 3 4 5 5 7 5 7 9 16 13 26 4 10 7 19 9 26 0 2 0 5 0 9-1 4-1 6 0 8 0 2 2 5 4 8 3 3 5 5 5 7 4 6 8 15 12 26 4 11 7 19 7 26 1 1 0 4 0 9-1 4-1 7 0 8 1 2 3 5 6 8 4 4 6 6 6 7 4 5 8 13 13 24 4 11 7 20 7 28 1 1 0 4 0 7-1 3-1 6-1 7 0 2 1 4 3 6 1 1 3 4 5 6 2 3 3 5 5 6 1 2 3 5 4 9 2 3 3 7 5 10 1 3 2 6 4 10 2 4 4 7 6 9 2 3 4 5 7 7 3 2 7 3 11 3 3 0 8 0 13-1l0-1c7 2 12 2 14 2l218 0c14 0 25-5 32-16 8-10 10-23 6-37l-79-259c-7-22-13-37-20-43-7-7-19-10-37-10l-248 0c-5 0-9-2-11-5-2-3-2-7 0-12 4-13 18-20 41-20l264 0c5 0 10 2 16 5 5 3 8 6 10 11l85 282c2 5 2 10 2 17 7-3 13-7 17-13z m-304 0c-1-3-1-5 0-7 1-1 3-2 6-2l174 0c2 0 4 1 7 2 2 2 4 4 5 7l6 18c0 3 0 5-1 7-1 1-3 2-6 2l-173 0c-3 0-5-1-8-2-2-2-4-4-4-7z m-24-73c-1-3-1-5 0-7 2-2 3-2 6-2l174 0c2 0 5 0 7 2 3 2 4 4 5 7l6 18c1 2 0 5-1 6-1 2-3 3-5 3l-174 0c-3 0-5-1-7-3-3-1-4-4-5-6z"
##         },
##         "click": "function(gd) { \n        // is this being viewed in RStudio?\n        if (location.search == '?viewer_pane=1') {\n          alert('To learn about plotly for collaboration, visit:\\n https://cpsievert.github.io/plotly_book/plot-ly-for-collaboration.html');\n        } else {\n          window.open('https://cpsievert.github.io/plotly_book/plot-ly-for-collaboration.html', '_blank');\n        }\n      }"
##       }
##     ],
##     "cloud": false
##   },
##   "data": [
##     {
##       "x": [507.4, 510.5, 516.3, 512.9, 518.1, 525.8, 531.5, 534.2, 544.9, 544.6, 550.4, 556.8, 563.8, 567.6, 568.8, 572.3, 577.4, 577.2, 584.2, 589.5, 589.7, 594.7, 601.1, 601.7, 603.5, 610.8, 614.1, 619.4, 621.4, 623.7, 629.6, 634.9, 633.2, 637, 643.4, 647.2, 649.5, 653.9, 660.1, 659.3, 657.6, 666.6, 677.2, 680.4, 683, 689.8, 692.2, 700.8, 699.9, 706, 714.1, 716.9, 722.1, 729.6, 732.6, 737.3, 750.4, 753.8, 759.2, 762.9, 771.2, 777.7, 782.5, 796.3, 801.8, 807.5, 817.9, 827.2, 834.2, 837.2, 843.1, 845.8, 855.7, 854.9, 870.9, 869.8, 878.6, 878.4, 886.4, 891.6, 903.3, 912.7, 924.3, 929.9, 939.8, 956.6, 956.8, 961, 958, 963.6, 977.4, 991.3, 992.6, 997.2, 1021.2, 1029.1, 1042.2, 1049.4, 1057.2, 1063.2, 1078, 1094.4, 1109.5, 1110.1, 1117.3, 1127.8, 1125.1, 1142.9, 1152.1, 1160.5, 1171.4, 1179.5, 1191.7, 1214.1, 1217.4, 1233.7, 1240.7, 1249.7, 1259.6, 1266.3, 1283.2, 1288.5, 1297.4, 1314.3, 1330, 1339.3, 1333, 1358.9, 1381.4, 1400.2, 1415.9, 1429.8, 1430.8, 1451, 1456.9, 1471, 1484.7, 1500.5, 1506.3, 1521.6, 1535, 1542.3, 1562.7, 1579.6, 1590.1, 1619.7, 1638.1, 1646, 1661.7, 1670.7, 1701.6, 1705.6, 1712.4, 1699.5, 1704.3, 1723, 1751.2, 1767.7, 1784.1, 1820.4, 1830.2, 1855.5, 1874.7, 1889.4, 1908.1, 1909.1, 1918.2, 1938.5, 1945.7, 1969.8, 1968.2, 1966.2, 1972.4, 1989.9, 1997.4, 2021.4, 2024.4, 2027.2, 2045.9, 2050.2, 2075.1, 2083.7, 2108.9, 2130.7, 2154.7, 2167.4, 2180.1, 2183.1, 2208.6, 2231.8, 2251, 2280.8, 2309, 2324.8, 2339.1, 2361.8, 2370.4, 2397.9, 2423.8, 2408.1, 2436.4, 2462.6, 2479.8, 2501.2, 2500.5, 2518.4, 2540.3, 2538.2, 2578.6, 2590, 2626.3, 2648.6, 2656.8, 2668.4, 2705.9, 2699.3, 2725.9, 2762.7, 2805.6, 2767.1, 2782.7, 2822.8, 2838.3, 2831.2, 2834.7, 2846.5, 2869, 2873.5, 2893.4, 2911.1, 2984.6, 2945.9, 2941.7, 3010.8, 2949.9, 3016.5, 3028.4, 3054.1, 3063.9, 3088.4, 3110.7, 3147, 3142.9, 3151.1, 3160.9, 3190.9, 3230.7, 3238.5, 3277.8, 3280.4, 3311, 3335.6, 3359.3, 3384.3, 3391.4, 3430.4, 3447, 3476.3, 3499.9, 3503.9, 3514.5, 3558.6, 3567.5, 3582.4, 3601.7, 3636.8, 3638.1, 3650, 3659.7, 3700.7, 3747.2, 3744.8, 3771.5, 3786.7, 3792.5, 3821.3, 3838.5, 3865, 3886.7, 3887.1, 3888.4, 3877.8, 3857.6, 3883.3, 3929.7, 3923.9, 3950, 3957.1, 3982.4, 3985.4, 4001.2, 3992.9, 4020.6, 4037.7, 4101.9, 4116.8, 4134.3, 4149, 4176.1, 4195, 4223, 4239.3, 4273.9, 4303.5, 4319.5, 4355.6, 4359.7, 4374.3, 4371.4, 4412.4, 4441.3, 4458.8, 4487.7, 4499.9, 4530.5, 4552, 4573.4, 4590.7, 4604.8, 4652.3, 4665.4, 4690.7, 4689.2, 4728.8, 4740.8, 4783, 4795.5, 4833.3, 4846, 4862.3, 4871.9, 4871.7, 4906.5, 4911.5, 4954.4, 4999, 4991.8, 5027.1, 5042.5, 5035.9, 5077.8, 5120.1, 5108.9, 5156.1, 5196.4, 5231.6, 5247.2, 5253.7, 5275.8, 5299, 5320, 5351.5, 5375, 5401.7, 5434.9, 5457.7, 5477.6, 5482.8, 5484.3, 5518.2, 5573, 5611.8, 5625.6, 5661.2, 5685.2, 5716.4, 5714.4, 5748.4, 5775, 5812.9, 5863.3, 5897.2, 5915.6, 5951, 5991.8, 6025.8, 6042.7, 6098.2, 6099.1, 6128.2, 6159.7, 6223.6, 6253.4, 6281.9, 6309.5, 6354.8, 6407.4, 6431.2, 6467.2, 6568.2, 6564.7, 6648.7, 6714.8, 6701, 6737.2, 6773.6, 6793.7, 6828.7, 6913.1, 6919.6, 6934.5, 6979.1, 7009.8, 7029.3, 7022.1, 7036.2, 7083.1, 7097.1, 7109.2, 7146.1, 7054.8, 7250.2, 7209.6, 7190, 7217.7, 7259.7, 7276.7, 7345.6, 7321.8, 7366.1, 7424.2, 7449, 7426.1, 7469.3, 7499.8, 7552.6, 7579.5, 7573.6, 7627.5, 7661.7, 7669.2, 7722.9, 7783.8, 7878.9, 7874, 7890.6, 7950.4, 7974.3, 8037.3, 8072.1, 8121, 8141.6, 8212.9, 8204.6, 8270.7, 8294.4, 8373, 8417.9, 8458.4, 8516.5, 8521.2, 8575.7, 8622.5, 8715.9, 8680.6, 8775.3, 8867.9, 8872.6, 8923.6, 8959.6, 8987.7, 9026.8, 9100.1, 9134.7, 9168.1, 9223.3, 9254.1, 9283.8, 9360.4, 9368.6, 9393.9, 9413.3, 9431.2, 9516.5, 9553.1, 9590.8, 9631.6, 9670.6, 9708.9, 9723.3, 9759.6, 9800.6, 9837.5, 9853.9, 9928.6, 9947.6, 9963.2, 9955.7, 10004.2, 10044.6, 10093.3, 10149.4, 10151.1, 10140.3, 10083.2, 9983.3, 9851.2, 9744.2, 9792.1, 9775.7, 9742.9, 9741.9, 9759.7, 9807.6, 9835.2, 9961.9, 9875.4, 9924.6, 9946.1, 10000.6, 10003.4, 10034.7, 10095.5, 10106.9, 10140.2, 10165.9, 10184.3, 10247.1, 10268.9, 10343.7, 10399.8, 10436.1, 10474.7, 10512.4, 10583.5, 10624.6, 10653.1, 10676.4, 10727.1, 10745.6, 10790.6, 10827.6, 10828.7, 10827.3, 10905.5, 10979.2, 10994.3, 11030.2, 11029, 11032.5, 11074.8, 11104.8, 11179.6, 11199.9, 11222.8, 11245.2, 11303.2, 11371.4, 11378.8, 11373.3, 11407.1, 11462.4, 11484.7, 11511.6, 11559.6, 11602.1, 11671.5, 11686.3, 11663.9, 11714.4, 11807.1, 11825.2, 11864.3, 11922.6, 11944.4, 12017, 12044.6, 12096.4, 12142.2, 12122, 12080.8, 12095.9, 12161.5, 12158.9],
##       "y": [12.5, 12.5, 11.7, 12.5, 12.5, 12.1, 11.7, 12.2, 11.6, 12.2, 12, 11.6, 10.6, 10.4, 10.4, 10.6, 10.4, 10.9, 10, 9.4, 9.9, 9.5, 10, 10.9, 11.7, 11.5, 11.5, 11.3, 11.5, 11.7, 11.7, 11.6, 12.3, 13.3, 12.3, 11.7, 13.2, 13.1, 12.9, 13, 13.3, 12.9, 13.1, 13.1, 13.3, 13, 13.4, 14.4, 13.6, 13.6, 12.9, 13, 12.8, 12.9, 12.4, 12.6, 11.5, 11.3, 11.5, 11.4, 11.4, 11.8, 12, 12.7, 13.4, 13.4, 12.1, 12.2, 12.4, 12.8, 12.8, 13.2, 12.8, 13.6, 12.8, 14, 14, 14.4, 14, 13.8, 13, 12.7, 12.3, 12.5, 12.7, 11.6, 12.3, 13, 13.4, 13.6, 12.8, 12.1, 12.3, 13.9, 17, 13.9, 12.3, 12.6, 12.6, 13, 12.3, 11.5, 11.3, 11.9, 11.8, 11.2, 11.8, 10.9, 11.2, 11.2, 10.8, 10.6, 10.9, 9.9, 9.8, 8.5, 9.8, 9.9, 9.8, 10.2, 10.1, 10.5, 10.7, 10.7, 10.8, 11, 11.4, 10.7, 10.5, 10.3, 9.8, 9.5, 10.3, 9.9, 10, 10.2, 10, 10, 10.6, 10.5, 10.7, 10.4, 9.8, 9.3, 10, 9.2, 8.9, 9.3, 9.4, 9.8, 9.6, 9.8, 9.8, 10.5, 10.6, 10.4, 10.5, 10.6, 11.1, 11, 11.5, 11.2, 10.5, 10.4, 10.3, 10.3, 10.4, 10.2, 11.7, 11.4, 11.9, 12.5, 12.7, 12, 12.2, 11.6, 11.7, 12.4, 11.7, 11.6, 12, 11.9, 11.1, 10.7, 10.3, 10.3, 10.4, 10.5, 10, 9.6, 9.4, 8.5, 9, 8.7, 9, 9.1, 9.7, 9.5, 9.4, 11.1, 10.9, 10.9, 10.5, 10.5, 11, 11.2, 11.2, 11.2, 10.3, 10.6, 9.7, 8.5, 8.1, 9.4, 10.5, 9, 8.5, 7.5, 6.7, 8.5, 8.4, 8, 8, 8.7, 9.3, 9.1, 8.6, 8.8, 8.7, 8.4, 6.6, 7.8, 8.2, 6.4, 9.1, 7.9, 7.9, 3.8, 7.5, 7, 6.8, 6.5, 7, 7.6, 7.9, 8, 7.5, 7.9, 7.5, 8.1, 7.7, 7.8, 7.9, 7.8, 8.2, 8, 7.7, 7.7, 8, 8.4, 8.9, 7.9, 7.6, 7.6, 7.7, 7.1, 7.5, 7.9, 8, 7.2, 7.4, 8.1, 7.8, 8.2, 8, 7.9, 8, 7.5, 7.5, 7.3, 7.4, 8.2, 8.7, 8.3, 7.4, 8, 7.7, 8.2, 7.6, 7.9, 8.1, 8.6, 8.4, 9.1, 8.8, 9.2, 9.2, 9.4, 9.4, 9.5, 9.1, 9.2, 8.2, 7.4, 7.3, 9.9, 8.3, 8.4, 8.3, 8.2, 7.7, 7.2, 7, 7.1, 6.3, 5.6, 5.6, 8.4, 6.4, 5.9, 6.2, 5.8, 7.1, 6.3, 6.4, 5.9, 6.2, 6.5, 6.4, 6.5, 6.9, 7.3, 7, 6.3, 6.5, 6.1, 6.4, 6.1, 6.1, 6.5, 6, 5.5, 6.1, 6.1, 6, 5, 6.1, 6.5, 6.1, 5.9, 6, 5.8, 5.8, 5.7, 5.6, 5.7, 5.8, 5.9, 6.2, 6, 5.5, 5.4, 5.6, 5.6, 5.8, 5.8, 6.7, 6.8, 6.9, 6.6, 6.3, 6.2, 6.3, 6.2, 5.8, 5.6, 5.7, 5.2, 5.7, 5.6, 5.3, 4.5, 4.3, 4.2, 4.1, 4, 3.5, 3.9, 4.1, 3.7, 4.7, 4.2, 3.9, 4.4, 4.2, 4.2, 4.6, 4.6, 3.8, 4, 3.8, 3.5, 4, 4.1, 4.5, 4.3, 3.7, 3.7, 5, 6.1, 6.3, 2.7, 3.4, 3.8, 5.6, 5.3, 5.3, 5.1, 5.6, 5.4, 4.6, 4.4, 4.9, 4.7, 4.7, 4.5, 4.5, 4.7, 4.6, 4.6, 5.1, 4.9, 5.5, 5.3, 4.5, 4.6, 4.7, 4.8, 4.5, 4.5, 4.5, 4.7, 4.7, 5, 4.5, 4.6, 3.9, 3.8, 3.4, 6.3, 2.9, 2.7, 2.7, 2.1, 3, 2.2, 1.9, 2.4, 2.3, 2.6, 2.7, 2.8, 3.7, 3.8, 3.7, 3.4, 3.2, 3.4, 2.9, 3, 3, 3.1, 3.2, 3, 3, 3.3, 3.6, 3.2, 3, 2.8, 2.8, 2.6, 2.8, 2.8, 2.5, 3, 3.4, 3.9, 4, 3.5, 7.9, 5.6, 4.4, 3.7, 4.4, 5.4, 6.3, 6.5, 6.5, 5.9, 6.1, 6.7, 8.1, 6.7, 6, 4.9, 5.9, 5.4, 5.7, 5.7, 5.6, 5.2, 5, 5.6, 6, 5.9, 5.9, 5.8, 5.6, 5.4, 5.3, 5.9, 6.2, 6.4, 6, 5.9, 5.9, 6.1, 6.3, 6.2, 5.7, 5.5, 5.6, 6.4, 6.6, 6.7, 6.9, 7, 7, 7.1, 6.6, 6.4, 6.5, 7.1, 8.2, 10.5, 4.5, 4.7, 4.9, 5.1, 5.2, 5.3, 5.1, 5.3, 5.2, 4.7, 4.3, 4.1, 4.9, 5, 4.8, 5, 5.1, 5.1, 5.1, 4.7, 4.6, 4.6, 4.5, 5, 5.5, 5.7, 5.2, 5.6],
##       "z": [2944, 2945, 2958, 3143, 3066, 3018, 2878, 3001, 2877, 2709, 2740, 2938, 2883, 2768, 2686, 2689, 2715, 2685, 2718, 2692, 2712, 2758, 2713, 2816, 2868, 2856, 3040, 3049, 2856, 2884, 3201, 3453, 3635, 3797, 3919, 4071, 4175, 4256, 4456, 4591, 4898, 5076, 4986, 4903, 4987, 4959, 4996, 4949, 5035, 5134, 5042, 4954, 5161, 5154, 5019, 4928, 5038, 4959, 4922, 4923, 4913, 4939, 4849, 4875, 4602, 4543, 4326, 4452, 4394, 4459, 4329, 4363, 4305, 4305, 4350, 4144, 4396, 4489, 4644, 4731, 4634, 4618, 4705, 4927, 5063, 5022, 5437, 5523, 6140, 6636, 7501, 7520, 7978, 8210, 8433, 8220, 8127, 7928, 7923, 7897, 7794, 7744, 7534, 7326, 7230, 7330, 7053, 7322, 7490, 7518, 7380, 7430, 7620, 7545, 7280, 7443, 7307, 7059, 6911, 7134, 6829, 6925, 6751, 6763, 6815, 6386, 6489, 6318, 6337, 6180, 6127, 6028, 6309, 6080, 6125, 5947, 6077, 6228, 6109, 6173, 6109, 6069, 5840, 5959, 5996, 6320, 6190, 6296, 6238, 6325, 6683, 6702, 6729, 7358, 7984, 8098, 8363, 8281, 8021, 8088, 8023, 7718, 8071, 8051, 7982, 7869, 8174, 8098, 7863, 8036, 8230, 8646, 9029, 9267, 9397, 9705, 9895, 10244, 10335, 10538, 10849, 10881, 11217, 11529, 11938, 12051, 11534, 11545, 11408, 11268, 11154, 11246, 10548, 10623, 10282, 9887, 9499, 9331, 9008, 8791, 8746, 8762, 8456, 8226, 8537, 8519, 8367, 8381, 8198, 8358, 8423, 8321, 8339, 8395, 8302, 8460, 8513, 8196, 8248, 8298, 8128, 8138, 7795, 8402, 8383, 8364, 8439, 8508, 8319, 8135, 8310, 8243, 8159, 7883, 7892, 7865, 7862, 7542, 7574, 7398, 7268, 7261, 7102, 7227, 7035, 6936, 6953, 6929, 6876, 6601, 6779, 6546, 6605, 6843, 6604, 6568, 6537, 6518, 6682, 6359, 6205, 6468, 6375, 6577, 6495, 6511, 6590, 6630, 6725, 6667, 6752, 6651, 6598, 6797, 6742, 6590, 6922, 7188, 7368, 7459, 7764, 7901, 8015, 8265, 8586, 8439, 8736, 8692, 8586, 8666, 8722, 8842, 8931, 9198, 9283, 9454, 9460, 9415, 9744, 10040, 9850, 9787, 9781, 9398, 9565, 9557, 9325, 9183, 9056, 9110, 9149, 9121, 8930, 8763, 8714, 8750, 8542, 8477, 8630, 8583, 8470, 8331, 7915, 7927, 7946, 7933, 7734, 7632, 7375, 7230, 7375, 7187, 7153, 7645, 7430, 7427, 7527, 7484, 7478, 7328, 7426, 7423, 7491, 7313, 7318, 7415, 7423, 7095, 7337, 6882, 6979, 7031, 7236, 7253, 7158, 7102, 7000, 6873, 6655, 6799, 6655, 6608, 6656, 6454, 6308, 6476, 6368, 6306, 6422, 5941, 6047, 6212, 6259, 6179, 6300, 6280, 6100, 6032, 5976, 6111, 5783, 6004, 5796, 5951, 6025, 5838, 5915, 5778, 5716, 5653, 5708, 5858, 5733, 5481, 5758, 5651, 5747, 5853, 5625, 5534, 5639, 5634, 6023, 6089, 6141, 6271, 6226, 6484, 6583, 7042, 7142, 7694, 8003, 8258, 8182, 8215, 8304, 8599, 8399, 8393, 8390, 8304, 8251, 8307, 8520, 8640, 8520, 8618, 8588, 8842, 8957, 9266, 9011, 8896, 8921, 8732, 8576, 8317, 8370, 8167, 8491, 8170, 8212, 8286, 8136, 7990, 7927, 8061, 7932, 7934, 7784, 7980, 7737, 7672, 7651, 7524, 7406, 7345, 7553, 7453, 7566, 7279, 7064, 7184, 7072, 7120, 6980, 7001, 7175, 7091, 6847, 6727, 6872, 6762, 7116, 6927, 6731, 6850, 6766, 6979, 7149, 7067, 7170, 7237, 7240, 7645, 7685, 7497, 7822, 7637, 8395, 8575, 8937, 9438, 9494, 10074, 10538, 11286, 12058, 12898, 13426, 13853, 14499, 14707, 14601, 14814, 15009, 15352, 15219, 15098, 15046, 15113, 15202, 15325, 14849, 14474, 14512, 14648, 14579, 14516, 15081, 14348, 14046, 13828, 13728, 13956, 13853, 13958, 13756, 13806, 13929, 13599, 13309, 13071, 12812, 12828, 12696, 12636, 12668, 12688, 12657, 12449, 12106, 12141, 12026, 12272, 12497, 11967, 11653, 11735, 11671, 11736, 11357, 11241, 11251, 11161, 10814, 10376, 10280, 10387, 10384, 9696, 9761, 9453, 9648, 9568, 9237, 8983, 9071, 8688, 8979, 8705, 8575, 8549],
##       "mode": "markers+lines",
##       "type": "scatter3d",
##       "line": {
##         "colorbar": {
##           "title": "as.numeric(date)",
##           "ticklen": 2
##         },
##         "cmin": -915,
##         "cmax": 16526,
##         "colorscale": [
##           ["0", "rgba(68,1,84,1)"],
##           ["0.0416977237543719", "rgba(70,19,97,1)"],
##           ["0.0833381113468264", "rgba(72,32,111,1)"],
##           ["0.125035835101198", "rgba(71,45,122,1)"],
##           ["0.166676222693653", "rgba(68,58,128,1)"],
##           ["0.208373946448025", "rgba(64,70,135,1)"],
##           ["0.250014334040479", "rgba(60,82,138,1)"],
##           ["0.291712057794851", "rgba(56,93,140,1)"],
##           ["0.333352445387306", "rgba(49,104,142,1)"],
##           ["0.375043002121438", "rgba(46,114,142,1)"],
##           ["0.416676222693653", "rgba(42,123,142,1)"],
##           ["0.458366779427785", "rgba(38,133,141,1)"],
##           ["0.5", "rgba(37,144,140,1)"],
##           ["0.541690556734132", "rgba(33,154,138,1)"],
##           ["0.583323777306347", "rgba(39,164,133,1)"],
##           ["0.625014334040479", "rgba(47,174,127,1)"],
##           ["0.666647554612694", "rgba(53,183,121,1)"],
##           ["0.708345278367066", "rgba(79,191,110,1)"],
##           ["0.749985665959521", "rgba(98,199,98,1)"],
##           ["0.791683389713893", "rgba(119,207,85,1)"],
##           ["0.833323777306347", "rgba(147,214,70,1)"],
##           ["0.875021501060719", "rgba(172,220,52,1)"],
##           ["0.916661888653174", "rgba(199,225,42,1)"],
##           ["0.958359612407545", "rgba(226,228,40,1)"],
##           ["1", "rgba(253,231,37,1)"]
##         ],
##         "showscale": false,
##         "color": [-915, -884, -853, -823, -792, -762, -731, -700, -671, -640, -610, -579, -549, -518, -487, -457, -426, -396, -365, -334, -306, -275, -245, -214, -184, -153, -122, -92, -61, -31, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396, 424, 455, 485, 516, 546, 577, 608, 638, 669, 699, 730, 761, 790, 821, 851, 882, 912, 943, 974, 1004, 1035, 1065, 1096, 1127, 1155, 1186, 1216, 1247, 1277, 1308, 1339, 1369, 1400, 1430, 1461, 1492, 1520, 1551, 1581, 1612, 1642, 1673, 1704, 1734, 1765, 1795, 1826, 1857, 1885, 1916, 1946, 1977, 2007, 2038, 2069, 2099, 2130, 2160, 2191, 2222, 2251, 2282, 2312, 2343, 2373, 2404, 2435, 2465, 2496, 2526, 2557, 2588, 2616, 2647, 2677, 2708, 2738, 2769, 2800, 2830, 2861, 2891, 2922, 2953, 2981, 3012, 3042, 3073, 3103, 3134, 3165, 3195, 3226, 3256, 3287, 3318, 3346, 3377, 3407, 3438, 3468, 3499, 3530, 3560, 3591, 3621, 3652, 3683, 3712, 3743, 3773, 3804, 3834, 3865, 3896, 3926, 3957, 3987, 4018, 4049, 4077, 4108, 4138, 4169, 4199, 4230, 4261, 4291, 4322, 4352, 4383, 4414, 4442, 4473, 4503, 4534, 4564, 4595, 4626, 4656, 4687, 4717, 4748, 4779, 4807, 4838, 4868, 4899, 4929, 4960, 4991, 5021, 5052, 5082, 5113, 5144, 5173, 5204, 5234, 5265, 5295, 5326, 5357, 5387, 5418, 5448, 5479, 5510, 5538, 5569, 5599, 5630, 5660, 5691, 5722, 5752, 5783, 5813, 5844, 5875, 5903, 5934, 5964, 5995, 6025, 6056, 6087, 6117, 6148, 6178, 6209, 6240, 6268, 6299, 6329, 6360, 6390, 6421, 6452, 6482, 6513, 6543, 6574, 6605, 6634, 6665, 6695, 6726, 6756, 6787, 6818, 6848, 6879, 6909, 6940, 6971, 6999, 7030, 7060, 7091, 7121, 7152, 7183, 7213, 7244, 7274, 7305, 7336, 7364, 7395, 7425, 7456, 7486, 7517, 7548, 7578, 7609, 7639, 7670, 7701, 7729, 7760, 7790, 7821, 7851, 7882, 7913, 7943, 7974, 8004, 8035, 8066, 8095, 8126, 8156, 8187, 8217, 8248, 8279, 8309, 8340, 8370, 8401, 8432, 8460, 8491, 8521, 8552, 8582, 8613, 8644, 8674, 8705, 8735, 8766, 8797, 8825, 8856, 8886, 8917, 8947, 8978, 9009, 9039, 9070, 9100, 9131, 9162, 9190, 9221, 9251, 9282, 9312, 9343, 9374, 9404, 9435, 9465, 9496, 9527, 9556, 9587, 9617, 9648, 9678, 9709, 9740, 9770, 9801, 9831, 9862, 9893, 9921, 9952, 9982, 10013, 10043, 10074, 10105, 10135, 10166, 10196, 10227, 10258, 10286, 10317, 10347, 10378, 10408, 10439, 10470, 10500, 10531, 10561, 10592, 10623, 10651, 10682, 10712, 10743, 10773, 10804, 10835, 10865, 10896, 10926, 10957, 10988, 11017, 11048, 11078, 11109, 11139, 11170, 11201, 11231, 11262, 11292, 11323, 11354, 11382, 11413, 11443, 11474, 11504, 11535, 11566, 11596, 11627, 11657, 11688, 11719, 11747, 11778, 11808, 11839, 11869, 11900, 11931, 11961, 11992, 12022, 12053, 12084, 12112, 12143, 12173, 12204, 12234, 12265, 12296, 12326, 12357, 12387, 12418, 12449, 12478, 12509, 12539, 12570, 12600, 12631, 12662, 12692, 12723, 12753, 12784, 12815, 12843, 12874, 12904, 12935, 12965, 12996, 13027, 13057, 13088, 13118, 13149, 13180, 13208, 13239, 13269, 13300, 13330, 13361, 13392, 13422, 13453, 13483, 13514, 13545, 13573, 13604, 13634, 13665, 13695, 13726, 13757, 13787, 13818, 13848, 13879, 13910, 13939, 13970, 14000, 14031, 14061, 14092, 14123, 14153, 14184, 14214, 14245, 14276, 14304, 14335, 14365, 14396, 14426, 14457, 14488, 14518, 14549, 14579, 14610, 14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944, 14975, 15006, 15034, 15065, 15095, 15126, 15156, 15187, 15218, 15248, 15279, 15309, 15340, 15371, 15400, 15431, 15461, 15492, 15522, 15553, 15584, 15614, 15645, 15675, 15706, 15737, 15765, 15796, 15826, 15857, 15887, 15918, 15949, 15979, 16010, 16040, 16071, 16102, 16130, 16161, 16191, 16222, 16252, 16283, 16314, 16344, 16375, 16405, 16436, 16467, 16495, 16526]
##       },
##       "marker": {
##         "colorbar": {
##           "title": "as.numeric(date)",
##           "ticklen": 2
##         },
##         "cmin": -915,
##         "cmax": 16526,
##         "colorscale": [
##           ["0", "rgba(68,1,84,1)"],
##           ["0.0416977237543719", "rgba(70,19,97,1)"],
##           ["0.0833381113468264", "rgba(72,32,111,1)"],
##           ["0.125035835101198", "rgba(71,45,122,1)"],
##           ["0.166676222693653", "rgba(68,58,128,1)"],
##           ["0.208373946448025", "rgba(64,70,135,1)"],
##           ["0.250014334040479", "rgba(60,82,138,1)"],
##           ["0.291712057794851", "rgba(56,93,140,1)"],
##           ["0.333352445387306", "rgba(49,104,142,1)"],
##           ["0.375043002121438", "rgba(46,114,142,1)"],
##           ["0.416676222693653", "rgba(42,123,142,1)"],
##           ["0.458366779427785", "rgba(38,133,141,1)"],
##           ["0.5", "rgba(37,144,140,1)"],
##           ["0.541690556734132", "rgba(33,154,138,1)"],
##           ["0.583323777306347", "rgba(39,164,133,1)"],
##           ["0.625014334040479", "rgba(47,174,127,1)"],
##           ["0.666647554612694", "rgba(53,183,121,1)"],
##           ["0.708345278367066", "rgba(79,191,110,1)"],
##           ["0.749985665959521", "rgba(98,199,98,1)"],
##           ["0.791683389713893", "rgba(119,207,85,1)"],
##           ["0.833323777306347", "rgba(147,214,70,1)"],
##           ["0.875021501060719", "rgba(172,220,52,1)"],
##           ["0.916661888653174", "rgba(199,225,42,1)"],
##           ["0.958359612407545", "rgba(226,228,40,1)"],
##           ["1", "rgba(253,231,37,1)"]
##         ],
##         "showscale": false,
##         "color": [-915, -884, -853, -823, -792, -762, -731, -700, -671, -640, -610, -579, -549, -518, -487, -457, -426, -396, -365, -334, -306, -275, -245, -214, -184, -153, -122, -92, -61, -31, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 396, 424, 455, 485, 516, 546, 577, 608, 638, 669, 699, 730, 761, 790, 821, 851, 882, 912, 943, 974, 1004, 1035, 1065, 1096, 1127, 1155, 1186, 1216, 1247, 1277, 1308, 1339, 1369, 1400, 1430, 1461, 1492, 1520, 1551, 1581, 1612, 1642, 1673, 1704, 1734, 1765, 1795, 1826, 1857, 1885, 1916, 1946, 1977, 2007, 2038, 2069, 2099, 2130, 2160, 2191, 2222, 2251, 2282, 2312, 2343, 2373, 2404, 2435, 2465, 2496, 2526, 2557, 2588, 2616, 2647, 2677, 2708, 2738, 2769, 2800, 2830, 2861, 2891, 2922, 2953, 2981, 3012, 3042, 3073, 3103, 3134, 3165, 3195, 3226, 3256, 3287, 3318, 3346, 3377, 3407, 3438, 3468, 3499, 3530, 3560, 3591, 3621, 3652, 3683, 3712, 3743, 3773, 3804, 3834, 3865, 3896, 3926, 3957, 3987, 4018, 4049, 4077, 4108, 4138, 4169, 4199, 4230, 4261, 4291, 4322, 4352, 4383, 4414, 4442, 4473, 4503, 4534, 4564, 4595, 4626, 4656, 4687, 4717, 4748, 4779, 4807, 4838, 4868, 4899, 4929, 4960, 4991, 5021, 5052, 5082, 5113, 5144, 5173, 5204, 5234, 5265, 5295, 5326, 5357, 5387, 5418, 5448, 5479, 5510, 5538, 5569, 5599, 5630, 5660, 5691, 5722, 5752, 5783, 5813, 5844, 5875, 5903, 5934, 5964, 5995, 6025, 6056, 6087, 6117, 6148, 6178, 6209, 6240, 6268, 6299, 6329, 6360, 6390, 6421, 6452, 6482, 6513, 6543, 6574, 6605, 6634, 6665, 6695, 6726, 6756, 6787, 6818, 6848, 6879, 6909, 6940, 6971, 6999, 7030, 7060, 7091, 7121, 7152, 7183, 7213, 7244, 7274, 7305, 7336, 7364, 7395, 7425, 7456, 7486, 7517, 7548, 7578, 7609, 7639, 7670, 7701, 7729, 7760, 7790, 7821, 7851, 7882, 7913, 7943, 7974, 8004, 8035, 8066, 8095, 8126, 8156, 8187, 8217, 8248, 8279, 8309, 8340, 8370, 8401, 8432, 8460, 8491, 8521, 8552, 8582, 8613, 8644, 8674, 8705, 8735, 8766, 8797, 8825, 8856, 8886, 8917, 8947, 8978, 9009, 9039, 9070, 9100, 9131, 9162, 9190, 9221, 9251, 9282, 9312, 9343, 9374, 9404, 9435, 9465, 9496, 9527, 9556, 9587, 9617, 9648, 9678, 9709, 9740, 9770, 9801, 9831, 9862, 9893, 9921, 9952, 9982, 10013, 10043, 10074, 10105, 10135, 10166, 10196, 10227, 10258, 10286, 10317, 10347, 10378, 10408, 10439, 10470, 10500, 10531, 10561, 10592, 10623, 10651, 10682, 10712, 10743, 10773, 10804, 10835, 10865, 10896, 10926, 10957, 10988, 11017, 11048, 11078, 11109, 11139, 11170, 11201, 11231, 11262, 11292, 11323, 11354, 11382, 11413, 11443, 11474, 11504, 11535, 11566, 11596, 11627, 11657, 11688, 11719, 11747, 11778, 11808, 11839, 11869, 11900, 11931, 11961, 11992, 12022, 12053, 12084, 12112, 12143, 12173, 12204, 12234, 12265, 12296, 12326, 12357, 12387, 12418, 12449, 12478, 12509, 12539, 12570, 12600, 12631, 12662, 12692, 12723, 12753, 12784, 12815, 12843, 12874, 12904, 12935, 12965, 12996, 13027, 13057, 13088, 13118, 13149, 13180, 13208, 13239, 13269, 13300, 13330, 13361, 13392, 13422, 13453, 13483, 13514, 13545, 13573, 13604, 13634, 13665, 13695, 13726, 13757, 13787, 13818, 13848, 13879, 13910, 13939, 13970, 14000, 14031, 14061, 14092, 14123, 14153, 14184, 14214, 14245, 14276, 14304, 14335, 14365, 14396, 14426, 14457, 14488, 14518, 14549, 14579, 14610, 14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944, 14975, 15006, 15034, 15065, 15095, 15126, 15156, 15187, 15218, 15248, 15279, 15309, 15340, 15371, 15400, 15431, 15461, 15492, 15522, 15553, 15584, 15614, 15645, 15675, 15706, 15737, 15765, 15796, 15826, 15857, 15887, 15918, 15949, 15979, 16010, 16040, 16071, 16102, 16130, 16161, 16191, 16222, 16252, 16283, 16314, 16344, 16375, 16405, 16436, 16467, 16495, 16526],
##         "line": {
##           "color": "transparent"
##         }
##       },
##       "frame": null
##     },
##     {
##       "x": [507.4, 12161.5],
##       "y": [1.9, 17],
##       "type": "scatter3d",
##       "mode": "markers",
##       "opacity": 0,
##       "hoverinfo": "none",
##       "showlegend": false,
##       "marker": {
##         "colorbar": {
##           "title": "as.numeric(date)",
##           "ticklen": 2,
##           "len": 0.5,
##           "y": 1,
##           "lenmode": "fraction",
##           "yanchor": "top"
##         },
##         "cmin": -915,
##         "cmax": 16526,
##         "colorscale": [
##           ["0", "rgba(68,1,84,1)"],
##           ["0.0416977237543719", "rgba(70,19,97,1)"],
##           ["0.0833381113468264", "rgba(72,32,111,1)"],
##           ["0.125035835101198", "rgba(71,45,122,1)"],
##           ["0.166676222693653", "rgba(68,58,128,1)"],
##           ["0.208373946448025", "rgba(64,70,135,1)"],
##           ["0.250014334040479", "rgba(60,82,138,1)"],
##           ["0.291712057794851", "rgba(56,93,140,1)"],
##           ["0.333352445387306", "rgba(49,104,142,1)"],
##           ["0.375043002121438", "rgba(46,114,142,1)"],
##           ["0.416676222693653", "rgba(42,123,142,1)"],
##           ["0.458366779427785", "rgba(38,133,141,1)"],
##           ["0.5", "rgba(37,144,140,1)"],
##           ["0.541690556734132", "rgba(33,154,138,1)"],
##           ["0.583323777306347", "rgba(39,164,133,1)"],
##           ["0.625014334040479", "rgba(47,174,127,1)"],
##           ["0.666647554612694", "rgba(53,183,121,1)"],
##           ["0.708345278367066", "rgba(79,191,110,1)"],
##           ["0.749985665959521", "rgba(98,199,98,1)"],
##           ["0.791683389713893", "rgba(119,207,85,1)"],
##           ["0.833323777306347", "rgba(147,214,70,1)"],
##           ["0.875021501060719", "rgba(172,220,52,1)"],
##           ["0.916661888653174", "rgba(199,225,42,1)"],
##           ["0.958359612407545", "rgba(226,228,40,1)"],
##           ["1", "rgba(253,231,37,1)"]
##         ],
##         "showscale": true,
##         "color": [-915, 16526]
##       },
##       "z": [2685, 15352],
##       "frame": null
##     }
##   ],
##   "highlight": {
##     "on": "plotly_click",
##     "persistent": false,
##     "dynamic": false,
##     "selectize": false,
##     "opacityDim": 0.2,
##     "selected": {
##       "opacity": 1
##     }
##   },
##   "base_url": "https://plot.ly"
## }

Motivation

Linked animated views

Motivation

3D Surface Plot

Plotly - Introduction

Plotly - Introduction

What is so great about plotly:

What I wish was avilable:

Vehicle Sales Dataset - Demo

Will demo the uses of plotly for exploratory analysis and to communicate the results utilizing key function of the package.

Data:

Key functions:

Vehicle Sales Dataset - Data Prep

Data prep:

# Loading the data from FRED
df.sales <- Quandl("FRED/TOTALNSA", type = "raw") #Total Vehicle Sales
df.unemp <- Quandl("FRED/UNRATENSA", type = "raw") #Unemployment Rate
names(df.unemp) <- c("Date", "UnRate")
# Flaging years where the sales droped compared to the previuos year
df <- df.sales %>% arrange(Date) %>% 
  mutate(year = lubridate::year(Date)) 
df.y <- group_by(df,year) %>%  # Calculate the yearly Sales
       summarise(sales.y = sum(Value))
df.y$delta.flag <- ifelse((df.y$sales.y - 
                          c(NA, df.y$sales.y[-nrow(df.y)])) < 0, 
                          1,0 ) 
df.y$delta.flag <- ifelse(is.na(df.y$delta.flag), 0, 
                          df.y$delta.flag) # Modifing NA's 
df1 <- df %>% left_join(df.y) %>% # Merging vehical sales 
              left_join(df.unemp) # and unemployment rate

Vehicle Sales Dataset - Time Series Plot

Simple time series plot

p <- plot_ly(data = df1)%>%
  add_lines(x = ~ Date, y = ~ Value, mode = "line", name = "Vehical Sales")
p

Vehicle Sales Dataset - Time Series Plot

Add titles:

p <- plot_ly(data = df1)%>%
  add_lines(x = ~ Date, y = ~ Value, mode = "line", name = "Vehical Sales") %>%
  layout(
    title = "Total Vehicle Sales",
    xaxis = list(title = "Source: U.S. Bureau of Economic Analysis"),
    yaxis = list(title = "Millions of Units"))

Vehicle Sales Dataset - Time Series Plot

Vehicle Sales Dataset - Shapes

Shapes function:

s <- e <- NULL
s.flag <- 0
e.flag <- 0
rect.list <- NULL
for(i in 2:nrow(df1)){
  if(df1$delta.flag[i] == 1 &  df1$delta.flag[i - 1] == 0){
    s <- df1$Date[i]
    s.flag <- 1 }
  
  if(df1$delta.flag[i] == 0 &  df1$delta.flag[i - 1] == 1){
    e <- df1$Date[i - 1]
    e.flag <- 1}
  
  if(s.flag == 1 & e.flag == 1){
   if(is.null(rect.list)){
     rect.list[[1]] <- list(type = "rect",
            fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
            x0 = s, x1 = e, xref = "x",
            y0 = 0, y1 = max(df1$Value)*1.05, yref = "y"
       )
   } else {
     rect.list[[length(rect.list) + 1]] <- list(type = "rect",
            fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
            x0 = s, x1 = e, xref = "x",
            y0 = 0, y1 = max(df1$Value)*1.05, yref = "y")
   }
    s <- e <- NULL
    s.flag <- e.flag <- 0
  }
}

Vehicle Sales Dataset - Shapes

p <- p %>% layout(shapes = list(
  rect.list[[1]],
  rect.list[[2]],
  rect.list[[3]],
  rect.list[[4]],
  rect.list[[5]],
  rect.list[[6]]))

Vehicle Sales Dataset - Shapes

Vehicle Sales Dataset - Second Axis

Using multiple axis:

p1 <- plot_ly(data = df1)%>%
  add_lines(x = ~ Date, y = ~ Value, mode = "line", name = "Vehical Sales") %>%
  add_trace(x = ~ Date, y = ~UnRate, name = "Unemployment Rate", yaxis = "y2",  
            mode = 'dash', line = list(color = "red")) %>%
  layout(
    title = "Total Vehicle Sales",
    xaxis = list(title = "Source: U.S. Bureau of Economic Analysis"),
    yaxis = list(title = "Millions of Units",
                 range = c(min(df1$Value) * 0.95, max(df1$Value) * 1.05)),# Set the y axis range
    # add rectangle shapes
    shapes = list(
      rect.list[[1]], rect.list[[2]], rect.list[[3]],
      rect.list[[4]], rect.list[[5]], rect.list[[6]]
    ),
    # Defind the second y axis parameters
    yaxis2 = list(
      tickfont = list(color = "red"),
      overlaying = "y",
      side = "right",
      title = "Percentage"
    )
  )

Vehicle Sales Dataset - Second Axis

Vehicle Sales Dataset - Seasonality Analysis

Prepare the data for time series analysis:

sales.ts <- window(ts(df1$Value, 
                   start = c(lubridate::year(min(df1$Date)), 
                             lubridate::month(min(df1$Date))),
                   frequency = 12
                   ), start = c(2010,1)
)

ts.df <- data.frame(dec_left = floor(time(sales.ts)),
                    dec_right = round((time(sales.ts ) - floor(time(sales.ts))) * 
                                        frequency(sales.ts ) + 1), 
                    value = as.numeric(sales.ts))

ts.df_wide <- reshape2::dcast(ts.df, dec_right ~ dec_left )

Vehicle Sales Dataset - Seasonality Analysis

Creating seasonal plot:

p <- plot_ly()
for(f in 2:ncol(ts.df_wide)){
  p <- p %>% add_trace(x = ts.df_wide[,1], y = ts.df_wide[,f],
                       name = names(ts.df_wide)[f],
                       mode = "line")}
p <- p %>%  layout(
    title = "Total Vehicle Sales by Year",
    xaxis = list(title = "Source: U.S. Bureau of Economic Analysis"),
    yaxis = list(title = "Millions of Units"))

Vehicle Sales Dataset - Seasonality Analysis

Seasonal plot:

Vehicle Sales Dataset - Seasonality Analysis

Lags plot:

lag <- NULL
lag_plots <- NULL 
max.lags <- 12
for(g in 1:max.lags){
  if(g == 1){
    lag <- c(NA, ts.df$value[- nrow(ts.df)]) 
  } else {
    lag <- c(NA,lag[-nrow(ts.df)])
  }
  lag_plots[[g]] <- plot_ly(x = lag, y = ts.df$value, 
                            name = paste("Lag", g, sep = " ")) %>%
    layout(xaxis = list(title = paste("Lag", g, sep = " "),
                        range = c( min(na.omit(as.numeric(lag))),  
                                   max(na.omit(as.numeric(lag))))),
           yaxis = list(title = paste("Series", sep = ""),
                        range = c( min(na.omit(as.numeric(ts.df$value))),  
                                   max(na.omit(as.numeric(ts.df$value))))),
           title = paste("Series vs Lags", sep = " "),
           annotations = list(
             showarrow = FALSE,
             xref = paste("x", g, sep = ""),
             yref = paste("y", g, sep = ""),
             text = paste("Lag", g, sep = " "))
    )
}

p2 <- subplot(lag_plots, 
        titleX = FALSE, titleY = TRUE,
        shareX = FALSE, shareY = FALSE, 
        margin = 0.05,
        nrows = ceiling(length(lag_plots) / 3))%>% 
  hide_legend()

Vehicle Sales Dataset - Seasonality Analysis

Can you identify which lag has the highest correlation?

Vehicle Sales Dataset - Forecast

Plotting the forecast object:

Vehicle Sales Dataset - Forecast

Split the data into training and testing

# Set the forecasting horizon
h <- 12 
# Split to training and testing
train <- window(sales.ts,start = time(sales.ts)[1], 
                end = time(sales.ts)[length(time(sales.ts)) - h])
test  <- window(sales.ts,start = time(sales.ts)[length(time(sales.ts)) - h + 1], 
                end = time(sales.ts)[length(time(sales.ts))])

Set ARIMA model

fc_arima <- forecast(auto.arima(train, test = "kpss", lambda = 0.01), h = h)

Evaluate the model performance

accuracy(fc_arima$mean, test)
##                 ME     RMSE      MAE      MPE     MAPE        ACF1
## Test set -57.14542 73.01442 64.18531 -4.07079 4.484585 -0.09016593
##          Theil's U
## Test set 0.3403804

Vehicle Sales Dataset - Forecast

attributes(fc_arima)
## $names
##  [1] "method"    "model"     "level"     "mean"      "lower"    
##  [6] "upper"     "x"         "series"    "fitted"    "residuals"
## 
## $class
## [1] "forecast"
fc_arima$fitted
##            Jan       Feb       Mar       Apr       May       Jun       Jul
## 2010  707.9844  762.5600  929.6726  933.1246 1011.0814  976.0613 1015.2704
## 2011  809.3701  916.0455 1243.8609 1164.0966 1283.6648 1120.5892 1172.5497
## 2012  941.7641 1108.3731 1382.3587 1292.7437 1185.9707 1210.5116 1244.8426
## 2013 1070.0174 1304.4099 1532.6698 1305.0498 1463.3377 1409.2534 1280.1891
## 2014 1158.0538 1275.6566 1501.2458 1362.9241 1517.6689 1498.2466 1405.6951
## 2015 1149.3428 1327.3974 1638.7879 1484.1523 1700.4510 1517.8283 1538.4475
## 2016 1286.9860 1367.8525 1642.2536 1544.6261 1712.9681 1546.0057 1571.7896
##            Aug       Sep       Oct       Nov       Dec
## 2010  997.8500  980.8246  974.7105  930.7550 1063.4490
## 2011 1102.1543 1064.5933 1058.3719  981.5960 1256.7312
## 2012 1238.2894 1232.7363 1199.5195 1163.6191 1400.8906
## 2013 1419.8720 1343.9127 1220.1126 1271.8710 1466.5594
## 2014 1597.1404 1247.8401 1331.4890 1361.5798 1458.9034
## 2015 1662.2671 1341.8956 1405.5665 1432.5994 1618.0502
## 2016
fc_arima$mean
##           Jan      Feb      Mar      Apr      May      Jun      Jul
## 2016                                                               
## 2017 1242.231 1411.636 1626.736 1546.477 1572.040 1568.647 1566.771
##           Aug      Sep      Oct      Nov      Dec
## 2016 1612.685 1502.369 1512.694 1397.589 1677.472
## 2017

Vehicle Sales Dataset - Forecast

p <- plot_ly() %>% 
  add_trace(x = time(sales.ts), 
            y = sales.ts, 
            mode = 'lines+markers', name = "Actual") %>%  
  add_trace(x = time(sales.ts), 
            y = c(fc_arima$fitted, rep(NA,length(sales.ts) - length(train))), 
          mode = 'lines+markers',name = "Fitted", line = list(color = "red")) %>%
  add_trace(x = time(sales.ts), 
            y = c(rep(NA,length(sales.ts) - length(test)), fc_arima$mean), 
            mode = 'lines+markers',name = "Forecast") %>% 
  layout(
    title = "Forecast and Fitted vs Actual", 
    xaxis = list(title = ""), 
    yaxis = list(title = "Millions of Units")
    )

Vehicle Sales Dataset - Forecast

Vehicle Sales Dataset - Forecast

Forecasting and ploting the reasults:

# Forecasting the next 12 months
fc_arima <- forecast(auto.arima(sales.ts, test = "kpss", lambda = 0.01), h = h)

p <- plot_ly() %>%
  add_lines(x = time(sales.ts), y = sales.ts,
            color = I("blue"), name = "Actual") %>%
  add_ribbons(x = time(fc_arima$mean), ymin = fc_arima$lower[, 2], ymax = fc_arima$upper[, 2],
              color = I("gray95"), name = "95% confidence") %>%
  add_ribbons(x = time(fc_arima$mean), ymin = fc_arima$lower[, 1], ymax = fc_arima$upper[, 1],
              color = I("gray80"), name = "80% confidence") %>%
  add_lines(x = time(fc_arima$mean), y = fc_arima$mean, color = I("orange"), name = "Forecast") %>%
  layout(
    title = "US Vehicle Sales - 12 Months Forecast", 
    xaxis = list(title = ""), 
    yaxis = list(title = "Millions of Units")
  )

Vehicle Sales Dataset - Forecast

Vehicle Sales Dataset - Forecast

What if we want to present multiple forecast outputs in the same plot frame?

Vehicle Sales Dataset - Forecast

mmm… that’s too messy

Vehicle Sales Dataset - Buttons

The use of the button function could address this issue:

p <- plot_ly() %>%
  add_lines(x = time(sales.ts), y = sales.ts,
            color = I("blue"), name = "Actual", visible = TRUE) %>%
  add_ribbons(x = time(fc_arima$mean), ymin = fc_arima$lower[, 2], ymax = fc_arima$upper[, 2],
              color = I("gray95"), name = "95% Confidence", visible = TRUE) %>%
  add_ribbons(x = time(fc_arima$mean), ymin = fc_arima$lower[, 1], ymax = fc_arima$upper[, 1],
              color = I("gray80"), name = "80% Confidence", visible = TRUE) %>%
  add_lines(x = time(fc_arima$mean), y = fc_arima$mean, color = I("orange"), name = "ARIMA", visible = TRUE) %>%
  add_ribbons(x = time(fc_ets$mean), ymin = fc_ets$lower[, 2], ymax = fc_ets$upper[, 2],
              color = I("gray95"), name = "95% Confidence", visible = FALSE) %>%
  add_ribbons(x = time(fc_ets$mean), ymin = fc_ets$lower[, 1], ymax = fc_ets$upper[, 1],
              color = I("gray80"), name = "80% Confidence", visible = FALSE) %>%
  add_lines(x = time(fc_ets$mean), y = fc_ets$mean, color = I("green"), name = "ETS", visible = FALSE) %>%
  
  add_ribbons(x = time(fc_hw$mean), ymin = fc_hw$lower[, 2], ymax = fc_hw$upper[, 2],
              color = I("gray95"), name = "95% Confidence", visible = FALSE) %>%
  add_ribbons(x = time(fc_hw$mean), ymin = fc_hw$lower[, 1], ymax = fc_hw$upper[, 1],
              color = I("gray80"), name = "80% Confidence", visible = FALSE) %>%
  add_lines(x = time(fc_hw$mean), y = fc_hw$mean, color = I("red"), name = "HW", visible = FALSE) %>%
  
  layout(
    title = "US Vehicle Sales - 12 Months Forecast", 
    xaxis = list(title = ""), 
    yaxis = list(title = "Millions of Units"),
    updatemenus = list(
      list(
        y = 0.95, x= - 0.1,
        buttons = list(
          # ARIMA Button
          list(method = "restyle",
               args = list("visible", list(TRUE, # Actual line 
                                           TRUE, TRUE, TRUE, # ARIMA model
                                           FALSE, FALSE, FALSE, # ETS
                                           FALSE, FALSE, FALSE  # HW
                                           
               )),
               label = "ARIMA"),
          # ETS Button
          list(method = "restyle",
               args = list("visible", list(TRUE, # Actual line 
                                           FALSE, FALSE, FALSE, # ARIMA model
                                           TRUE, TRUE, TRUE, # ETS
                                           FALSE, FALSE, FALSE  # HW
                                           
               )),
               label = "ETS"),
          # HW Button
          list(method = "restyle",
               args = list("visible", list(TRUE, # Actual line 
                                           FALSE, FALSE, FALSE, # ARIMA model
                                           FALSE, FALSE, FALSE, # ETS
                                           TRUE, TRUE, TRUE  # HW
                                           
               )),
               label = "HW")
          
        )
      )
    )
  )

Vehicle Sales Dataset - Buttons

The use of the button function could address this issue:

Plotly and Shiny

My shiny based package with data visualization application utilizing plotly (draft version)

# source("https://raw.githubusercontent.com/RamiKrispin/Shiny-App/master/Shiny%20Modeling%20Git.R")
#runApp(list(ui = ui, server = server), launch.browser = TRUE)

Additional Resources

Questions?

Thank you!