-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortcourse.tex
1539 lines (1328 loc) · 58.8 KB
/
shortcourse.tex
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
\documentclass{beamer}\usepackage[]{graphicx}\usepackage[]{xcolor}
% maxwidth is the original width if it is less than linewidth
% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother
\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\let\hlipl\hlkwb
\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
\def\at@end@of@kframe{}%
\ifinner\ifhmode%
\def\at@end@of@kframe{\end{minipage}}%
\begin{minipage}{\columnwidth}%
\fi\fi%
\def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
\colorbox{shadecolor}{##1}\hskip-\fboxsep
% There is no \\@totalrightmargin, so:
\hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
\MakeFramed {\advance\hsize-\width
\@totalleftmargin\z@ \linewidth\hsize
\@setminipage}}%
{\par\unskip\endMakeFramed%
\at@end@of@kframe}
\makeatother
\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX
\usepackage{alltt}
\setbeamersize{text margin left=0.25cm,text margin right=0.25cm}
\title{Data Visualizations for Surveys Using ggplot2 \& ggsurvey \\
AAPOR 2022 Short Course}
\author{Brittany Alexander Ph.D. \\
Associate Statistician \\
Ipsos Public Affairs \\
Twitter: @balexanderstats}
\usepackage{hyperref}
\hypersetup{
colorlinks=true,
linkcolor=blue,
urlcolor=blue,
pdfpagemode=FullScreen,
}
\usecolortheme{beaver}
\setbeamertemplate{footline}[frame number]
\AtBeginSection[]
{
\begin{frame}
\frametitle{Table of Contents}
\tableofcontents[currentsection]
\end{frame}
}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}{Outline}
\tableofcontents
\end{frame}
\section{Course Objectives}
\begin{frame}{Course Objectives}
\begin{enumerate}
\item Learn about the Grammar of Graphics that ggplot2 uses
\item Learn how to use ggplot2 to visualize data
\item Learn how to use ggsurvey to create weighted bar and histograms
\item Learn how to visualize crosstabs using ggsurvey
\item Lean how to make other plots using ggsurvey and ggplot2
\end{enumerate}
\end{frame}
\begin{frame}{RCloud Setup}
Go to \href{https://rstudio.cloud/project/3797940}{R Cloud Link} and create a free account. This will give you access to an R Studio project with all the course materials pre-installed.
\end{frame}
\section{Introduction to ggplot2 and ggsurvey}
\begin{frame}{Required Software (all available free)}
\begin{enumerate}
\item R (version 3.5 or newer required)
\item RStudio highly recommended
\item ggplot2 R package
\item dplyr R package
\item survey \& srvyr R package
\item anesr R package
\item ggsurvey R package
\item palmerpenguins R package
\item stringr R package
\end{enumerate}
\end{frame}
\begin{frame}{Introducing ggplot2}
\begin{itemize}
\item ggplot2 is an R package created by Hadley Wickham that uses the grammar of graphics
\item ggplot2 is a part of the tidyverse which is a collection of R packages
\item ggplot2 is more flexible than base R plotting
\item ggplot2 looks more modern than base R plotting
\end{itemize}
\end{frame}
\begin{frame}{Grammar of Graphics}
\begin{itemize}
\item The philosopy of ggplot2 is laid out in Wickham (2010), and builds upon previous work by Wilkinson, Anand, and Grossman (2005), and Bertin (1983).
(1983)
\item The grammar of graphics is a framework for visualizations to break apart the elements of a plot:
\begin{enumerate}
\item the data and the mapping of the aesthetic in the plot
\item the geometric objects of a plot, (is it a bar chart or histogram or something else)
\item the scale of the plots
\item the facet specification (are we making separate plots for each level of another variable)
\end{enumerate}
\end{itemize}
\end{frame}
\begin{frame}{Dataset 1: mtcars}
mtcars is a built in data set in R that takes data from the 1974 Motor Trend magazine with 11 variables relating to 32 automobile design and performance. \\
Variables: mpg (miles per gallon), cyl (number of cylinders),disp (displacement), hp (horsepower), drat (rear axle ratio), wt (weight in 1000lbs), qsec (0.25 mile time), vs (engine 0 = v-shape, 1 = straight), am (transmission (0=automatic, 1 = manual), gear (number of gears), carb (number of carburetors).
\end{frame}
\begin{frame}[fragile]{Example 1 ggplot2}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{library}\hlstd{(ggplot2)}
\hlstd{plot} \hlkwb{=} \hlkwd{ggplot}\hlstd{(mtcars,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= hp,}
\hlkwc{y} \hlstd{= mpg))} \hlopt{+} \hlkwd{geom_point}\hlstd{()}
\hlstd{plot}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-2-1}
\end{knitrout}
\end{frame}
\begin{frame}{ggplot2 structure from the "Cheat Sheet"}
\href{https://www.rstudio.com/resources/cheatsheets/}{Tidyverse Cheat Sheets including ggplot2}
\begin{figure}
\includegraphics[height=0.75\textheight]{ggplot2cheatsheetbasic.png}
\end{figure}
\end{frame}
\begin{frame}{ggplot2 Options}
\href{https://ggplot2-book.org/}{ggplot2 book}
\begin{enumerate}
\item Plot types: density, histogram, boxplot, bar chart, violin plot, error bars for bar plots, maps
\item custom color schemes, add fill/color or shapes matching categorical variables
\item custom scales or coordinate styems for continuous variables
\item faceting to make plots for each level of a categorical variable
\item custom labels and legends
\end{enumerate}
\end{frame}
\begin{frame}[fragile]{Example ggplot2 add facet and title}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plotnew} \hlkwb{=} \hlstd{plot} \hlopt{+} \hlkwd{ggtitle}\hlstd{(}\hlstr{"MPG of Car given HP by
transmission"}\hlstd{)} \hlopt{+}
\hlkwd{xlab}\hlstd{(}\hlstr{"Horse Power"}\hlstd{)} \hlopt{+} \hlkwd{ylab}\hlstd{(}\hlstr{"Miles Per Gallon"}\hlstd{)} \hlopt{+}
\hlkwd{facet_grid}\hlstd{(}\hlopt{~}\hlstd{am)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example ggplot2 add facet and title}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plotnew}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-4-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Why ggsurvey}
\begin{itemize}
\item ggplot2 has all the necessary components to make visualizations of survey data but the code can get messy especially when handling weights or making crosstabs.
\item ggplot2 defaults to showing categorical data via counts, but proportions are often of interest with survey data especially when making comparisons across groups of different size.
\item ggsurvey creates functions to make common visualizations of survey data in a single function call while preserving some of the customization features in ggplot.
\item ggsurvey retains the compatibility of ggplot2 with the rest of the tidyverse
\item ggsurvey has functions to handle data frames (with weight as a variable) and functions that handle svy.design objects from survey package.
\end{itemize}
\end{frame}
\begin{frame}{ggsurvey Features}
\begin{itemize}
\item ggsurvey plot types: bar chart, histogram, boxplot, hexplot
\item has faceting built in using 2d (one facets), 3d (two facets)
\item uses weight argument in ggplot to handle weights
\item ggsurvey is a series of "wrapper" functions that call ggplot or other tidyverse functions
\item ggsurvey works with unweighted data if you add a "weight" variable that is 1 for every row
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Dataset 2: apistrat from the survey package}
The second dataset we will use is the apistrat data set from the survey package. This a stratified sample from a data set of all California schools with various characteristics of the schools such as type of school (stype), the Academic Performance index in 1999 and 2000 (api99, api00), if a school is year round (yr.rnd), percent of parents that are high school graduates (hsg)
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{library}\hlstd{(survey)}
\hlkwd{data}\hlstd{(api)}
\hlkwd{library}\hlstd{(dplyr)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 2A Crosstabs with base ggplot}
By default, ggplot does not adjust for varying sample sizes across graphics which doesn't always lead to good comparisons.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{ggplot}\hlstd{(apistrat,} \hlkwd{aes}\hlstd{(yr.rnd))} \hlopt{+}
\hlkwd{geom_bar}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{weight} \hlstd{= pw))} \hlopt{+}
\hlkwd{facet_grid}\hlstd{(}\hlkwc{cols} \hlstd{=} \hlkwd{vars}\hlstd{(stype))}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-6-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 2A Crosstabs with base ggplot}
First let's look at the base ggplot2 code needed to produce a crosstab.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{newdf} \hlkwb{=} \hlstd{apistrat} \hlopt{%>%}
\hlkwd{group_by}\hlstd{(stype, yr.rnd)} \hlopt{%>%}
\hlkwd{tally}\hlstd{(,} \hlkwc{wt} \hlstd{= pw)} \hlopt{%>%}
\hlkwd{mutate}\hlstd{(}\hlkwc{f} \hlstd{= n}\hlopt{/}\hlkwd{sum}\hlstd{(n))}
\hlstd{plotnew} \hlkwb{=} \hlkwd{ggplot}\hlstd{(newdf,} \hlkwd{aes}\hlstd{(yr.rnd))} \hlopt{+}
\hlkwd{geom_bar}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{weight} \hlstd{= f))} \hlopt{+}
\hlkwd{facet_grid}\hlstd{(}\hlkwc{cols} \hlstd{=} \hlkwd{vars}\hlstd{(stype))}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 2A Crosstabs with base ggplot}
First let's look at the base ggplot2 code needed to produce a crosstab.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plotnew}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-8-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 2B Crosstabs with ggsurvey}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{library}\hlstd{(ggsurvey)}
\hlkwd{ggbarcrosstabs}\hlstd{(apistrat, yr.rnd,}
\hlstd{stype, pw)}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-9-1}
\end{knitrout}
\end{frame}
\section{Using ggplot2}
\begin{frame}
To understand the what can be done with ggsurvey, we will first look at how base ggplot2 works for non-survey data.\\
In base ggplot2 a plot is made with at least two functions: ggplot and an appriorate geom function. There are some geoms that are commonly used in addition to other geoms such as adding text or error bars.
\end{frame}
\begin{frame}[fragile]{Main Plot Geoms}
\begin{table}[]
\begin{tabular}{|l|l|}
\hline
& Geom function \\ \hline
Bar chart & \begin{tabular}[c]{@{}l@{}}geom\_bar (for raw data), \\ geom\_col (for frequency tables)\end{tabular} \\ \hline
Boxplot & geom\_boxplot \\ \hline
Histogram & geom\_histogram \\ \hline
Line Plot & geom\_line \\ \hline
Scatterplot & geom\_point \\ \hline
Hex plot & geom\_hexbin (in hexbin package) \\ \hline
Violin plot & geom\_violin \\ \hline
\end{tabular}
\end{table}
\end{frame}
\begin{frame}[fragile]{Supplemental Plot Geoms}
\begin{table}[]
\begin{tabular}{|l|l|}
\hline
& Geom \\ \hline
Error bars & geom\_errorbars \\ \hline
Text & geom\_text \\ \hline
Smoothing lines & geom\_smooth \\ \hline
Line & geom\_abline \\ \hline
horizontal line & geom\_hline \\ \hline
vertical line & geom\_vline \\ \hline
\end{tabular}
\end{table}
\end{frame}
\begin{frame}[fragile]{Dataset 3: Palmer Penguins}
The Palmer penguins data set details information taken from penguins on the palmer archipelago. The variables are
species, island, bill\_length\_mm, bill\_depth\_mm, flipper\_depth\_mm, flipper\_length\_mm, body\_mass\_g, sex, year.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{library}\hlstd{(palmerpenguins)}
\hlkwd{data}\hlstd{(penguins)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 3: A Bar Chart}
First let's use the ggplot and geom\_bar functions to make a bar chart of the species of penguins in the data set.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot3} \hlkwb{=} \hlkwd{ggplot}\hlstd{(penguins,} \hlkwd{aes}\hlstd{(species))} \hlopt{+}
\hlkwd{geom_bar}\hlstd{()}
\hlstd{plot3}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-11-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Title customization for Example 3}
Next let's add a title to the previous plot with ggtitle.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot3} \hlopt{+} \hlkwd{ggtitle}\hlstd{(}\hlstr{"Species of Penguins"}\hlstd{)}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-12-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Label customization Part 1 for Example 3}
Next let's add a x axis label with xlab adds a label to the x axis.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot3} \hlopt{+} \hlkwd{ggtitle}\hlstd{(}\hlstr{"Species of Penguins"}\hlstd{)} \hlopt{+}
\hlkwd{xlab}\hlstd{(}\hlstr{"Species"}\hlstd{)}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-13-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Label customization Part 2 for Example 3}
Next let's add a y axis label to the previous plot with ylab.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot3title} \hlkwb{=} \hlstd{plot3} \hlopt{+} \hlkwd{ggtitle}\hlstd{(}\hlstr{"Species of Penguins"}\hlstd{)} \hlopt{+}
\hlkwd{xlab}\hlstd{(}\hlstr{"Species"}\hlstd{)} \hlopt{+} \hlkwd{ylab}\hlstd{(}\hlstr{"Number of Penguins"}\hlstd{)}
\hlstd{plot3title}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-14-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Label customization Part 2 for Example 3}
Next let's add a y axis label to the previous plot with ylab.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot3title}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-15-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Color customization}
We can make the bars each a different color by passing aes(fill = Species) inside geom\_bar
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot3color} \hlkwb{=} \hlkwd{ggplot}\hlstd{(penguins,} \hlkwd{aes}\hlstd{(species))} \hlopt{+}
\hlkwd{geom_bar}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{fill} \hlstd{= species))} \hlopt{+}
\hlkwd{ggtitle}\hlstd{(}\hlstr{"Species of Penguins"}\hlstd{)} \hlopt{+}
\hlkwd{xlab}\hlstd{(}\hlstr{"Species"}\hlstd{)} \hlopt{+} \hlkwd{ylab}\hlstd{(}\hlstr{"Number of Penguins"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Color customization}
We can make the bars each a different color by passing aes(fill = Species) inside geom\_bar
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot3color}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-17-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Faceting}
Faceting is a common tool to compare the the distribution of one set of variables by considering another variable. Going back to Example 1, we compared the relationship between horsepower and mpg depending on the transmission of the car using facet\_grid. Faceting helps to show the affect of a variable on multiple variables. Crosstabs are faceted graphs comparing the distribution of a survey question given an categorical variable.
\end{frame}
\begin{frame}[fragile]{Faceting}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plotnew} \hlkwb{=} \hlstd{plot} \hlopt{+} \hlkwd{ggtitle}\hlstd{(}\hlstr{"MPG of Car given HP by
transmission"}\hlstd{)} \hlopt{+}
\hlkwd{xlab}\hlstd{(}\hlstr{"Horse Power"}\hlstd{)} \hlopt{+} \hlkwd{ylab}\hlstd{(}\hlstr{"Miles Per Gallon"}\hlstd{)} \hlopt{+}
\hlkwd{facet_grid}\hlstd{(}\hlopt{~}\hlstd{am)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 4 Bar chart of Species with Facet Grid}
$y~x$ means x is on the horizontal axis
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot4} \hlkwb{=} \hlkwd{ggplot}\hlstd{(penguins,} \hlkwd{aes}\hlstd{(species))} \hlopt{+}
\hlkwd{geom_bar}\hlstd{()} \hlopt{+} \hlkwd{facet_grid}\hlstd{(year} \hlopt{~}
\hlstd{island)}
\hlstd{plot4}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-19-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 4 Bar chart of Species with Facet Grid}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot4}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-20-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 4 Bar chart of Species with Facet Grid}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot4a} \hlkwb{=} \hlkwd{ggplot}\hlstd{(penguins,} \hlkwd{aes}\hlstd{(species))} \hlopt{+}
\hlkwd{geom_bar}\hlstd{()} \hlopt{+} \hlkwd{facet_grid}\hlstd{(}\hlkwc{rows} \hlstd{=} \hlkwd{vars}\hlstd{(year),}
\hlkwc{cols} \hlstd{=} \hlkwd{vars}\hlstd{(island))}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 4 Bar chart of Species with Facet Grid }
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot4a}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-22-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 4 Bar chart text wrapping }
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{library}\hlstd{(ggrepel)}
\hlstd{plot4} \hlopt{+} \hlkwd{theme}\hlstd{(}\hlkwc{axis.text.x} \hlstd{=} \hlkwd{element_text}\hlstd{(}\hlkwc{angle} \hlstd{=} \hlnum{45}\hlstd{,}
\hlkwc{hjust} \hlstd{=} \hlnum{1}\hlstd{))}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-23-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Facet Wrap}
Facet Wrap wraps the facet for when there are too many columns/rows
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot4} \hlkwb{=} \hlkwd{ggplot}\hlstd{(penguins,} \hlkwd{aes}\hlstd{(species))} \hlopt{+}
\hlkwd{geom_bar}\hlstd{()} \hlopt{+} \hlkwd{facet_wrap}\hlstd{(}\hlkwd{vars}\hlstd{(year,}
\hlstd{island))}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Facet Wrap}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot4}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-25-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 5 Scatterplots}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot5} \hlkwb{=} \hlkwd{ggplot}\hlstd{(penguins,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= bill_length_mm,}
\hlkwc{y} \hlstd{= bill_depth_mm))} \hlopt{+} \hlkwd{geom_point}\hlstd{()}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 5 Scatterplots}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot5}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-27-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 5 with Species and Sex}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot5a} \hlkwb{=} \hlkwd{ggplot}\hlstd{(penguins,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= bill_length_mm,}
\hlkwc{y} \hlstd{= bill_depth_mm))} \hlopt{+} \hlkwd{geom_point}\hlstd{(}\hlkwd{aes}\hlstd{(}\hlkwc{color} \hlstd{= species,}
\hlkwc{shape} \hlstd{= sex))}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 5 with Species and Sex}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot5a}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-29-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 6A Histograms}
A histogram takes a continuous variable and puts it into bins and then counts the number of data points in each bin.By default geom\_histogram makes 30 evenly sized bins.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{ggplot}\hlstd{(penguins,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= body_mass_g))} \hlopt{+}
\hlkwd{geom_histogram}\hlstd{()}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-30-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 6B Histograms}
We can specify the size of the bins with binwidth. I prefer to try the default bins to get a sense of the range of the data.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot6b} \hlkwb{=} \hlkwd{ggplot}\hlstd{(penguins,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= body_mass_g))} \hlopt{+}
\hlkwd{geom_histogram}\hlstd{(}\hlkwc{binwidth} \hlstd{=} \hlnum{200}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 6B Histograms}
We can specify the size of the bins with binwidth. I prefer to try the default bins to get a sense of the range of the data.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot6b}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-32-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 7 Boxplot}
A boxplot shows the minimum, 25th percentile, median, 75th percentile, and maximum.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{ggplot}\hlstd{(penguins,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= flipper_length_mm))} \hlopt{+}
\hlkwd{geom_boxplot}\hlstd{()}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-33-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 7 Boxplot by Species}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot7} \hlkwb{=} \hlkwd{ggplot}\hlstd{(penguins,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= flipper_length_mm,}
\hlkwc{y} \hlstd{= species))} \hlopt{+} \hlkwd{geom_boxplot}\hlstd{()}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 7 Boxplot by Species}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot7}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-35-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 7 with Title}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot7new} \hlkwb{=} \hlstd{plot7} \hlopt{+} \hlkwd{ggtitle}\hlstd{(}\hlstr{"Boxplot of Flipper Length"}\hlstd{)} \hlopt{+}
\hlkwd{xlab}\hlstd{(}\hlstr{"Flipper Length"}\hlstd{)} \hlopt{+} \hlkwd{ylab}\hlstd{(}\hlstr{"Species"}\hlstd{)}
\hlstd{plot7new}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-36-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 7 with Title}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot7new}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-37-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 7 with Facet for Year}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot7new} \hlopt{+} \hlkwd{facet_grid}\hlstd{(}\hlopt{~}\hlstd{year)}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-38-1}
\end{knitrout}
\end{frame}
\begin{frame}{Any Questions?}
\end{frame}
\section{Using ggsurvey for bar charts}
\begin{frame}{Introduction to ggsurvey vs ggplot}
\begin{itemize}
\item ggsurvey combines the ggplot+geom+facet\_grid into one function to save time
\item For each plot type ggsurvey specifies how to do it without a facet, with one facet, and with two facets
\item You can add ggtitle, xlab, ylab just like regular ggplot2
\item ggsurvey is compatible with dataframe and survey design objects (including srvyr objects for select functions)
\item like in ggplot we don't put column names in quotes
\item For data frames the survey weight should a column in the data frame
\end{itemize}
\end{frame}
\begin{frame}{Bar Chart Functions}
\begin{table}[]
\begin{tabular}{|l|l|l|}
\hline
Plot type & Data type & ggsurvey function \\ \hline
Bar Chart & data frame & ggbarweight(df, x, weights, fill) \\ \hline
Bar Chart & svydesign & ggbarweight\_svy(surveyobj, x, fill) \\ \hline
Crosstab & data frame & ggcrosstab(df, x, y,weights, fill, labeller) \\ \hline
Crosstab & svydesign & ggcrosstab\_svy(surveyobj, x, y, fill, labeller) \\ \hline
\begin{tabular}[c]{@{}l@{}}Crosstab\\ with 2\\ variables\end{tabular} & data frame & ggcrosstab3d(df, x, y,z weights, fill, labeller) \\ \hline
\begin{tabular}[c]{@{}l@{}}Crosstab\\ with 2\\ variables\end{tabular} & svydesign & ggcrosstab3d\_svy(surveyobj, x,y,z, fill. labeller) \\ \hline
\end{tabular}
\end{table}
\end{frame}
\begin{frame}{Dataset 4: ANES}
The American National Election Study is a large multi-mode multi-wave national survey conducted every two years with a broad range of questions related both to elections and broader policy issues. It is a collaboration between Stanford University and the University of Michigan and is funded by the National Science Foundation. It has been conducted since 1948. The population for the survey is US citizens. \\
\href{https://electionstudies.org/}{ANES information}
The anesr package accesses ANES data, and we will use the 2020 time series data which has 8280 respondents.
\end{frame}
\begin{frame}[fragile]{Install and Load ANES}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{# install.packages('devtools')}
\hlcom{# library(devtools)}
\hlcom{# install_github('jamesmartherus/anesr')}
\hlkwd{library}\hlstd{(anesr)}
\hlkwd{data}\hlstd{(}\hlstr{"timeseries_2020"}\hlstd{)}
\hlstd{timeseries_2020} \hlkwb{=} \hlstd{haven}\hlopt{::}\hlkwd{as_factor}\hlstd{(timeseries_2020)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}{Variables in ANES}
We will look at the following questions in the pre-election phase: V201114 (Right Track / Wrong Track), V201390 (Speed of Response of Federal Government to COVID-19), V201324 (State of Economy), V201151 feeling thermometer for Joe Biden, V201152 feeling thermometer for Trump. \\
We will also consider crosstabs on the following variables V201549x (race), V201511x (education), V201600 (gender). \\
The relevant weight is V200010a,strata V200010d, cluster is V200010c.
\end{frame}
\begin{frame}[fragile]{Rename variables and Drop Empty Levels}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{timeseries_2020}\hlopt{$}\hlstd{rightwrongtrack} \hlkwb{=} \hlstd{timeseries_2020}\hlopt{$}\hlstd{V201114}
\hlstd{timeseries_2020}\hlopt{$}\hlstd{speedcovid} \hlkwb{=} \hlstd{timeseries_2020}\hlopt{$}\hlstd{V201390}
\hlstd{timeseries_2020}\hlopt{$}\hlstd{economy} \hlkwb{=} \hlstd{timeseries_2020}\hlopt{$}\hlstd{V201324}
\hlstd{timeseries_2020}\hlopt{$}\hlstd{race} \hlkwb{=} \hlstd{timeseries_2020}\hlopt{$}\hlstd{V201549x}
\hlstd{timeseries_2020}\hlopt{$}\hlstd{gender} \hlkwb{=} \hlstd{timeseries_2020}\hlopt{$}\hlstd{V201600}
\hlstd{timeseries_2020} \hlkwb{=} \hlkwd{droplevels}\hlstd{(timeseries_2020)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Removing Refusals on Demographics}
To make the plots look a little cleaner we will remove individuals who refused or said didn't know about their race, education, or gender, or refused one of our questions of interest.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{timeseries_2020} \hlkwb{=} \hlstd{timeseries_2020} \hlopt{%>%}
\hlkwd{filter}\hlstd{(race} \hlopt{!=} \hlstr{"-9. Refused"}\hlstd{,}
\hlstd{race} \hlopt{!=} \hlstr{"-8. Don't know"}\hlstd{,}
\hlstd{gender} \hlopt{!=} \hlstr{"-9. Refused"}\hlstd{,}
\hlstd{rightwrongtrack} \hlopt{!=} \hlstr{"-9. Refused"}\hlstd{,}
\hlstd{rightwrongtrack} \hlopt{!=} \hlstr{"-8. Don't know"}\hlstd{,}
\hlstd{economy} \hlopt{!=} \hlstr{"-9. Refused"}\hlstd{,}
\hlstd{speedcovid} \hlopt{!=} \hlstr{"-9. Refused"}\hlstd{)}
\hlstd{timeseries_2020} \hlkwb{=} \hlkwd{droplevels}\hlstd{(timeseries_2020)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Creating a Survey Design Object For ANES}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{library}\hlstd{(srvyr)}
\hlstd{anes_survey} \hlkwb{=} \hlkwd{svydesign}\hlstd{(}\hlkwc{id} \hlstd{=} \hlopt{~}\hlstd{V200001,}
\hlkwc{strata} \hlstd{=} \hlopt{~}\hlstd{V200010d,} \hlkwc{weights} \hlstd{=} \hlopt{~}\hlstd{V200010a,}
\hlkwc{data} \hlstd{= timeseries_2020)}
\hlstd{anes_svy} \hlkwb{=} \hlkwd{as_survey}\hlstd{(anes_survey)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 8A: ggsurvey Bar chart with Data Frame}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot8a} \hlkwb{=} \hlkwd{ggbarweight}\hlstd{(timeseries_2020,}
\hlstd{rightwrongtrack, V200010a)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 8A: ggsurvey Bar chart with Data Frame}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot8a}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-44-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 8A: Adding titles to previous plot}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot8atitle} \hlkwb{=} \hlstd{plot8a} \hlopt{+} \hlkwd{ggtitle}\hlstd{(}\hlstr{"Right Direction / Wrong Track"}\hlstd{)} \hlopt{+}
\hlkwd{ylab}\hlstd{(}\hlstr{"Proportion"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 8A: Adding titles to previous plot}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot8atitle}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-46-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 8B: ggsurvey Bar Chart with Survey Object }
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot8b} \hlkwb{=} \hlkwd{ggbarweight_svy}\hlstd{(anes_survey,}
\hlstd{rightwrongtrack)}
\hlstd{plot8b}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-47-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 8B: Adding titles to previous plot}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot8title} \hlkwb{=} \hlstd{plot8b} \hlopt{+} \hlkwd{ggtitle}\hlstd{(}\hlstr{"Right Direction / Wrong Track"}\hlstd{)} \hlopt{+}
\hlkwd{xlab}\hlstd{(}\hlstr{"Proportion"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 8B: Adding titles to previous plot}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{plot8title}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-49-1}
\end{knitrout}
\end{frame}
\begin{frame}{Filtering: Advanced}
Sometimes we don't want to include a certain category in the plot. For instance we may want to remove the refusals from the graph. We can use the filter function from dplyr on a data frame to remove the refusals from the plot, and then pipe operator. The pipe operator passes the object on the left of the pipe into the function on the right as the first argument. At this time this only works with data frames with survey weights.
\end{frame}
\begin{frame}[fragile]{Removing Refusals and Don't Know}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{filterplot} \hlkwb{=} \hlstd{timeseries_2020} \hlopt{%>%}
\hlkwd{filter}\hlstd{(race} \hlopt{==} \hlstr{"1. White, non-Hispanic"}\hlstd{)} \hlopt{%>%}
\hlkwd{ggbarweight}\hlstd{(rightwrongtrack,}
\hlstd{V200010a)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Removing Refusals and Don't Know}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{filterplot}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-51-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 9A: Bar Chart with One Crosstab For Dataframe}
Sometimes the text overlaps in ggplots.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{ggbarcrosstabs}\hlstd{(timeseries_2020,}
\hlstd{rightwrongtrack, gender, V200010a)}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-52-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 9A: Text Wrap Labels}
We can use the stringr package to use text wrapping for the labels on the bars.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{library}\hlstd{(stringr)}
\hlstd{plot9Atext} \hlkwb{=} \hlkwd{ggbarcrosstabs}\hlstd{(timeseries_2020,}
\hlstd{rightwrongtrack, gender, V200010a)} \hlopt{+}
\hlkwd{scale_x_discrete}\hlstd{(}\hlkwc{labels} \hlstd{=} \hlkwa{function}\hlstd{(}\hlkwc{x}\hlstd{)} \hlkwd{str_wrap}\hlstd{(x,}
\hlkwc{width} \hlstd{=} \hlnum{10}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 9A: Text Wrap Labels}
We can use the stringr package to use text wrapping for the labels on the bars.
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{library}\hlstd{(stringr)}
\hlstd{plot9Atext}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-54-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 9B: Bar Chart with One Crosstab For svydesign}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{ggbarcrosstabs_svy}\hlstd{(anes_survey,}
\hlstd{rightwrongtrack, gender)}
\end{alltt}
\end{kframe}
\includegraphics[width=0.95\linewidth]{figure/unnamed-chunk-55-1}
\end{knitrout}
\end{frame}
\begin{frame}[fragile]{Example 9B: Text Wrap Labels}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{ggbarcrosstabs_svy}\hlstd{(anes_survey,}
\hlstd{rightwrongtrack, gender)} \hlopt{+}
\hlkwd{scale_x_discrete}\hlstd{(}\hlkwc{labels} \hlstd{=} \hlkwa{function}\hlstd{(}\hlkwc{x}\hlstd{)} \hlkwd{str_wrap}\hlstd{(x,}
\hlkwc{width} \hlstd{=} \hlnum{10}\hlstd{))}