forked from jjallaire/dataviz-r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotly_shiny_tutorial_v2.html
1179 lines (1076 loc) · 164 KB
/
plotly_shiny_tutorial_v2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="progressive" content="false" />
<meta name="allow-skip" content="false" />
<title>Plotly and Shiny Tutorial</title>
<!-- highlightjs -->
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs && document.readyState && document.readyState === "complete") {
window.setTimeout(function() {
hljs.initHighlighting();
}, 0);
}
</script>
</head>
<body>
<div class="pageContent band">
<div class="bandContent page">
<div class="topics">
<p><img src="images/opensdp_logo_white.png" width="400px" /></p>
<div id="section-introduction" class="section level2">
<h2>Introduction</h2>
<p>This tutorial is an introduction to making interactive graphics, and even entire websites, using R. The R packages that make this possible are “Plotly” and “Shiny.”</p>
<p>Plotly is the R package used to create the graphs and charts in your visualization project. Plotly has an elegant default graphical style that includes all major graph types and hover-over tooltips. Take a look at the <a href="https://plot.ly/r/">plotly reference</a> and have this <a href="https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf">plotly cheat sheet</a> open as you go through the exercises in this tutorial.</p>
<p>Plotly alone can’t give you a fully interactive user experience. The Shiny package takes interactivity to the next level. It allows us to include additional complex user controls such as sidebars and buttons. Shiny also makes it easy to create a full website for your data dashboard if you desire. You can see examples of Shiny projects in the <a href="https://www.rstudio.com/products/shiny/shiny-user-showcase/">Shiny showcase</a>, though these examples only scratch the surface of what <a href="http://shiny.rstudio.com/tutorial/">Shiny can do</a>. <a href="https://cpsievert.github.io/plotly_book/index.html">This free online book</a> is also a great reference for Plotly.</p>
<div class="panel panel-default">
<div class="panel-heading tutorial-panel-heading">Quiz</div>
<table class="table quiz-table">
<tr>
<td>
<div id="htmlwidget-291c6d8f37e567037778" style="width:100%;height:auto;", class = "quiz html-widget">
<div class="panel panel-default">
<div class="panel-body quizArea">
</div>
</div>
</div>
<script type="application/json" data-for="htmlwidget-291c6d8f37e567037778">{"x":{"question":"Check your understanding. What are the functions of the Shiny and Plotly packages?","answers":[{"option":"Shiny creates chart graphics. Plotly adds additional user controls and website features.","correct":false,"message":null},{"option":"Plotly aggregates data for creating graphics. Shiny creates the interactive visualizations.","correct":false,"message":null},{"option":"Plotly creates chart graphics. Shiny adds additional user controls and website features.","correct":true,"message":null},{"option":"I don't know. Use Tableau instead.","correct":false,"message":null}],"label":"shinyquestion-1","skipStartButton":true,"perQuestionResponseAnswers":true,"perQuestionResponseMessaging":true,"preventUnanswered":true,"displayQuestionCount":false,"displayQuestionNumber":false,"disableRanking":true,"nextQuestionText":"","checkAnswerText":"Submit Answer","allowRetry":false,"randomSortAnswers":false,"json":{"info":{"name":"","main":""},"questions":[{"q":"Check your understanding. What are the functions of the Shiny and Plotly packages?","a":[{"option":"Shiny creates chart graphics. Plotly adds additional user controls and website features.","correct":false,"message":null},{"option":"Plotly aggregates data for creating graphics. Shiny creates the interactive visualizations.","correct":false,"message":null},{"option":"Plotly creates chart graphics. Shiny adds additional user controls and website features.","correct":true,"message":null},{"option":"I don't know. Use Tableau instead.","correct":false,"message":null}],"correct":"Correct!","incorrect":"Incorrect."}]}},"evals":[],"jsHooks":[]}</script>
</td>
</tr>
</table>
</div>
<div id="section-objectives" class="section level3">
<h3>Objectives</h3>
<p>At the end of this tutorial you will be able to:</p>
<ol style="list-style-type: decimal">
<li><p>Create interactive graphs using plotly</p></li>
<li><p>Add user controls to your plotly graph using shiny</p></li>
<li><p>Use shiny to make a full website in R</p></li>
</ol>
</div>
<div id="section-instructors-note" class="section level3">
<h3>Instructor’s Note</h3>
<p>This tutorial contains boxes where you can try out code for yourself. I highly encourage you to give the code a try. To run your code, hit the green “Run Code” button. If your code does not contain errors, output will appear. If the code did not run, you will see an error.</p>
<p>You can click the “Solution” button to see the intended answer at any time. To run the tutorial code:</p>
<ul>
<li><p>Click the solution button</p></li>
<li><p>Click “Copy to Clipboard”</p></li>
<li><p>Paste the copied code into the exercise box (<code>Ctrl V</code> keyboard shortcut or right click and click “Paste”)</p></li>
<li><p>Click “Run Code”</p></li>
</ul>
</div>
<div id="section-our-data" class="section level3">
<h3>Our Data</h3>
<p>This tutorial uses data from the <a href="www.sdp.cepr.harvard.edu/toolkit">College-Going toolkit</a>, which is unique by <code>student_id + year</code>. For simplicity, the tutorial automatically translates the toolkit data into stored school- or agency-level datasets in R. Use the space below to take a look at our data. Use the function <code>head(datasetname, n=5)</code> on <code>schoolData</code> and <code>agencyData</code> to view the first 5 lines of our datasets.</p>
<div class="tutorial-exercise" data-label="viewdata" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="0">
<script type="application/json" data-opts-chunk="1">{"fig.width":6.5,"fig.height":4,"fig.retina":2,"fig.align":"default","fig.keep":"high","fig.show":"asis","out.width":624,"warning":false,"error":false,"message":true,"exercise.df_print":"paged","exercise.checker":"NULL"}</script>
</div>
<div class="tutorial-exercise-support" data-label="viewdata-solution" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="0">
<pre class="text"><code>head(schoolData, n=5)
head(agencyData, n=5)
# Remember you access columns by putting the dataset name and the dollar sign,
# e.g. schoolData$last_hs_name</code></pre>
</div>
</div>
</div>
<div id="section-plotly-anatomy" class="section level2">
<h2>Plotly Anatomy</h2>
<p>Plotly is the R package that we will use to render our graphs. The syntax is fairly simple. Here is a basic scatter plot. You can see the point information when you hover over the markers.</p>
<div class="tutorial-exercise" data-label="basicscatter" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="0">
<pre class="text"><code>plot_1 <- plot_ly(data = schoolData, x = ~avg_test_math_8_std, y = ~pct_4yr, type="scatter", mode = "markers")
plot_1</code></pre>
<div id="106b9238090ea" style="width:624px;height:384px;" class="plotly html-widget"></div>
<script type="application/json" data-for="106b9238090ea">{"x":{"visdat":{"106b9751c857f":["function () ","plotlyVisDat"]},"cur_data":"106b9751c857f","attrs":{"106b9751c857f":{"x":{},"y":{},"mode":"markers","alpha":1,"sizes":[10,100],"type":"scatter"}},"layout":{"margin":{"b":40,"l":60,"t":25,"r":10},"xaxis":{"domain":[0,1],"title":"avg_test_math_8_std"},"yaxis":{"domain":[0,1],"title":"pct_4yr"},"hovermode":"closest","showlegend":false},"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":[-0.0572368162790698,-0.17006474045977,0.0620150684410646,0.168237465365854,0.320300586206897,0.122428973929961,0.21138548814433,0.492871761734694,0.46508424925816,0.713709099337748,0.951112215879017],"y":[27.1317829457364,28.0459770114943,41.4448669201521,41.4634146341463,44.1379310344828,44.3579766536965,51.0309278350515,52.8061224489796,53.4124629080119,65.121412803532,73.1568998109641],"mode":"markers","type":"scatter","marker":{"fillcolor":"rgba(31,119,180,1)","color":"rgba(31,119,180,1)","line":{"color":"transparent"}},"xaxis":"x","yaxis":"y","frame":null}],"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.2,"selected":{"opacity":1}},"base_url":"https://plot.ly"},"evals":["config.modeBarButtonsToAdd.0.click"],"jsHooks":{"render":[{"code":"function(el, x) { var ctConfig = crosstalk.var('plotlyCrosstalkOpts').set({\"on\":\"plotly_click\",\"persistent\":false,\"dynamic\":false,\"selectize\":false,\"opacityDim\":0.2,\"selected\":{\"opacity\":1}}); }","data":null}]}}</script>
<script type="application/json" data-opts-chunk="1">{"fig.width":6.5,"fig.height":4,"fig.retina":2,"fig.align":"default","fig.keep":"high","fig.show":"asis","out.width":624,"warning":false,"error":false,"message":true,"exercise.df_print":"paged","exercise.checker":"NULL"}</script>
</div>
<p>We’ve defined the arguments <code>data</code>, <code>x</code>, <code>y</code>, <code>type</code>, and <code>mode</code>. Note: You must repeat the name of the plot <code>plot_1</code> in order to make it show up.</p>
<div id="section-changing-markers-and-tooltips" class="section level3">
<h3>Changing markers and tooltips</h3>
<p>Now we can redo the same basic plot, but change the marker styling and add more tooltip information. We’ve added definitions for <code>marker</code>, <code>hoverinfo</code>, and <code>text</code>.</p>
<div class="tutorial-exercise" data-label="prettyscatter" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="0">
<pre class="text"><code>plot_2 <- plot_ly(data = schoolData, x = ~avg_test_math_8_std, y = ~pct_4yr, mode = "markers", type="scatter",
marker = list(size=10,
color = "steelblue",
line = list(
color = "navy",
width = 2
)),
hoverinfo = "text",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
"Average 8th Grade Math Score: ", format(schoolData$avg_test_math_8_std, digits=2), "%", "<br>",
"Seamless 4-yr Enrollment: ", format(schoolData$pct_4yr, digits=2), "%",
sep=""
)
)
plot_2</code></pre>
<div id="106b93d1a8138" style="width:624px;height:384px;" class="plotly html-widget"></div>
<script type="application/json" data-for="106b93d1a8138">{"x":{"visdat":{"106b96694f3ed":["function () ","plotlyVisDat"]},"cur_data":"106b96694f3ed","attrs":{"106b96694f3ed":{"x":{},"y":{},"mode":"markers","marker":{"size":10,"color":"steelblue","line":{"color":"navy","width":2}},"hoverinfo":"text","text":{},"alpha":1,"sizes":[10,100],"type":"scatter"}},"layout":{"margin":{"b":40,"l":60,"t":25,"r":10},"xaxis":{"domain":[0,1],"title":"avg_test_math_8_std"},"yaxis":{"domain":[0,1],"title":"pct_4yr"},"hovermode":"closest","showlegend":false},"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":[-0.0572368162790698,-0.17006474045977,0.0620150684410646,0.168237465365854,0.320300586206897,0.122428973929961,0.21138548814433,0.492871761734694,0.46508424925816,0.713709099337748,0.951112215879017],"y":[27.1317829457364,28.0459770114943,41.4448669201521,41.4634146341463,44.1379310344828,44.3579766536965,51.0309278350515,52.8061224489796,53.4124629080119,65.121412803532,73.1568998109641],"mode":"markers","marker":{"fillcolor":"rgba(31,119,180,1)","color":"steelblue","size":10,"line":{"color":"navy","width":2}},"hoverinfo":"text","text":["School: River <br>HS Graduates (N): 258<br>Average 8th Grade Math Score: -0.057%<br>Seamless 4-yr Enrollment: 27%","School: West Spring Hill <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: -0.170%<br>Seamless 4-yr Enrollment: 28%","School: Upland <br>HS Graduates (N): 263<br>Average 8th Grade Math Score: 0.062%<br>Seamless 4-yr Enrollment: 41%","School: North <br>HS Graduates (N): 205<br>Average 8th Grade Math Score: 0.168%<br>Seamless 4-yr Enrollment: 41%","School: South <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: 0.320%<br>Seamless 4-yr Enrollment: 44%","School: Peak <br>HS Graduates (N): 257<br>Average 8th Grade Math Score: 0.122%<br>Seamless 4-yr Enrollment: 44%","School: City <br>HS Graduates (N): 194<br>Average 8th Grade Math Score: 0.211%<br>Seamless 4-yr Enrollment: 51%","School: White Sands <br>HS Graduates (N): 392<br>Average 8th Grade Math Score: 0.493%<br>Seamless 4-yr Enrollment: 53%","School: Central <br>HS Graduates (N): 337<br>Average 8th Grade Math Score: 0.465%<br>Seamless 4-yr Enrollment: 53%","School: Camino <br>HS Graduates (N): 453<br>Average 8th Grade Math Score: 0.714%<br>Seamless 4-yr Enrollment: 65%","School: Orchard <br>HS Graduates (N): 529<br>Average 8th Grade Math Score: 0.951%<br>Seamless 4-yr Enrollment: 73%"],"type":"scatter","xaxis":"x","yaxis":"y","frame":null}],"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.2,"selected":{"opacity":1}},"base_url":"https://plot.ly"},"evals":["config.modeBarButtonsToAdd.0.click"],"jsHooks":{"render":[{"code":"function(el, x) { var ctConfig = crosstalk.var('plotlyCrosstalkOpts').set({\"on\":\"plotly_click\",\"persistent\":false,\"dynamic\":false,\"selectize\":false,\"opacityDim\":0.2,\"selected\":{\"opacity\":1}}); }","data":null}]}}</script>
<script type="application/json" data-opts-chunk="1">{"fig.width":6.5,"fig.height":4,"fig.retina":2,"fig.align":"default","fig.keep":"high","fig.show":"asis","out.width":624,"warning":false,"error":false,"message":false,"exercise.df_print":"paged","exercise.checker":"NULL"}</script>
</div>
</div>
<div id="section-adding-traces-best-fit-line" class="section level3">
<h3>Adding Traces (Best Fit Line)</h3>
<p>What if we want to add a best fit line to our scatter plot? This is the equivalent of adding another <code>plot_ly</code> graph to the same plot. The syntax is the same, but instead of <code>plot_ly()</code> we use <code>add_trace()</code> after our previous plot.</p>
<div class="tutorial-exercise" data-label="bestfit" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="25">
<pre class="text"><code># Necessary to repeat code for plot_2 because of how the html document works (see below)
plot_2 <- plot_ly(data = schoolData, x = ~avg_test_math_8_std, y = ~pct_4yr, mode = "markers", type="scatter",
marker = list(size=10,
color = "steelblue",
line = list(
color = "navy",
width = 2
)),
hoverinfo = "text",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
"Average 8th Grade Math Score: ", format(schoolData$avg_test_math_8_std, digits=2), "%", "<br>",
"Seamless 4-yr Enrollment: ", format(schoolData$pct_4yr, digits=2), "%",
sep=""
)
)
# Add best fit line
fit <- lm(pct_4yr ~ avg_test_math_8_std, data = schoolData)
plot_3 <- plot_2 %>%
add_trace(x = schoolData$avg_test_math_8_std, y = fitted(fit), mode = "lines+markers", line = list(color = "black"))
plot_3</code></pre>
<div id="106b936fb5b3" style="width:624px;height:384px;" class="plotly html-widget"></div>
<script type="application/json" data-for="106b936fb5b3">{"x":{"visdat":{"106b9361a76d5":["function () ","plotlyVisDat"]},"cur_data":"106b9361a76d5","attrs":{"106b9361a76d5":{"x":{},"y":{},"mode":"markers","marker":{"size":10,"color":"steelblue","line":{"color":"navy","width":2}},"hoverinfo":"text","text":{},"alpha":1,"sizes":[10,100],"type":"scatter"},"106b9361a76d5.1":{"x":[-0.0572368162790698,-0.17006474045977,0.0620150684410646,0.168237465365854,0.320300586206897,0.122428973929961,0.21138548814433,0.492871761734694,0.46508424925816,0.713709099337748,0.951112215879017],"y":[33.263269700949,28.7548894314638,38.028338703815,42.2727751937231,48.3489161729556,40.4423586504416,43.9968846814615,55.244518378535,54.1341844439441,64.0687407760038,73.554898872955],"mode":"lines+markers","marker":{"size":10,"color":"steelblue","line":{"color":"navy","width":2}},"hoverinfo":"text","text":{},"alpha":1,"sizes":[10,100],"type":"scatter","line":{"color":"black"}}},"layout":{"margin":{"b":40,"l":60,"t":25,"r":10},"xaxis":{"domain":[0,1],"title":"avg_test_math_8_std"},"yaxis":{"domain":[0,1],"title":"pct_4yr"},"hovermode":"closest","showlegend":true},"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":[-0.0572368162790698,-0.17006474045977,0.0620150684410646,0.168237465365854,0.320300586206897,0.122428973929961,0.21138548814433,0.492871761734694,0.46508424925816,0.713709099337748,0.951112215879017],"y":[27.1317829457364,28.0459770114943,41.4448669201521,41.4634146341463,44.1379310344828,44.3579766536965,51.0309278350515,52.8061224489796,53.4124629080119,65.121412803532,73.1568998109641],"mode":"markers","marker":{"fillcolor":"rgba(31,119,180,1)","color":"steelblue","size":10,"line":{"color":"navy","width":2}},"hoverinfo":"text","text":["School: River <br>HS Graduates (N): 258<br>Average 8th Grade Math Score: -0.057%<br>Seamless 4-yr Enrollment: 27%","School: West Spring Hill <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: -0.170%<br>Seamless 4-yr Enrollment: 28%","School: Upland <br>HS Graduates (N): 263<br>Average 8th Grade Math Score: 0.062%<br>Seamless 4-yr Enrollment: 41%","School: North <br>HS Graduates (N): 205<br>Average 8th Grade Math Score: 0.168%<br>Seamless 4-yr Enrollment: 41%","School: South <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: 0.320%<br>Seamless 4-yr Enrollment: 44%","School: Peak <br>HS Graduates (N): 257<br>Average 8th Grade Math Score: 0.122%<br>Seamless 4-yr Enrollment: 44%","School: City <br>HS Graduates (N): 194<br>Average 8th Grade Math Score: 0.211%<br>Seamless 4-yr Enrollment: 51%","School: White Sands <br>HS Graduates (N): 392<br>Average 8th Grade Math Score: 0.493%<br>Seamless 4-yr Enrollment: 53%","School: Central <br>HS Graduates (N): 337<br>Average 8th Grade Math Score: 0.465%<br>Seamless 4-yr Enrollment: 53%","School: Camino <br>HS Graduates (N): 453<br>Average 8th Grade Math Score: 0.714%<br>Seamless 4-yr Enrollment: 65%","School: Orchard <br>HS Graduates (N): 529<br>Average 8th Grade Math Score: 0.951%<br>Seamless 4-yr Enrollment: 73%"],"type":"scatter","xaxis":"x","yaxis":"y","frame":null},{"x":[-0.0572368162790698,-0.17006474045977,0.0620150684410646,0.168237465365854,0.320300586206897,0.122428973929961,0.21138548814433,0.492871761734694,0.46508424925816,0.713709099337748,0.951112215879017],"y":[33.263269700949,28.7548894314638,38.028338703815,42.2727751937231,48.3489161729556,40.4423586504416,43.9968846814615,55.244518378535,54.1341844439441,64.0687407760038,73.554898872955],"mode":"lines+markers","marker":{"size":10,"color":"steelblue","line":{"color":"navy","width":2}},"hoverinfo":"text","text":["School: River <br>HS Graduates (N): 258<br>Average 8th Grade Math Score: -0.057%<br>Seamless 4-yr Enrollment: 27%","School: West Spring Hill <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: -0.170%<br>Seamless 4-yr Enrollment: 28%","School: Upland <br>HS Graduates (N): 263<br>Average 8th Grade Math Score: 0.062%<br>Seamless 4-yr Enrollment: 41%","School: North <br>HS Graduates (N): 205<br>Average 8th Grade Math Score: 0.168%<br>Seamless 4-yr Enrollment: 41%","School: South <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: 0.320%<br>Seamless 4-yr Enrollment: 44%","School: Peak <br>HS Graduates (N): 257<br>Average 8th Grade Math Score: 0.122%<br>Seamless 4-yr Enrollment: 44%","School: City <br>HS Graduates (N): 194<br>Average 8th Grade Math Score: 0.211%<br>Seamless 4-yr Enrollment: 51%","School: White Sands <br>HS Graduates (N): 392<br>Average 8th Grade Math Score: 0.493%<br>Seamless 4-yr Enrollment: 53%","School: Central <br>HS Graduates (N): 337<br>Average 8th Grade Math Score: 0.465%<br>Seamless 4-yr Enrollment: 53%","School: Camino <br>HS Graduates (N): 453<br>Average 8th Grade Math Score: 0.714%<br>Seamless 4-yr Enrollment: 65%","School: Orchard <br>HS Graduates (N): 529<br>Average 8th Grade Math Score: 0.951%<br>Seamless 4-yr Enrollment: 73%"],"type":"scatter","line":{"fillcolor":"rgba(255,127,14,1)","color":"black"},"xaxis":"x","yaxis":"y","frame":null}],"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.2,"selected":{"opacity":1}},"base_url":"https://plot.ly"},"evals":["config.modeBarButtonsToAdd.0.click"],"jsHooks":{"render":[{"code":"function(el, x) { var ctConfig = crosstalk.var('plotlyCrosstalkOpts').set({\"on\":\"plotly_click\",\"persistent\":false,\"dynamic\":false,\"selectize\":false,\"opacityDim\":0.2,\"selected\":{\"opacity\":1}}); }","data":null}]}}</script>
<script type="application/json" data-opts-chunk="1">{"fig.width":6.5,"fig.height":4,"fig.retina":2,"fig.align":"default","fig.keep":"high","fig.show":"asis","out.width":624,"warning":false,"error":false,"message":false,"exercise.df_print":"paged","exercise.checker":"NULL"}</script>
</div>
<p>Note: In a regular R file we do not have to repeat the code for <code>plot_2</code> before the <code>add_trace</code>. This is a limitation of the web-browser-based environment we are working in. Keep in mind as you work through the exercises that you will have to copy code from earlier exercises if you want to call plot objects from those exercises.</p>
</div>
<div id="section-layout-titles-and-more" class="section level3">
<h3>Layout: Titles and More</h3>
<p>Other features we may want to add are simple lines for the agency average and add titles. These all go in <code>layout()</code>. The options we will use in <code>layout()</code> are:</p>
<ul>
<li><p><code>title</code></p></li>
<li><p><code>xaxis</code> (<code>title</code>, <code>range</code>, <code>zeroline</code>, <code>showline</code>)</p></li>
<li><p><code>yaxis</code> (<code>title</code>, <code>range</code>)</p></li>
<li><p><code>margin</code></p></li>
<li><p><code>showlegend</code></p></li>
<li><p><code>shapes</code></p></li>
<li><p><code>annotations</code></p></li>
</ul>
<p>But there are many more things you can add!</p>
<div class="tutorial-exercise" data-label="layout-scatter" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="30">
<pre class="text"><code># Necessary to repeat code for plot_2 because of how the html document works (see below)
plot_2 <- plot_ly(data = schoolData, x = ~avg_test_math_8_std, y = ~pct_4yr, mode = "markers", type="scatter",
marker = list(size=10,
color = "steelblue",
line = list(
color = "navy",
width = 2
)),
hoverinfo = "text",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
"Average 8th Grade Math Score: ", format(schoolData$avg_test_math_8_std, digits=2), "%", "<br>",
"Seamless 4-yr Enrollment: ", format(schoolData$pct_4yr, digits=2), "%",
sep=""
)
)
# Add best fit line
fit <- lm(pct_4yr ~ avg_test_math_8_std, data = schoolData)
plot_3 <- plot_2 %>%
add_trace(x = schoolData$avg_test_math_8_std, y = fitted(fit), mode = "lines", line = list(color = "black"))
# Create agency average lines using shapes - these lines will be added in next step
# Average 8th grade score
avg_8th_line <- list(
type = 'line',
line = list(color = "orange", dash = 'dash'),
x0 = agencyData$avg_test_math_8_std,
x1 = agencyData$avg_test_math_8_std,
y0 = 0,
y1 = 100
)
# Average enrollment
avg_4yr_line <- list(
type = 'line',
line = list(color = "orange", dash = 'dash'),
x0 = -.5,
x1 = 1,
y0 = agencyData$pct_4yr,
y1 = agencyData$pct_4yr
)
# Average 8th grade line label
avg_8th_label <- list(
text="Avg 8th Grade Score",
x=agencyData$avg_test_math_8_std,
y=100,
showarrow = FALSE,
xanchor = 'left',
yanchor = 'top'
)
# Average enrollment line label
avg_4yr_label <- list(
text="Average 4-year Enrollment ",
x=-.5,
y=agencyData$pct_4yr,
showarrow = FALSE,
xanchor = 'left',
yanchor = 'bottom'
)
# Add layout - start with plot_3
plot_3_with_opts <- layout(plot_3,
title = "Relationship between Average Four-Year College Graduation <br> and Average 8th Grade Achivement for High Schools",
xaxis = list(
title = "Average 8th Grade Math Test Score (Std. Dev. Units)",
range = c(-.5, 1),
zeroline = TRUE,
showline = TRUE
),
yaxis = list(
title = "High School Graduates (%)",
range = c(0,100)
),
margin = list(b = 100, t = 100),
showlegend = FALSE,
shapes = list(avg_8th_line, avg_4yr_line),
annotations = list(avg_8th_label, avg_4yr_label)
)
plot_3_with_opts</code></pre>
<div id="106b926f300b3" style="width:624px;height:384px;" class="plotly html-widget"></div>
<script type="application/json" data-for="106b926f300b3">{"x":{"visdat":{"106b96eb4e81d":["function () ","plotlyVisDat"]},"cur_data":"106b96eb4e81d","attrs":{"106b96eb4e81d":{"x":{},"y":{},"mode":"markers","marker":{"size":10,"color":"steelblue","line":{"color":"navy","width":2}},"hoverinfo":"text","text":{},"alpha":1,"sizes":[10,100],"type":"scatter"},"106b96eb4e81d.1":{"x":[-0.0572368162790698,-0.17006474045977,0.0620150684410646,0.168237465365854,0.320300586206897,0.122428973929961,0.21138548814433,0.492871761734694,0.46508424925816,0.713709099337748,0.951112215879017],"y":[33.263269700949,28.7548894314638,38.028338703815,42.2727751937231,48.3489161729556,40.4423586504416,43.9968846814615,55.244518378535,54.1341844439441,64.0687407760038,73.554898872955],"mode":"lines","marker":{"size":10,"color":"steelblue","line":{"color":"navy","width":2}},"hoverinfo":"text","text":{},"alpha":1,"sizes":[10,100],"type":"scatter","line":{"color":"black"}}},"layout":{"margin":{"b":100,"l":60,"t":100,"r":10},"title":"Relationship between Average Four-Year College Graduation <br> and Average 8th Grade Achivement for High Schools","xaxis":{"domain":[0,1],"title":"Average 8th Grade Math Test Score (Std. Dev. Units)","range":[-0.5,1],"zeroline":true,"showline":true},"yaxis":{"domain":[0,1],"title":"High School Graduates (%)","range":[0,100]},"showlegend":false,"shapes":[{"type":"line","line":{"color":"orange","dash":"dash"},"x0":0.359298796035125,"x1":0.359298796035125,"y0":0,"y1":100},{"type":"line","line":{"color":"orange","dash":"dash"},"x0":-0.5,"x1":1,"y0":49.4944119212347,"y1":49.4944119212347}],"annotations":[{"text":"Avg 8th Grade Score","x":0.359298796035125,"y":100,"showarrow":false,"xanchor":"left","yanchor":"top"},{"text":"Average 4-year Enrollment ","x":-0.5,"y":49.4944119212347,"showarrow":false,"xanchor":"left","yanchor":"bottom"}],"hovermode":"closest"},"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":[-0.0572368162790698,-0.17006474045977,0.0620150684410646,0.168237465365854,0.320300586206897,0.122428973929961,0.21138548814433,0.492871761734694,0.46508424925816,0.713709099337748,0.951112215879017],"y":[27.1317829457364,28.0459770114943,41.4448669201521,41.4634146341463,44.1379310344828,44.3579766536965,51.0309278350515,52.8061224489796,53.4124629080119,65.121412803532,73.1568998109641],"mode":"markers","marker":{"fillcolor":"rgba(31,119,180,1)","color":"steelblue","size":10,"line":{"color":"navy","width":2}},"hoverinfo":"text","text":["School: River <br>HS Graduates (N): 258<br>Average 8th Grade Math Score: -0.057%<br>Seamless 4-yr Enrollment: 27%","School: West Spring Hill <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: -0.170%<br>Seamless 4-yr Enrollment: 28%","School: Upland <br>HS Graduates (N): 263<br>Average 8th Grade Math Score: 0.062%<br>Seamless 4-yr Enrollment: 41%","School: North <br>HS Graduates (N): 205<br>Average 8th Grade Math Score: 0.168%<br>Seamless 4-yr Enrollment: 41%","School: South <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: 0.320%<br>Seamless 4-yr Enrollment: 44%","School: Peak <br>HS Graduates (N): 257<br>Average 8th Grade Math Score: 0.122%<br>Seamless 4-yr Enrollment: 44%","School: City <br>HS Graduates (N): 194<br>Average 8th Grade Math Score: 0.211%<br>Seamless 4-yr Enrollment: 51%","School: White Sands <br>HS Graduates (N): 392<br>Average 8th Grade Math Score: 0.493%<br>Seamless 4-yr Enrollment: 53%","School: Central <br>HS Graduates (N): 337<br>Average 8th Grade Math Score: 0.465%<br>Seamless 4-yr Enrollment: 53%","School: Camino <br>HS Graduates (N): 453<br>Average 8th Grade Math Score: 0.714%<br>Seamless 4-yr Enrollment: 65%","School: Orchard <br>HS Graduates (N): 529<br>Average 8th Grade Math Score: 0.951%<br>Seamless 4-yr Enrollment: 73%"],"type":"scatter","xaxis":"x","yaxis":"y","frame":null},{"x":[-0.0572368162790698,-0.17006474045977,0.0620150684410646,0.168237465365854,0.320300586206897,0.122428973929961,0.21138548814433,0.492871761734694,0.46508424925816,0.713709099337748,0.951112215879017],"y":[33.263269700949,28.7548894314638,38.028338703815,42.2727751937231,48.3489161729556,40.4423586504416,43.9968846814615,55.244518378535,54.1341844439441,64.0687407760038,73.554898872955],"mode":"lines+markers","marker":{"size":10,"color":"steelblue","line":{"color":"navy","width":2}},"hoverinfo":"text","text":["School: River <br>HS Graduates (N): 258<br>Average 8th Grade Math Score: -0.057%<br>Seamless 4-yr Enrollment: 27%","School: West Spring Hill <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: -0.170%<br>Seamless 4-yr Enrollment: 28%","School: Upland <br>HS Graduates (N): 263<br>Average 8th Grade Math Score: 0.062%<br>Seamless 4-yr Enrollment: 41%","School: North <br>HS Graduates (N): 205<br>Average 8th Grade Math Score: 0.168%<br>Seamless 4-yr Enrollment: 41%","School: South <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: 0.320%<br>Seamless 4-yr Enrollment: 44%","School: Peak <br>HS Graduates (N): 257<br>Average 8th Grade Math Score: 0.122%<br>Seamless 4-yr Enrollment: 44%","School: City <br>HS Graduates (N): 194<br>Average 8th Grade Math Score: 0.211%<br>Seamless 4-yr Enrollment: 51%","School: White Sands <br>HS Graduates (N): 392<br>Average 8th Grade Math Score: 0.493%<br>Seamless 4-yr Enrollment: 53%","School: Central <br>HS Graduates (N): 337<br>Average 8th Grade Math Score: 0.465%<br>Seamless 4-yr Enrollment: 53%","School: Camino <br>HS Graduates (N): 453<br>Average 8th Grade Math Score: 0.714%<br>Seamless 4-yr Enrollment: 65%","School: Orchard <br>HS Graduates (N): 529<br>Average 8th Grade Math Score: 0.951%<br>Seamless 4-yr Enrollment: 73%"],"type":"scatter","line":{"fillcolor":"rgba(255,127,14,1)","color":"black"},"xaxis":"x","yaxis":"y","frame":null}],"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.2,"selected":{"opacity":1}},"base_url":"https://plot.ly"},"evals":["config.modeBarButtonsToAdd.0.click"],"jsHooks":{"render":[{"code":"function(el, x) { var ctConfig = crosstalk.var('plotlyCrosstalkOpts').set({\"on\":\"plotly_click\",\"persistent\":false,\"dynamic\":false,\"selectize\":false,\"opacityDim\":0.2,\"selected\":{\"opacity\":1}}); }","data":null}]}}</script>
<script type="application/json" data-opts-chunk="1">{"fig.width":6.5,"fig.height":4,"fig.retina":2,"fig.align":"default","fig.keep":"high","fig.show":"asis","out.width":624,"warning":false,"error":false,"message":false,"exercise.df_print":"paged","exercise.checker":"NULL"}</script>
</div>
</div>
<div id="section-review" class="section level3">
<h3>Review</h3>
<p>Recap:</p>
<ol style="list-style-type: decimal">
<li><p>Start your graph using the basic <code>plot_ly()</code> arguments such as <code>x</code>, <code>y</code>, and <code>data</code>.</p></li>
<li><p>Modify bars or markers, and hover text also inside <code>plot_ly()</code></p></li>
<li><p>To add more data in the <code>plot_ly()</code> format to the same graph, use <code>add_trace()</code> and put in the <code>plot_ly()</code> arguments</p></li>
<li><p>Modify titles, axes, legends, and add basic shape lines and text using <code>layout()</code></p></li>
</ol>
<p>Now it’s your turn.</p>
</div>
</div>
<div id="section-plotly-exercises" class="section level2">
<h2>Plotly Exercises</h2>
<div id="section-basic-bar-graph-and-custom-tooltips" class="section level3">
<h3>Basic Bar Graph (and Custom Tooltips)</h3>
<p>Instead of a scatter plot we will be building a bar graph as an exercise. Each bar will be a school and our outcome will be 4-year enrollment. Because we want the bars sorted from lowest to highest by enrollment, we use <code>order_4yr</code> as our <code>x</code> variable. We will add the school names to the x-axis in a later step. In the exercise below, there is an outline of the graph code. Input the missing information. Remember, you can return to the first page if you need to see variable names and refer to the previous scatter plot example. Feel free to change the colors or other features. A full list of colors is available <a href="https://www.w3.org/TR/css3-color/#svg-color">here</a>. Note: You could also use RGB or hex colors.</p>
<div class="tutorial-exercise" data-label="basic-bar" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="0">
<pre class="text"><code>bar_1 <- plot_ly(data = schoolData, x = ?, y = ?, type="?", name="Seamless",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
"Seamless Enrollment: ", format(schoolData$pct_4yr, digits = 2), "%", "<br>",
"Delayed Enrollment: ", format(schoolData$pct_4yr_delayed, digits = 2), "%",
sep=""
),
hoverinfo = "?",
color = I("navy")
)
bar_1</code></pre>
<script type="application/json" data-opts-chunk="1">{"fig.width":6.5,"fig.height":4,"fig.retina":2,"fig.align":"default","fig.keep":"high","fig.show":"asis","out.width":624,"warning":true,"error":false,"message":true,"exercise.df_print":"paged","exercise.checker":"NULL"}</script>
</div>
<div class="tutorial-exercise-support" data-label="basic-bar-solution" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="0">
<pre class="text"><code>bar_1 <- plot_ly(data = schoolData, x = ~order_4yr, y = ~pct_4yr, type="bar", name="Seamless",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
"Seamless Enrollment: ", format(schoolData$pct_4yr, digits = 2), "%", "<br>",
"Delayed Enrollment: ", format(schoolData$pct_4yr_delayed, digits = 2), "%",
sep=""
),
hoverinfo = "text",
color = I("navy")
)
bar_1</code></pre>
</div>
<p>Clicking the solution button will review the solution. Did you get it?</p>
</div>
<div id="section-add-additional-bars-side-by-side" class="section level3">
<h3>Add Additional Bars Side-By-Side</h3>
<p>Let’s also add the delayed enrollment percentages for each school to our graph. This is equivalent to adding another <code>plot_ly</code> graph, so we use <code>add_trace</code>. We have to add the trace to <code>bar_1</code> so <code>bar_1</code> is already filled in from the previous exercise. If you want to use your own graph, copy and paste your code from the previous exercise. Complete the outline for <code>add_trace()</code>.</p>
<div class="tutorial-exercise" data-label="bar-add-trace" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="0">
<pre class="text"><code># bar_1 complete or add your code here
bar_1 <- plot_ly(data = schoolData, x = ~order_4yr, y = ~pct_4yr, type="bar", name="Seamless",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
"Seamless Enrollment: ", format(schoolData$pct_4yr, digits = 2), "%", "<br>",
"Delayed Enrollment: ", format(schoolData$pct_4yr_delayed, digits = 2), "%",
sep=""
),
hoverinfo = "text",
color = I("navy")
)
# End bar_1 or your code
# ***Fill in add_trace()***
bar_2 <- add_trace(?, x = ?, y = ?, 2, name = "Delayed",
color = I("orange"),
hoverinfo = "none"
)
bar_2</code></pre>
<script type="application/json" data-opts-chunk="1">{"fig.width":6.5,"fig.height":4,"fig.retina":2,"fig.align":"default","fig.keep":"high","fig.show":"asis","out.width":624,"warning":true,"error":false,"message":true,"exercise.df_print":"paged","exercise.checker":"NULL"}</script>
</div>
<div class="tutorial-exercise-support" data-label="bar-add-trace-solution" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="0">
<pre class="text"><code>bar_1 <- plot_ly(data = schoolData, x = ~order_4yr, y = ~pct_4yr, type="bar", name="Seamless",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
"Seamless Enrollment: ", format(schoolData$pct_4yr, digits = 2), "%", "<br>",
"Delayed Enrollment: ", format(schoolData$pct_4yr_delayed, digits = 2), "%",
sep=""
),
hoverinfo = "text",
color = I("navy")
)
bar_2 <- add_trace(bar_1, x = ~order_4yr, y = ~pct_4yr_delayed, 2, name = "Delayed",
color = I("orange"),
hoverinfo = "none"
)
bar_2</code></pre>
</div>
</div>
<div id="section-stacking-bars" class="section level3">
<h3>Stacking Bars</h3>
<p>To stack the bars, we use <code>layout()</code> and add <code>barmode = "stack"</code>. Add this below.</p>
<div class="tutorial-exercise" data-label="bar-stack" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="20">
<pre class="text"><code># bar_1 complete or add your code here
bar_1 <- plot_ly(data = schoolData, x = ~order_4yr, y = ~pct_4yr, type="bar", name="Seamless",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
"Seamless Enrollment: ", format(schoolData$pct_4yr, digits = 2), "%", "<br>",
"Delayed Enrollment: ", format(schoolData$pct_4yr_delayed, digits = 2), "%",
sep=""
),
hoverinfo = "text",
color = I("navy")
)
# End bar_1 or your code
# bar_2 complete or add your code here
bar_2 <- add_trace(bar_1, x = ~order_4yr, y = ~pct_4yr_delayed, 2, name = "Delayed",
color = I("orange"),
hoverinfo = "none"
)
# End bar_2 or your code
# ***Fill in bar_3***
bar_3 <- layout(?, barmode = "?")
bar_3</code></pre>
<script type="application/json" data-opts-chunk="1">{"fig.width":6.5,"fig.height":4,"fig.retina":2,"fig.align":"default","fig.keep":"high","fig.show":"asis","out.width":624,"warning":true,"error":false,"message":true,"exercise.df_print":"paged","exercise.checker":"NULL"}</script>
</div>
<div class="tutorial-exercise-support" data-label="bar-stack-solution" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="0">
<pre class="text"><code>bar_1 <- plot_ly(data = schoolData, x = ~order_4yr, y = ~pct_4yr, type="bar", name="Seamless",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
"Seamless Enrollment: ", format(schoolData$pct_4yr, digits = 2), "%", "<br>",
"Delayed Enrollment: ", format(schoolData$pct_4yr_delayed, digits = 2), "%",
sep=""
),
hoverinfo = "text",
color = I("navy")
)
bar_2 <- add_trace(bar_1, x = ~order_4yr, y = ~pct_4yr_delayed, 2, name = "Delayed",
color = I("orange"),
hoverinfo = "none"
)
bar_3 <- layout(bar_2, barmode = "stack")
bar_3</code></pre>
</div>
</div>
<div id="section-add-lines-and-labels" class="section level3">
<h3>Add Lines and Labels</h3>
<p>Just like in the previous scatter example, we will now add lines and text, and change the layout to add titles.</p>
<div class="tutorial-exercise" data-label="bar-layout" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="30">
<pre class="text"><code># bar_1 complete or add your code here
bar_1 <- plot_ly(data = schoolData, x = ~order_4yr, y = ~pct_4yr, type="bar", name="Seamless",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
"Seamless Enrollment: ", format(schoolData$pct_4yr, digits = 2), "%", "<br>",
"Delayed Enrollment: ", format(schoolData$pct_4yr_delayed, digits = 2), "%",
sep=""
),
hoverinfo = "text",
color = I("navy")
)
# End bar_1 or your code
# bar_2 complete or add your code here
bar_2 <- add_trace(bar_1, x = ~order_4yr, y = ~pct_4yr_delayed, 2, name = "Delayed",
color = I("orange"),
hoverinfo = "none"
)
# End bar_2 or your code
# bar_3 complete or add your code here
bar_3 <- layout(bar_2, barmode = "stack")
# End bar_3 or your code
# Added lines and text - nothing to do here, filled in for you
# Average enrollment line
avg_line <- list(
type = 'line',
line = list(color = "black", dash = 'dash'),
x0 = 0,
x1 = 11,
y0 = agencyData$pct_4yr,
y1 = agencyData$pct_4yr
)
# Average enrollment line label - Nothing to fill in here, done for you
# X position chosen by looking at number of schools and divide that in half for the x position
avg_line_label <- list(
text="Average 4-year Enrollment",
x=5.5,
y=agencyData$pct_4yr,
showarrow = FALSE,
xanchor = 'center',
yanchor = 'bottom'
)
# ***Fill in the missing information in layout outline***
bar_3_with_opts <- layout(?,
title = "Average 4-Year Seamless and Delayed Enrollment by High School",
xaxis = ?(
title = "High School",
tickvals = 1:length(schoolData$last_hs_name),
ticktext = schoolData$last_hs_name,
zeroline = ?,
showline = ?
),
yaxis = ?(
title = "High School Graduates (%)",
range = c(0,?)
),
margin = list(b = 100, t = 100),
shapes = list(?),
annotations = list(?)
)
bar_3_with_opts</code></pre>
<script type="application/json" data-opts-chunk="1">{"fig.width":6.5,"fig.height":4,"fig.retina":2,"fig.align":"default","fig.keep":"high","fig.show":"asis","out.width":624,"warning":true,"error":false,"message":true,"exercise.df_print":"paged","exercise.checker":"NULL"}</script>
</div>
<div class="tutorial-exercise-support" data-label="bar-layout-solution" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="0">
<pre class="text"><code>bar_1 <- plot_ly(data = schoolData, x = ~order_4yr, y = ~pct_4yr, type="bar", name="Seamless",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
"Seamless Enrollment: ", format(schoolData$pct_4yr, digits = 2), "%", "<br>",
"Delayed Enrollment: ", format(schoolData$pct_4yr_delayed, digits = 2), "%",
sep=""
),
hoverinfo = "text",
color = I("navy")
)
bar_2 <- add_trace(bar_1, x = ~order_4yr, y = ~pct_4yr_delayed, 2, name = "Delayed",
color = I("orange"),
hoverinfo = "none"
)
bar_3 <- layout(bar_2, barmode = "stack")
# Average enrollment line
avg_line <- list(
type = 'line',
line = list(color = "black", dash = 'dash'),
x0 = 0,
x1 = 11,
y0 = agencyData$pct_4yr,
y1 = agencyData$pct_4yr
)
# Average enrollment line label
# X position chosen by looking at number of schools and dividing that in half, could be done dynamically
avg_line_label <- list(
text="Average 4-year Enrollment",
x=5.5,
y=agencyData$pct_4yr,
showarrow = FALSE,
xanchor = 'center',
yanchor = 'bottom'
)
bar_3_with_opts <- layout(bar_3,
title = "Average 4-Year Seamless and Delayed Enrollment by High School",
xaxis = list(
title = "High School",
tickvals = 1:length(schoolData$last_hs_name),
ticktext = schoolData$last_hs_name,
zeroline = TRUE,
showline = TRUE
),
yaxis = list(
title = "High School Graduates (%)",
range = c(0,100)
),
margin = list(b = 100, t = 100),
shapes = list(avg_line),
annotations = list(avg_line_label)
)
bar_3_with_opts</code></pre>
</div>
<div class="panel panel-default">
<div class="panel-heading tutorial-panel-heading">Quiz</div>
<table class="table quiz-table">
<tr>
<td>
<div id="htmlwidget-7670d01199d8ba4e09d9" style="width:100%;height:auto;", class = "quiz html-widget">
<div class="panel panel-default">
<div class="panel-body quizArea">
</div>
</div>
</div>
<script type="application/json" data-for="htmlwidget-7670d01199d8ba4e09d9">{"x":{"question":"Check your understanding by filling in the blanks. _____ set the categorical range for the x-axis ticks and _____ labeled the ticks with the high school names.","answers":[{"option":"ticktext, tickvals","correct":false,"message":null},{"option":"range, ticktext","correct":false,"message":null},{"option":"range, title","correct":false,"message":null},{"option":"tickvals, ticktext","correct":true,"message":null}],"label":"xaxis-question-1","skipStartButton":true,"perQuestionResponseAnswers":true,"perQuestionResponseMessaging":true,"preventUnanswered":true,"displayQuestionCount":false,"displayQuestionNumber":false,"disableRanking":true,"nextQuestionText":"","checkAnswerText":"Submit Answer","allowRetry":false,"randomSortAnswers":false,"json":{"info":{"name":"","main":""},"questions":[{"q":"Check your understanding by filling in the blanks. _____ set the categorical range for the x-axis ticks and _____ labeled the ticks with the high school names.","a":[{"option":"ticktext, tickvals","correct":false,"message":null},{"option":"range, ticktext","correct":false,"message":null},{"option":"range, title","correct":false,"message":null},{"option":"tickvals, ticktext","correct":true,"message":null}],"correct":"Correct!","incorrect":"Incorrect."}]}},"evals":[],"jsHooks":[]}</script>
</td>
</tr>
</table>
</div>
</div>
</div>
<div id="section-quick-aside-on-functions" class="section level2">
<h2>Quick Aside on Functions</h2>
<p>To make our graph interactive, we have to make it so that the graph code changes the arguments and data elements within it based on user input. The easiest way to do this is to change our graph code into a function. OUr function will take basic input arguments and use those inputs to change the output. A very basic example of a function is below. Put whatever text you want where it says “Your text here” and watch it get printed out.</p>
<div class="tutorial-exercise" data-label="basic-function" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="0">
<pre class="text"><code>printme <- function(x){
print(x)
}
printme(x="Your text here")</code></pre>
<script type="application/json" data-opts-chunk="1">{"fig.width":6.5,"fig.height":4,"fig.retina":2,"fig.align":"default","fig.keep":"high","fig.show":"asis","out.width":624,"warning":false,"error":false,"message":false,"exercise.df_print":"paged","exercise.checker":"NULL"}</script>
</div>
<p>We want to do the same thing for our graphing code so that we can have the graph be for 2-year or 4-year enrollment. Our input is “2” or “4” and graph output changes based on that input. Below we can take all parameters in the college enrollment scatter that should change with the selection of 2-year or 4-year enrollment and change them using and if statement. Put 2 or 4 into the function at the bottom and watch the graph change.</p>
<div class="tutorial-exercise" data-label="scatter-function" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="30">
<pre class="text"><code>scatter_function <- function(college_type){
if (college_type == 2){
school_y <- schoolData$pct_2yr
agency_y <- agencyData$pct_2yr
tooltip_title <- "Seamless 2-yr Enrollment: "
avg_label <- "Average 2-yr Enrollment"
title <- "Relationship between Average Two-Year College Graduation <br> and Average 8th Grade Achivement for High Schools"
}
else if (college_type==4){
school_y <- schoolData$pct_4yr
agency_y <- agencyData$pct_4yr
tooltip_title <- "Seamless 4-yr Enrollment: "
avg_label <- "Average 4-yr Enrollment"
title <- "Relationship between Average Four-Year College Graduation <br> and Average 8th Grade Achivement for High Schools"
}
else{
stop("college_type must be 2 or 4")
}
# Same plot but make the markers bigger, change the marker colors
# Named colors list https://www.w3.org/TR/css3-color/#svg-color
plot_2 <- plot_ly(data = schoolData, x = ~avg_test_math_8_std, y = school_y, mode = "markers", type="scatter",
marker = list(size=10,
color = "steelblue",
line = list(
color = "navy",
width = 2
)),
hoverinfo = "text",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
"Average 8th Grade Math Score: ", format(schoolData$avg_test_math_8_std, digits=2), "%", "<br>",
tooltip_title, format(school_y, digits=2), "%",
sep=""
)
)
# Adding lines
# Add fit line
fit <- lm(school_y ~ avg_test_math_8_std, data = schoolData)
plot_3 <- plot_2 %>%
add_trace(x = schoolData$avg_test_math_8_std, y = fitted(fit), mode = "lines", line = list(color = "black"))
plot_3
# Create agency average lines using shapes - these lines will be added in next step
# Average 8th grade score
avg_8th_line <- list(
type = 'line',
line = list(color = "orange", dash = 'dash'),
x0 = agencyData$avg_test_math_8_std,
x1 = agencyData$avg_test_math_8_std,
y0 = 0,
y1 = 100
)
# Average enrollment
avg_4yr_line <- list(
type = 'line',
line = list(color = "orange", dash = 'dash'),
x0 = -.5,
x1 = 1,
y0 = agency_y,
y1 = agency_y
)
# Average 8th grade line label
avg_8th_label <- list(
text="Avg 8th Grade Score",
x=agencyData$avg_test_math_8_std,
y=100,
showarrow = FALSE,
xanchor = 'left',
yanchor = 'top'
)
# Average enrollment line label
avg_4yr_label <- list(
text=avg_label,
x=-.5,
y=agency_y,
showarrow = FALSE,
xanchor = 'left',
yanchor = 'bottom'
)
# Add lines and Styling options to plot 3
plot_3_with_opts <- layout(plot_3,
title = title,
xaxis = list(
title = "Average 8th Grade Math Test Score (Std. Dev. Units)",
range = c(-.5, 1),
zeroline = TRUE,
showline = TRUE
),
yaxis = list(
title = "High School Graduates (%)",
range = c(0,100)
),
margin = list(b = 100, t = 100),
showlegend = FALSE,
shapes = list(avg_8th_line, avg_4yr_line),
annotations = list(avg_8th_label, avg_4yr_label)
)
return(plot_3_with_opts)
} # End function
scatter_function(college_type=2)</code></pre>
<div id="106b91908cd23" style="width:624px;height:384px;" class="plotly html-widget"></div>
<script type="application/json" data-for="106b91908cd23">{"x":{"visdat":{"106b979d14118":["function () ","plotlyVisDat"]},"cur_data":"106b979d14118","attrs":{"106b979d14118":{"x":{},"y":[32.5581395348837,26.6666666666667,18.6311787072243,25.3658536585366,27.816091954023,29.5719844357977,16.4948453608247,26.2755102040816,24.6290801186944,21.1920529801325,14.1776937618147],"mode":"markers","marker":{"size":10,"color":"steelblue","line":{"color":"navy","width":2}},"hoverinfo":"text","text":{},"alpha":1,"sizes":[10,100],"type":"scatter"},"106b979d14118.1":{"x":[-0.0572368162790698,-0.17006474045977,0.0620150684410646,0.168237465365854,0.320300586206897,0.122428973929961,0.21138548814433,0.492871761734694,0.46508424925816,0.713709099337748,0.951112215879017],"y":[27.2620765808527,28.3155862900513,26.148584337641,25.1567525078238,23.7368914638458,25.5844807572079,24.7538658885766,22.1255402822431,22.3850009999784,20.0635128098084,17.8468054646509],"mode":"lines","marker":{"size":10,"color":"steelblue","line":{"color":"navy","width":2}},"hoverinfo":"text","text":{},"alpha":1,"sizes":[10,100],"type":"scatter","line":{"color":"black"}}},"layout":{"margin":{"b":100,"l":60,"t":100,"r":10},"title":"Relationship between Average Two-Year College Graduation <br> and Average 8th Grade Achivement for High Schools","xaxis":{"domain":[0,1],"title":"Average 8th Grade Math Test Score (Std. Dev. Units)","range":[-0.5,1],"zeroline":true,"showline":true},"yaxis":{"domain":[0,1],"title":"High School Graduates (%)","range":[0,100]},"showlegend":false,"shapes":[{"type":"line","line":{"color":"orange","dash":"dash"},"x0":0.359298796035125,"x1":0.359298796035125,"y0":0,"y1":100},{"type":"line","line":{"color":"orange","dash":"dash"},"x0":-0.5,"x1":1,"y0":23.6029803086748,"y1":23.6029803086748}],"annotations":[{"text":"Avg 8th Grade Score","x":0.359298796035125,"y":100,"showarrow":false,"xanchor":"left","yanchor":"top"},{"text":"Average 2-yr Enrollment","x":-0.5,"y":23.6029803086748,"showarrow":false,"xanchor":"left","yanchor":"bottom"}],"hovermode":"closest"},"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":[-0.0572368162790698,-0.17006474045977,0.0620150684410646,0.168237465365854,0.320300586206897,0.122428973929961,0.21138548814433,0.492871761734694,0.46508424925816,0.713709099337748,0.951112215879017],"y":[32.5581395348837,26.6666666666667,18.6311787072243,25.3658536585366,27.816091954023,29.5719844357977,16.4948453608247,26.2755102040816,24.6290801186944,21.1920529801325,14.1776937618147],"mode":"markers","marker":{"fillcolor":"rgba(31,119,180,1)","color":"steelblue","size":10,"line":{"color":"navy","width":2}},"hoverinfo":"text","text":["School: River <br>HS Graduates (N): 258<br>Average 8th Grade Math Score: -0.057%<br>Seamless 2-yr Enrollment: 33%","School: West Spring Hill <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: -0.170%<br>Seamless 2-yr Enrollment: 27%","School: Upland <br>HS Graduates (N): 263<br>Average 8th Grade Math Score: 0.062%<br>Seamless 2-yr Enrollment: 19%","School: North <br>HS Graduates (N): 205<br>Average 8th Grade Math Score: 0.168%<br>Seamless 2-yr Enrollment: 25%","School: South <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: 0.320%<br>Seamless 2-yr Enrollment: 28%","School: Peak <br>HS Graduates (N): 257<br>Average 8th Grade Math Score: 0.122%<br>Seamless 2-yr Enrollment: 30%","School: City <br>HS Graduates (N): 194<br>Average 8th Grade Math Score: 0.211%<br>Seamless 2-yr Enrollment: 16%","School: White Sands <br>HS Graduates (N): 392<br>Average 8th Grade Math Score: 0.493%<br>Seamless 2-yr Enrollment: 26%","School: Central <br>HS Graduates (N): 337<br>Average 8th Grade Math Score: 0.465%<br>Seamless 2-yr Enrollment: 25%","School: Camino <br>HS Graduates (N): 453<br>Average 8th Grade Math Score: 0.714%<br>Seamless 2-yr Enrollment: 21%","School: Orchard <br>HS Graduates (N): 529<br>Average 8th Grade Math Score: 0.951%<br>Seamless 2-yr Enrollment: 14%"],"type":"scatter","xaxis":"x","yaxis":"y","frame":null},{"x":[-0.0572368162790698,-0.17006474045977,0.0620150684410646,0.168237465365854,0.320300586206897,0.122428973929961,0.21138548814433,0.492871761734694,0.46508424925816,0.713709099337748,0.951112215879017],"y":[27.2620765808527,28.3155862900513,26.148584337641,25.1567525078238,23.7368914638458,25.5844807572079,24.7538658885766,22.1255402822431,22.3850009999784,20.0635128098084,17.8468054646509],"mode":"lines+markers","marker":{"size":10,"color":"steelblue","line":{"color":"navy","width":2}},"hoverinfo":"text","text":["School: River <br>HS Graduates (N): 258<br>Average 8th Grade Math Score: -0.057%<br>Seamless 2-yr Enrollment: 33%","School: West Spring Hill <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: -0.170%<br>Seamless 2-yr Enrollment: 27%","School: Upland <br>HS Graduates (N): 263<br>Average 8th Grade Math Score: 0.062%<br>Seamless 2-yr Enrollment: 19%","School: North <br>HS Graduates (N): 205<br>Average 8th Grade Math Score: 0.168%<br>Seamless 2-yr Enrollment: 25%","School: South <br>HS Graduates (N): 435<br>Average 8th Grade Math Score: 0.320%<br>Seamless 2-yr Enrollment: 28%","School: Peak <br>HS Graduates (N): 257<br>Average 8th Grade Math Score: 0.122%<br>Seamless 2-yr Enrollment: 30%","School: City <br>HS Graduates (N): 194<br>Average 8th Grade Math Score: 0.211%<br>Seamless 2-yr Enrollment: 16%","School: White Sands <br>HS Graduates (N): 392<br>Average 8th Grade Math Score: 0.493%<br>Seamless 2-yr Enrollment: 26%","School: Central <br>HS Graduates (N): 337<br>Average 8th Grade Math Score: 0.465%<br>Seamless 2-yr Enrollment: 25%","School: Camino <br>HS Graduates (N): 453<br>Average 8th Grade Math Score: 0.714%<br>Seamless 2-yr Enrollment: 21%","School: Orchard <br>HS Graduates (N): 529<br>Average 8th Grade Math Score: 0.951%<br>Seamless 2-yr Enrollment: 14%"],"type":"scatter","line":{"fillcolor":"rgba(255,127,14,1)","color":"black"},"xaxis":"x","yaxis":"y","frame":null}],"highlight":{"on":"plotly_click","persistent":false,"dynamic":false,"selectize":false,"opacityDim":0.2,"selected":{"opacity":1}},"base_url":"https://plot.ly"},"evals":["config.modeBarButtonsToAdd.0.click"],"jsHooks":{"render":[{"code":"function(el, x) { var ctConfig = crosstalk.var('plotlyCrosstalkOpts').set({\"on\":\"plotly_click\",\"persistent\":false,\"dynamic\":false,\"selectize\":false,\"opacityDim\":0.2,\"selected\":{\"opacity\":1}}); }","data":null}]}}</script>
<script type="application/json" data-opts-chunk="1">{"fig.width":6.5,"fig.height":4,"fig.retina":2,"fig.align":"default","fig.keep":"high","fig.show":"asis","out.width":624,"warning":false,"error":false,"message":false,"exercise.df_print":"paged","exercise.checker":"NULL"}</script>
</div>
<p>Time permitting, try the same exercise on our bar chart.</p>
<div class="tutorial-exercise" data-label="bar-function" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="30">
<pre class="text"><code>bar_function <- function(college_type){
# 2-year is filled in for you
if (college_type == 2){
school_seamless <- schoolData$pct_2yr
agency_seamless <- agencyData$pct_2yr
school_delayed <- schoolData$pct_2yr_delayed
order <- schoolData$order_2yr
tooltip_title_seamless <- "Seamless 2-yr Enrollment: "
tooltip_title_delayed <- "Delayed 2-yr Enrollment: "
avg_label <- "Average 2-yr Enrollment"
title <- "Average 2-Year Seamless and Delayed Enrollment by High School"
}
# ***Try 4-year***
else if (college_type==4){
school_seamless <- ?
agency_seamless <- ?
school_delayed <- ?
order <- ?
tooltip_title_seamless <- ?
tooltip_title_delayed <- ?
avg_label <- ?
title <- ?
}
else{
stop("college_type must be 2 or 4")
}
# Fill in the inputs for bar_1. The rest is done for you.
bar_1 <- plot_ly(data = schoolData, x = ?, y = ?, type="bar", name="Seamless",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
? , format(?, digits = 2), "%", "<br>",
?, format(?, digits = 2), "%",
sep=""
),
hoverinfo = "text",
color = I("navy")
)
bar_1
# Add delayed enrollment to graph - turn the hover off using hoverinfo = none
bar_2 <- add_trace(bar_1, x = order, y = school_delayed, 2, name = "Delayed",
color = I("orange"),
hoverinfo = "none"
)
bar_2
# Stack them up! Change the barmode to stack in layout
bar_3 <- layout(bar_2, barmode = "stack")
bar_3
# Add line and line label for average seamless rate and format the plot with style()
# Average enrollment line
avg_line <- list(
type = 'line',
line = list(color = "black", dash = 'dash'),
x0 = 0,
x1 = 11,
y0 = agency_seamless,
y1 = agency_seamless
)
# Average enrollment line label
# Hint: look at the number of schools and divide that in half for the x position
avg_line_label <- list(
text=avg_label,
x=5.5,
y=agency_seamless,
showarrow = FALSE,
xanchor = 'center',
yanchor = 'bottom'
)
bar_3_with_opts <- layout(bar_3,
title = title,
xaxis = list(
title = "High School",
tickvals = 1:length(schoolData$last_hs_name),
ticktext = schoolData$last_hs_name,
zeroline = TRUE,
showline = TRUE
),
yaxis = list(
title = "High School Graduates (%)",
range = c(0,100)
),
margin = list(b = 100, t = 100),
shapes = list(avg_line),
annotations = list(avg_line_label)
)
bar_3_with_opts
# Make sure you return the output
return(bar_3_with_opts)
} # End function
# Put in 2 or 4 here and watch the graph change when you run the code!
bar_function(2)</code></pre>
<script type="application/json" data-opts-chunk="1">{"fig.width":6.5,"fig.height":4,"fig.retina":2,"fig.align":"default","fig.keep":"high","fig.show":"asis","out.width":624,"warning":false,"error":false,"message":false,"exercise.df_print":"paged","exercise.checker":"NULL"}</script>
</div>
<div class="tutorial-exercise-support" data-label="bar-function-solution" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="0">
<pre class="text"><code>bar_function <- function(college_type){
if (college_type == 2){
school_seamless <- schoolData$pct_2yr
agency_seamless <- agencyData$pct_2yr
school_delayed <- schoolData$pct_2yr_delayed
order <- schoolData$order_2yr
tooltip_title_seamless <- "Seamless 2-yr Enrollment: "
tooltip_title_delayed <- "Delayed 2-yr Enrollment: "
avg_label <- "Average 2-yr Enrollment"
title <- "Average 2-Year Seamless and Delayed Enrollment by High School"
}
else if (college_type==4){
school_seamless <- schoolData$pct_4yr
agency_seamless <- agencyData$pct_4yr
school_delayed <- schoolData$pct_4yr_delayed
order <- schoolData$order_4yr
tooltip_title_seamless <- "Seamless 4-yr Enrollment: "
tooltip_title_delayed <- "Delayed 4-yr Enrollment: "
avg_label <- "Average 4-yr Enrollment"
title <- "Average 4-Year Seamless and Delayed Enrollment by High School"
}
else{
stop("college_type must be 2 or 4")
}
bar_1 <- plot_ly(data = schoolData, x = order, y = school_seamless, type="bar", name="Seamless",
text = ~paste(
"School: ", schoolData$last_hs_name, "<br>",
"HS Graduates (N): ", schoolData$hs_diploma, "<br>",
tooltip_title_seamless , format(school_seamless, digits = 2), "%", "<br>",
tooltip_title_delayed, format(school_delayed, digits = 2), "%",
sep=""
),
hoverinfo = "text",
color = I("navy")
)
bar_1
# Add delayed enrollment to graph - turn the hover off using hoverinfo = none
bar_2 <- add_trace(bar_1, x = order, y = school_delayed, 2, name = "Delayed",
color = I("orange"),
hoverinfo = "none"
)
bar_2
# Stack them up! Change the barmode to stack in layout
bar_3 <- layout(bar_2, barmode = "stack")
bar_3
# Add line and line label for average seamless rate and format the plot with style()
# Average enrollment line
avg_line <- list(
type = 'line',
line = list(color = "black", dash = 'dash'),
x0 = 0,
x1 = 11,
y0 = agency_seamless,
y1 = agency_seamless
)
# Average enrollment line label
# Hint: look at the number of schools and divide that in half for the x position
avg_line_label <- list(
text=avg_label,
x=5.5,
y=agency_seamless,
showarrow = FALSE,
xanchor = 'center',
yanchor = 'bottom'
)
bar_3_with_opts <- layout(bar_3,
title = title,
xaxis = list(
title = "High School",
tickvals = 1:length(schoolData$last_hs_name),
ticktext = schoolData$last_hs_name,
zeroline = TRUE,
showline = TRUE
),
yaxis = list(
title = "High School Graduates (%)",
range = c(0,100)
),
margin = list(b = 100, t = 100),
shapes = list(avg_line),
annotations = list(avg_line_label)
)
bar_3_with_opts
# Make sure you return the output
return(bar_3_with_opts)
} # End function
# Put in 2 or 4 here and watch the graph change when you run the code!
bar_function(2)</code></pre>
</div>
</div>
<div id="section-shiny-plotly" class="section level2">
<h2>Shiny + Plotly</h2>
<p>Shiny will allow us to add interactivity to our graphs and even build a full website using R. Here is a <a href="http://shiny.rstudio.com/images/shiny-cheatsheet.pdf">reference page</a> for building a Shiny App. Shiny apps can get very complicated, but the basic template is simple:</p>
<ol style="list-style-type: decimal">
<li><p>Define <code>ui</code> (user interface). Typically this is <code>fluidpage</code>. This is where the content of the webpage goes. <code>ui <- fluidpage()</code></p></li>
<li><p>Define <code>server <- function(input, output) {}</code> Inside the curly braces put what is updated based on the input from the user interface.</p></li>
<li><p>Add the line <code>shinyApp(ui = ui, server = server)</code></p></li>
</ol>
<p>Done!</p>
<p>User input comes from Shiny widgets. There are <a href="https://shiny.rstudio.com/gallery/widget-gallery.html">many other widgets available</a>. Some example widgets are below. Feel free to try out other widgets from the gallery.</p>
<div class="tutorial-exercise" data-label="shinywidgets" data-caption="Code" data-completion="1" data-diagnostics="1" data-lines="0">
<pre class="text"><code> radioButtons("buttons", label = "Select Enrollment Type:",
choices = list("2-year" = 2, "4-year" = 4)
)</code></pre>
<div id="buttons" class="form-group shiny-input-radiogroup shiny-input-container">
<label class="control-label" for="buttons">Select Enrollment Type:</label>
<div class="shiny-options-group">
<div class="radio">
<label>
<input type="radio" name="buttons" value="2" checked="checked"/>
<span>2-year</span>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="buttons" value="4"/>
<span>4-year</span>
</label>
</div>
</div>
</div>
<pre class="text"><code> selectInput("select", label = h3("Select a School"),
choices = list("School A" = 1, "School B" = 2, "School C" = 3),
selected = 1)</code></pre>
<div class="form-group shiny-input-container">
<label class="control-label" for="select">
<h3>Select a School</h3>
</label>
<div>
<select id="select"><option value="1" selected>School A</option>
<option value="2">School B</option>
<option value="3">School C</option></select>
<script type="application/json" data-for="select" data-nonempty="">{}</script>
</div>
</div>
<script type="application/json" data-opts-chunk="1">{"fig.width":6.5,"fig.height":4,"fig.retina":2,"fig.align":"default","fig.keep":"high","fig.show":"asis","out.width":624,"warning":false,"error":false,"message":false,"exercise.df_print":"paged","exercise.checker":"NULL"}</script>
</div>
<div id="section-ending-interactivity" class="section level3">
<h3>Ending interactivity</h3>
<p>Unfotunately this concludes the interactive portion of the tutorial in your web browser because the <code>learnr</code> <a href="https://rstudio.github.io/learnr/">tutorial package</a> does not currently support embedding Shiny Apps. You may still follow along in this tutorial. You will run the remainder of the content on your local machine in RStudio using the R scripts on our <a href="https://github.com/strategicdataproject/dataviz-r">strategicdataproject/dataviz-r</a> github page.</p>
<p>To install RStudio, you must first download and install R <a href="https://www.r-project.org/">here</a>. Then, download and install RStudio <a href="https://www.rstudio.com/products/rstudio/download2/">here</a>. Once in RStudio, install the Shiny and Plotly packages by going to <code>Tools</code> -> <code>Install Packages</code>.</p>
</div>
<div id="section-example-scatter-in-shiny" class="section level3">
<h3>Example Scatter in Shiny</h3>
<p>After downloading the <a href="https://github.com/strategicdataproject/dataviz-r">Github repository</a>, open <code>1_shiny_example_scatter.R</code>. Scroll to line 126. Here you can see the layout for our ShinyApp. Within <code>fluidpage()</code> there is the <code>titlePanel</code>, which puts the title at the top, and <code>sidebarLayout</code> which gives the page a <code>sidebarPanel</code> where our controls are and a <code>mainPanel</code> where our graph lives. Inside <code>server</code>, we render our <code>scatterfunction</code> using <code>renderPlotly</code>.</p>
<pre class="r"><code># Simple example - control the scatter with a sidebar
ui <- fluidPage(
titlePanel("College Enrollment Scatter"),
sidebarLayout(
sidebarPanel(
radioButtons("buttons", label = "Select Enrollment Type:",
choices = list("2-year" = 2, "4-year" = 4)
)
),
mainPanel(plotlyOutput("scatter"))
)
)
server <- function(input, output) {
output$scatter <- renderPlotly({
scatter_function(college_type=input$buttons)
}) # End render plotly
}
shinyApp(ui, server)</code></pre>
<p>This is the result.</p>
<p><img src="images/scatter.JPG" width="100%" /></p>
</div>
<div id="section-try-it-shiny-bar" class="section level3">
<h3>Try It: Shiny Bar</h3>
<p>Your turn! Open <code>2a_shiny_bar_exercise.R</code> and fill in the ?’s using the scatter example as a guide. If you get stuck, view the solution in <code>2b_shiny_bar_solution.R</code>.</p>
<p>This is the result.</p>
<p><img src="images/bar.JPG" width="100%" /></p>
</div>
</div>
<div id="section-full-websites-with-shiny" class="section level2">
<h2>Full Websites with Shiny</h2>
<p>Finally, in the <a href="https://github.com/strategicdataproject/dataviz-r">Github Repository</a> you can view an example of a two-page website created using Shiny. We use the <code>shinythemes</code> package to give the website some styling and include both graphs as tabs on the page. There is also an example text page. View the code below.</p>
<p>This is the result. <img src="images/full_site_1.JPG" width="100%" /> <img src="images/full_site_2.JPG" width="100%" /></p>
<p>This concludes the tutorial. Good luck on your data visualization journeys!</p>
<script type="application/shiny-prerendered" data-context="server-start">
packages <- c("learnr","ggplot2","plotly", "shiny", "shinythemes")
lapply(packages, library, character.only = TRUE)
knitr::opts_chunk$set(echo = FALSE)
schoolData <- structure(list(last_hs_name = c("River ", "West Spring Hill ",
"Upland ", "North ", "South ", "Peak ", "City ", "White Sands ",
"Central ", "Camino ", "Orchard "), hs_diploma = c(258L, 435L,