\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\usepackage[margin=1in]{geometry}
\usepackage{hyperref}
\hypersetup{unicode=true,
            pdftitle={Visualizing Change in Ordinal Meausures: Religious Attendance in the United States (1972-2018)},
            pdfauthor={Michael Lee Wood},
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

%%% Use protect on footnotes to avoid problems with footnotes in titles
\let\rmarkdownfootnote\footnote%
\def\footnote{\protect\rmarkdownfootnote}

%%% Change title format to be more compact
\usepackage{titling}

% Create subtitle command for use in maketitle
\providecommand{\subtitle}[1]{
  \posttitle{
    \begin{center}\large#1\end{center}
    }
}

\setlength{\droptitle}{-2em}

  \title{Visualizing Change in Ordinal Meausures: Religious Attendance in the
United States (1972-2018)}
    \pretitle{\vspace{\droptitle}\centering\huge}
  \posttitle{\par}
    \author{Michael Lee Wood}
    \preauthor{\centering\large\emph}
  \postauthor{\par}
      \predate{\centering\large\emph}
  \postdate{\par}
    \date{11 December 2019}


\begin{document}
\maketitle

This file contains instructions and code to prepare the gss data and
create the plot.

\hypertarget{preliminary-steps-and-cleaning}{%
\section{Preliminary Steps and
Cleaning}\label{preliminary-steps-and-cleaning}}

If you haven't already, first download the GSS data from the NORC
website. An extract is available here:
\url{https://gssdataexplorer.norc.org/projects/62946/extracts/45347?token=UxRsTrX6sjsAaDwx9oxf}.
Alternatively, you can download all code and necessary files from my
GitHub page: \url{https://github.com/michaellwood/relig_attend_viz}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{setwd}\NormalTok{(}\StringTok{"working_directory_here"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Load the following packages:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{pacman}\OperatorTok{::}\KeywordTok{p_load}\NormalTok{(ggplot2, tidyr, readxl, dplyr, reshape2)}
\end{Highlighting}
\end{Shaded}

Read in the ``GSS.xls'' file and store it as a dataframe. (Note: this
has all years, but only a few variables). There will be a couple
warnings, which you can ignore.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18 <-}\StringTok{ }\NormalTok{readxl}\OperatorTok{::}\KeywordTok{read_xls}\NormalTok{(}\StringTok{"GSS.xls"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Rename columns and drop columns we don't need. I also create a subset
with the gender variable for an extra example at the end.

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{names}\NormalTok{(gss18)[}\DecValTok{1}\NormalTok{] <-}\StringTok{ "year"}
\KeywordTok{names}\NormalTok{(gss18)[}\DecValTok{3}\NormalTok{] <-}\StringTok{ "attend"}
\KeywordTok{names}\NormalTok{(gss18)[}\DecValTok{5}\NormalTok{] <-}\StringTok{ "gender"}
\NormalTok{gss18gender <-}\StringTok{ }\NormalTok{gss18[,}\KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{5}\NormalTok{)]}
\NormalTok{gss18 <-}\StringTok{ }\NormalTok{gss18[,}\KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{3}\NormalTok{)]}
\end{Highlighting}
\end{Shaded}

Convert the attendance variable to a factor (it is currently a string)

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18}\OperatorTok{$}\NormalTok{attend   <-}\StringTok{ }\KeywordTok{as.factor}\NormalTok{(gss18}\OperatorTok{$}\NormalTok{attend)}
\end{Highlighting}
\end{Shaded}

Remove ``DK'' and ``NA'' responses from the attendance variable

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18<-gss18[}\OperatorTok{!}\NormalTok{(gss18}\OperatorTok{$}\NormalTok{attend}\OperatorTok{==}\StringTok{'Dk,na'}\NormalTok{),]}
\NormalTok{gss18}\OperatorTok{$}\NormalTok{attend <-}\StringTok{ }\KeywordTok{droplevels}\NormalTok{(gss18}\OperatorTok{$}\NormalTok{attend)}
\end{Highlighting}
\end{Shaded}

Remove two rows with NA in the year

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18<-gss18[}\OperatorTok{!}\NormalTok{(}\KeywordTok{is.na}\NormalTok{(gss18}\OperatorTok{$}\NormalTok{year)}\OperatorTok{==}\OtherTok{TRUE}\NormalTok{),] }
\end{Highlighting}
\end{Shaded}

Check the factor levels for the attendance variable

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{levels}\NormalTok{(gss18}\OperatorTok{$}\NormalTok{attend)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] "2-3x a month"     "Every week"       "Lt once a year"  
## [4] "More thn once wk" "Never"            "Nrly every week" 
## [7] "Once a month"     "Once a year"      "Sevrl times a yr"
\end{verbatim}

We need to reorder the levels of the attendance variable so they appear
in the correct order in the plot. As you can see, they are currently
arranged alphabetically!

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18}\OperatorTok{$}\NormalTok{attend <-}\StringTok{ }\KeywordTok{factor}\NormalTok{(gss18}\OperatorTok{$}\NormalTok{attend,}\KeywordTok{levels}\NormalTok{(gss18}\OperatorTok{$}\NormalTok{attend)[}\KeywordTok{c}\NormalTok{(}\DecValTok{5}\NormalTok{,}\DecValTok{3}\NormalTok{,}\DecValTok{8}\NormalTok{,}\DecValTok{9}\NormalTok{,}\DecValTok{7}\NormalTok{,}\DecValTok{1}\NormalTok{,}\DecValTok{6}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{4}\NormalTok{)])}
\end{Highlighting}
\end{Shaded}

Check the new level order

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{levels}\NormalTok{(gss18}\OperatorTok{$}\NormalTok{attend)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] "Never"            "Lt once a year"   "Once a year"     
## [4] "Sevrl times a yr" "Once a month"     "2-3x a month"    
## [7] "Nrly every week"  "Every week"       "More thn once wk"
\end{verbatim}

\hypertarget{preparing-for-the-plot}{%
\section{Preparing for the Plot}\label{preparing-for-the-plot}}

To create the visualization we need a dataframe that gives the
proportion of responses for each repsonse category by year. This is done
in two steps. First, we reshape the data from wide to long and tally up
the responses. Then we go back to wide format, giving each response
category its own column with a sum.

Inspect our current dataframe

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{head}\NormalTok{(gss18)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## # A tibble: 6 x 2
##    year attend      
##   <dbl> <fct>       
## 1  1972 Once a year 
## 2  1972 Every week  
## 3  1972 Once a month
## 4  1972 Never       
## 5  1972 Never       
## 6  1972 Once a year
\end{verbatim}

Create an ID variable preparatory to reshaping the data. We didn't use
the GSS ID variable because those numbers often repeat, and we need
every case to have a unique ID.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18}\OperatorTok{$}\NormalTok{id <-}\StringTok{ }\KeywordTok{rownames}\NormalTok{(gss18)}
\end{Highlighting}
\end{Shaded}

Reshape from wide to long and tally. The new dataframe will show how
many cases there are in each attendance response category by year.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18_l <-}\StringTok{ }\NormalTok{gss18 }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{group_by}\NormalTok{(year, attend) }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{tally}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

Inspect the new dataframe

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{head}\NormalTok{(gss18_l)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## # A tibble: 6 x 3
## # Groups:   year [1]
##    year attend               n
##   <dbl> <fct>            <int>
## 1  1972 Never              150
## 2  1972 Lt once a year     137
## 3  1972 Once a year        178
## 4  1972 Sevrl times a yr   226
## 5  1972 Once a month       108
## 6  1972 2-3x a month       142
\end{verbatim}

Reshape the dataframe again, creating a column for each factor level of
the attend variable

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18_attend <-}\StringTok{ }\KeywordTok{dcast}\NormalTok{(gss18_l,year}\OperatorTok{~}\NormalTok{attend)}
\end{Highlighting}
\end{Shaded}

Inspect our new dataframe

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{head}\NormalTok{(gss18_attend)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   year Never Lt once a year Once a year Sevrl times a yr Once a month
## 1 1972   150            137         178              226          108
## 2 1973   204            117         194              226           93
## 3 1974   181            103         226              188          118
## 4 1975   216            108         184              209          102
## 5 1976   194            136         203              232          106
## 6 1977   212            127         204              189          113
##   2-3x a month Nrly every week Every week More thn once wk
## 1          142              98        460              101
## 2          125             116        308              112
## 3          129              86        333              117
## 4          133              99        334              102
## 5          100              90        298              133
## 6          138              87        328              123
\end{verbatim}

We now have our data in the right general format, but we need to make a
few other changes before creating the plot. First, we need to convert
our raw counts to percentages. We begin by getting the total number of
cases for each year.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18_attend}\OperatorTok{$}\NormalTok{tot <-}\StringTok{ }\KeywordTok{rowSums}\NormalTok{(gss18_attend[,}\KeywordTok{c}\NormalTok{(}\DecValTok{2}\OperatorTok{:}\DecValTok{10}\NormalTok{)])}
\end{Highlighting}
\end{Shaded}

Now divide each cell by the total, and multiply by 100

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18_attend[,}\OperatorTok{-}\DecValTok{1}\NormalTok{] <-}\StringTok{ }\KeywordTok{lapply}\NormalTok{(gss18_attend[,}\OperatorTok{-}\DecValTok{1}\NormalTok{], }\ControlFlowTok{function}\NormalTok{(x)\{(x}\OperatorTok{/}\NormalTok{gss18_attend}\OperatorTok{$}\NormalTok{tot)}\OperatorTok{*}\DecValTok{100}\NormalTok{\})}
\end{Highlighting}
\end{Shaded}

We don't need the ``total'' column anymore, so we can drop it

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18_attend <-}\StringTok{ }\NormalTok{gss18_attend[,}\OperatorTok{-}\DecValTok{11}\NormalTok{]}
\end{Highlighting}
\end{Shaded}

Reinspect the dataframe (always a good practice!)

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{head}\NormalTok{(gss18_attend)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   year    Never Lt once a year Once a year Sevrl times a yr Once a month
## 1 1972  9.37500       8.562500    11.12500         14.12500     6.750000
## 2 1973 13.64548       7.826087    12.97659         15.11706     6.220736
## 3 1974 12.22147       6.954760    15.25996         12.69413     7.967589
## 4 1975 14.52589       7.262946    12.37391         14.05514     6.859449
## 5 1976 13.00268       9.115282    13.60590         15.54960     7.104558
## 6 1977 13.93820       8.349770    13.41223         12.42604     7.429323
##   2-3x a month Nrly every week Every week More thn once wk
## 1     8.875000        6.125000   28.75000         6.312500
## 2     8.361204        7.759197   20.60201         7.491639
## 3     8.710331        5.806887   22.48481         7.900068
## 4     8.944183        6.657700   22.46133         6.859449
## 5     6.702413        6.032172   19.97319         8.914209
## 6     9.072978        5.719921   21.56476         8.086785
\end{verbatim}

Next, we need to split our data into ``high'' and ``low'' categories,
but we can't do that without first splitting our middle category, which
here is ``Once a month.'' When it comes to interpretation, this means
that ``Once a month'' is a neutral category, because for each year, half
of ``Once a month'' will appear on the ``low'' side of the plot, and
half will appear on the ``high'' side.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18_attend}\OperatorTok{$}\StringTok{`}\DataTypeTok{OnceMonthLo}\StringTok{`}\NormalTok{ <-}\StringTok{ }\NormalTok{gss18_attend}\OperatorTok{$}\StringTok{`}\DataTypeTok{Once a month}\StringTok{`}\OperatorTok{/}\DecValTok{2}
\NormalTok{gss18_attend}\OperatorTok{$}\StringTok{`}\DataTypeTok{OnceMonthHi}\StringTok{`}\NormalTok{ <-}\StringTok{ }\NormalTok{gss18_attend}\OperatorTok{$}\StringTok{`}\DataTypeTok{OnceMonthLo}\StringTok{`}
\NormalTok{gss18_attend <-}\StringTok{ }\NormalTok{gss18_attend[,}\OperatorTok{-}\DecValTok{6}\NormalTok{]}
\NormalTok{gss18_attend <-}\StringTok{ }\NormalTok{gss18_attend[,}\KeywordTok{c}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{,}\DecValTok{10}\OperatorTok{:}\DecValTok{11}\NormalTok{,}\DecValTok{6}\OperatorTok{:}\DecValTok{9}\NormalTok{)]}
\end{Highlighting}
\end{Shaded}

Reinspect the dataframe

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{head}\NormalTok{(gss18_attend)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   year    Never Lt once a year Once a year Sevrl times a yr OnceMonthLo
## 1 1972  9.37500       8.562500    11.12500         14.12500    3.375000
## 2 1973 13.64548       7.826087    12.97659         15.11706    3.110368
## 3 1974 12.22147       6.954760    15.25996         12.69413    3.983795
## 4 1975 14.52589       7.262946    12.37391         14.05514    3.429724
## 5 1976 13.00268       9.115282    13.60590         15.54960    3.552279
## 6 1977 13.93820       8.349770    13.41223         12.42604    3.714661
##   OnceMonthHi 2-3x a month Nrly every week Every week More thn once wk
## 1    3.375000     8.875000        6.125000   28.75000         6.312500
## 2    3.110368     8.361204        7.759197   20.60201         7.491639
## 3    3.983795     8.710331        5.806887   22.48481         7.900068
## 4    3.429724     8.944183        6.657700   22.46133         6.859449
## 5    3.552279     6.702413        6.032172   19.97319         8.914209
## 6    3.714661     9.072978        5.719921   21.56476         8.086785
\end{verbatim}

Next we need to get our colors ready. This is a bit of work because we
need three palettes: one for the ``high'' bars, one for the ``low''
bars, and one for the legend. I chose a color palette from the Urban
Institute's Data Visualization Style Guide
\url{https://urbaninstitute.github.io/graphics-styleguide/} First we
create an object that stores the colors for the low bars

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{pal_lo <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"#0a4c6a"}\NormalTok{,}\StringTok{"#1696d2"}\NormalTok{,}\StringTok{"#73bfe2"}\NormalTok{,}\StringTok{"#cfe8f3"}\NormalTok{,}\StringTok{"#f5f5f5"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Now the high bars

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{pal_hi <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"#f5f5f5"}\NormalTok{,}\StringTok{"#fff2cf"}\NormalTok{, }\StringTok{"#fdd870"}\NormalTok{, }\StringTok{"#fdaa11"}\NormalTok{, }\StringTok{"#ca5800"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Create the palette for the legend

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{legend.pal <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"#ca5800"}\NormalTok{,}\StringTok{"#fdaa11"}\NormalTok{,}\StringTok{"#fdd870"}\NormalTok{,}\StringTok{"#fff2cf"}\NormalTok{,}
                \StringTok{"#f5f5f5"}\NormalTok{,}\StringTok{"#cfe8f3"}\NormalTok{,}\StringTok{"#73bfe2"}\NormalTok{,}\StringTok{"#1696d2"}\NormalTok{, }\StringTok{"#0a4c6a"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Now we need to create two separate dataframes so we can plot the high
and low bars separately. First, we'll create a dataframe that goes from
``never'' to ``once a month-lo''

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss_att_lo <-}\StringTok{ }\NormalTok{gss18_attend[,}\DecValTok{1}\OperatorTok{:}\DecValTok{6}\NormalTok{]}
\end{Highlighting}
\end{Shaded}

Next we'll reshape wide to long

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss_att_lo <-}\StringTok{ }\KeywordTok{melt}\NormalTok{(gss_att_lo,}\DataTypeTok{id=}\StringTok{"year"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Make the values negative, so that they get plotted on the left side of
the graph

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss_att_lo}\OperatorTok{$}\NormalTok{value <-}\StringTok{ }\OperatorTok{-}\NormalTok{gss_att_lo}\OperatorTok{$}\NormalTok{value}
\end{Highlighting}
\end{Shaded}

Now we'll use that vector of colors to assign a color to each category.
We do this by creating a separating color column

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss_att_lo}\OperatorTok{$}\NormalTok{col <-}\StringTok{ }\KeywordTok{rep}\NormalTok{(pal_lo,}\DataTypeTok{each=}\NormalTok{(}\DecValTok{160}\OperatorTok{/}\DecValTok{5}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

Change these to color values to factors and make sure they are in the
right order

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss_att_lo}\OperatorTok{$}\NormalTok{col <-}\StringTok{ }\KeywordTok{factor}\NormalTok{(gss_att_lo}\OperatorTok{$}\NormalTok{col, }
                         \DataTypeTok{levels =} \KeywordTok{c}\NormalTok{(}\StringTok{"#0a4c6a"}\NormalTok{,}\StringTok{"#1696d2"}\NormalTok{,}\StringTok{"#73bfe2"}\NormalTok{,}\StringTok{"#cfe8f3"}\NormalTok{,}\StringTok{"#f5f5f5"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

Inspect the dataframe

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{head}\NormalTok{(gss_att_lo)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   year variable     value     col
## 1 1972    Never  -9.37500 #0a4c6a
## 2 1973    Never -13.64548 #0a4c6a
## 3 1974    Never -12.22147 #0a4c6a
## 4 1975    Never -14.52589 #0a4c6a
## 5 1976    Never -13.00268 #0a4c6a
## 6 1977    Never -13.93820 #0a4c6a
\end{verbatim}

Now we do that again, but for the ``high'' plot

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss_att_hi <-}\StringTok{ }\NormalTok{gss18_attend[,}\KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{7}\OperatorTok{:}\DecValTok{11}\NormalTok{)]}
\NormalTok{gss_att_hi <-}\StringTok{ }\KeywordTok{melt}\NormalTok{(gss_att_hi,}\DataTypeTok{id=}\StringTok{"year"}\NormalTok{)}
\CommentTok{#We need to reverse the levels here so they are plotted in the right order}
\NormalTok{gss_att_hi}\OperatorTok{$}\NormalTok{variable <-}\StringTok{ }\KeywordTok{factor}\NormalTok{(gss_att_hi}\OperatorTok{$}\NormalTok{variable,}
                              \KeywordTok{levels}\NormalTok{(gss_att_hi}\OperatorTok{$}\NormalTok{variable)[}\KeywordTok{c}\NormalTok{(}\DecValTok{5}\NormalTok{,}\DecValTok{4}\NormalTok{,}\DecValTok{3}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{1}\NormalTok{)])}
\NormalTok{gss_att_hi}\OperatorTok{$}\NormalTok{col <-}\StringTok{ }\KeywordTok{rep}\NormalTok{(pal_hi,}\DataTypeTok{each=}\NormalTok{(}\DecValTok{160}\OperatorTok{/}\DecValTok{5}\NormalTok{))}
\NormalTok{gss_att_hi}\OperatorTok{$}\NormalTok{col <-}\StringTok{ }\KeywordTok{factor}\NormalTok{(gss_att_hi}\OperatorTok{$}\NormalTok{col,}
                         \DataTypeTok{levels =} \KeywordTok{c}\NormalTok{(}\StringTok{"#ca5800"}\NormalTok{,}\StringTok{"#fdaa11"}\NormalTok{,}\StringTok{"#fdd870"}\NormalTok{,}\StringTok{"#fff2cf"}\NormalTok{,}\StringTok{"#f5f5f5"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

Our data and colors are ready. Now we need to finish preparing the
legend by specifying all the labels

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{mylevels <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"More than once a week"}\NormalTok{,}\StringTok{"Every week"}\NormalTok{,}\StringTok{"Nearly every week"}\NormalTok{,}
              \StringTok{"2-3x a month"}\NormalTok{,}\StringTok{"Once a month"}\NormalTok{,}\StringTok{"Several times a year"}\NormalTok{,}
              \StringTok{"Once a year"}\NormalTok{,}\StringTok{"Less than once a year"}\NormalTok{,}\StringTok{"Never"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

If we create the plot as is, the values on the left will appear as
negative numbers ranging from 0\% to -50\%. However, we can create the
following function which will allow us to make both sides of the axis
range from 0\% to 50\%. We will call this function when we create the
actual plot.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{abs_percent <-}\StringTok{ }\ControlFlowTok{function}\NormalTok{(x,}\DataTypeTok{digits=}\DecValTok{2}\NormalTok{)}
\NormalTok{\{}\KeywordTok{paste0}\NormalTok{(}\KeywordTok{round}\NormalTok{(}\KeywordTok{abs}\NormalTok{(x),digits), }\StringTok{"%"}\NormalTok{)\}}
\end{Highlighting}
\end{Shaded}

\hypertarget{creating-the-plot}{%
\section{Creating the Plot}\label{creating-the-plot}}

We're finally ready to create the plot. Here are the steps:

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
  Create two geom\_bars using the ``high'' and ``low'' dataframes. I
  made the width slightly below one so the bars are wide, but don't
  touch
\item
  Add a line at 0. Technically this is a horizontal line, but we are
  going to flip the x and y axis, so it will appear vertical.
\item
  Add the legend, specifying our custom text and color palette.
\item
  Change the theme to ``classic''
\item
  Customize the x axis so it shows each year
\item
  Specify the break points for the y axis
\item
  Add lines that mark 50\% on the y axis. This tells us whether a year
  has more people on the high or low end
\item
  Flip the x and y axes
\item
  Add title, caption, and axis labels
\item
  Remove the legend title, adjust a label position, and specify text
  sizes
\end{enumerate}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p <-}\StringTok{ }
\StringTok{  }\KeywordTok{ggplot}\NormalTok{() }\OperatorTok{+}\StringTok{  }
\StringTok{  }\KeywordTok{geom_bar}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ gss_att_lo, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x=}\NormalTok{year, }\DataTypeTok{y=}\NormalTok{value,}\DataTypeTok{fill=}\NormalTok{col),}
           \DataTypeTok{position=}\StringTok{"stack"}\NormalTok{, }\DataTypeTok{stat=}\StringTok{"identity"}\NormalTok{,}\DataTypeTok{width=}\NormalTok{.}\DecValTok{93}\NormalTok{)}\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{geom_bar}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ gss_att_hi, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x=}\NormalTok{year, }\DataTypeTok{y=}\NormalTok{value,}\DataTypeTok{fill=}\NormalTok{col),}
           \DataTypeTok{position=}\StringTok{"stack"}\NormalTok{, }\DataTypeTok{stat=}\StringTok{"identity"}\NormalTok{,}\DataTypeTok{width=}\NormalTok{.}\DecValTok{93}\NormalTok{)}\OperatorTok{+}
\StringTok{  }\KeywordTok{geom_hline}\NormalTok{(}\DataTypeTok{yintercept =} \DecValTok{0}\NormalTok{, }\DataTypeTok{color =}\KeywordTok{c}\NormalTok{(}\StringTok{"white"}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{scale_fill_identity}\NormalTok{(}\DataTypeTok{labels =}\NormalTok{ mylevels, }\DataTypeTok{breaks=}\NormalTok{legend.pal, }\DataTypeTok{guide=}\StringTok{"legend"}\NormalTok{)}\OperatorTok{+}
\StringTok{  }\KeywordTok{theme_classic}\NormalTok{()}\OperatorTok{+}
\StringTok{  }\KeywordTok{scale_x_continuous}\NormalTok{(}\DataTypeTok{breaks=}\KeywordTok{c}\NormalTok{(}\DecValTok{1972}\OperatorTok{:}\DecValTok{1978}\NormalTok{,}\DecValTok{1980}\NormalTok{,}\DecValTok{1982}\OperatorTok{:}\DecValTok{1991}\NormalTok{,}\DecValTok{1993}\NormalTok{,}\KeywordTok{seq}\NormalTok{(}\DecValTok{1994}\NormalTok{,}\DecValTok{2018}\NormalTok{,}\DecValTok{2}\NormalTok{)))}\OperatorTok{+}
\StringTok{  }\KeywordTok{scale_y_continuous}\NormalTok{(}\DataTypeTok{breaks=}\KeywordTok{seq}\NormalTok{(}\OperatorTok{-}\DecValTok{50}\NormalTok{,}\DecValTok{50}\NormalTok{,}\DecValTok{10}\NormalTok{),}
                     \DataTypeTok{labels=}\NormalTok{abs_percent)}\OperatorTok{+}
\StringTok{  }\KeywordTok{geom_hline}\NormalTok{(}\DataTypeTok{yintercept=}\KeywordTok{c}\NormalTok{(}\OperatorTok{-}\DecValTok{50}\NormalTok{,}\DecValTok{50}\NormalTok{),}\DataTypeTok{linetype=}\StringTok{"dashed"}\NormalTok{,}\DataTypeTok{color=}\StringTok{"grey"}\NormalTok{)}\OperatorTok{+}
\StringTok{  }\KeywordTok{coord_flip}\NormalTok{()}\OperatorTok{+}
\StringTok{  }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{title =} \StringTok{"How often do you attend religious services?"}\NormalTok{,}
       \DataTypeTok{caption =} \StringTok{"General Social Survey 1972-2018"}\NormalTok{,}
       \DataTypeTok{y =} \StringTok{""}\NormalTok{,}
       \DataTypeTok{x =} \StringTok{"Year"}\NormalTok{)}\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.title =} \KeywordTok{element_blank}\NormalTok{())}\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.title.x =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{hjust=}\NormalTok{.}\DecValTok{54}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{plot.title=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{20}\NormalTok{),}
        \DataTypeTok{plot.caption=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{10}\NormalTok{),}
        \DataTypeTok{axis.title.y=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{15}\NormalTok{),}
        \DataTypeTok{axis.text.y=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{10}\NormalTok{),}
        \DataTypeTok{axis.text.x=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{10}\NormalTok{),}
        \DataTypeTok{legend.text=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{10}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

That's it!

\includegraphics{Visualizing-Religious-Change-over-Time-20191211_files/figure-latex/Religious Attendance 1972-2018 20191211.jpg}

\hypertarget{adding-annotations-with-the-category-values.}{%
\section{Adding annotations with the category
values.}\label{adding-annotations-with-the-category-values.}}

Though not featured in the plot above, we can also add annotations that
show the values for each of the categories. This can make comparing
values a bit easier, but it can also make the plot more cluttered if
there are categories with very small values. I explain how to do this
here for your reference.

To annotate the plot with the values for each category, we need to
calculate WHERE each label should appear. To get these labels near the
center of each category, add up the values of the preceding categories
and then add 1/2 of the value of the category we are working on. These
will be the ``y'' locations of our annotations We don't need to do this
for the middle category, because we already know that the text should be
at 0. The following code makes these calculations and stores them in a
new dataframe.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{labeldf <-}\StringTok{ }\NormalTok{gss18_attend}
\NormalTok{labeldf <-}\StringTok{ }\NormalTok{labeldf[,}\OperatorTok{-}\DecValTok{6}\NormalTok{]}
\NormalTok{labeldf[,}\DecValTok{2}\NormalTok{] <-}\StringTok{ }\OperatorTok{-}\NormalTok{(((labeldf[,}\DecValTok{2}\NormalTok{])}\OperatorTok{/}\DecValTok{2}\NormalTok{)}\OperatorTok{+}\KeywordTok{rowSums}\NormalTok{(labeldf[,}\DecValTok{3}\OperatorTok{:}\DecValTok{6}\NormalTok{]))}
\NormalTok{labeldf[,}\DecValTok{3}\NormalTok{] <-}\StringTok{ }\OperatorTok{-}\NormalTok{(((labeldf[,}\DecValTok{3}\NormalTok{])}\OperatorTok{/}\DecValTok{2}\NormalTok{)}\OperatorTok{+}\KeywordTok{rowSums}\NormalTok{(labeldf[,}\DecValTok{4}\OperatorTok{:}\DecValTok{6}\NormalTok{]))}
\NormalTok{labeldf[,}\DecValTok{4}\NormalTok{] <-}\StringTok{ }\OperatorTok{-}\NormalTok{(((labeldf[,}\DecValTok{4}\NormalTok{])}\OperatorTok{/}\DecValTok{2}\NormalTok{)}\OperatorTok{+}\KeywordTok{rowSums}\NormalTok{(labeldf[,}\DecValTok{5}\OperatorTok{:}\DecValTok{6}\NormalTok{]))}
\NormalTok{labeldf[,}\DecValTok{5}\NormalTok{] <-}\StringTok{ }\OperatorTok{-}\NormalTok{(((labeldf[,}\DecValTok{5}\NormalTok{])}\OperatorTok{/}\DecValTok{2}\NormalTok{)}\OperatorTok{+}\NormalTok{labeldf[,}\DecValTok{6}\NormalTok{])}

\NormalTok{labeldf[,}\DecValTok{10}\NormalTok{] <-}\StringTok{ }\NormalTok{((labeldf[,}\DecValTok{10}\NormalTok{])}\OperatorTok{/}\DecValTok{2}\NormalTok{)}\OperatorTok{+}\KeywordTok{rowSums}\NormalTok{(labeldf[,}\DecValTok{6}\OperatorTok{:}\DecValTok{9}\NormalTok{])}
\NormalTok{labeldf[,}\DecValTok{9}\NormalTok{] <-}\StringTok{ }\NormalTok{((labeldf[,}\DecValTok{9}\NormalTok{])}\OperatorTok{/}\DecValTok{2}\NormalTok{)}\OperatorTok{+}\KeywordTok{rowSums}\NormalTok{(labeldf[,}\DecValTok{6}\OperatorTok{:}\DecValTok{8}\NormalTok{])}
\NormalTok{labeldf[,}\DecValTok{8}\NormalTok{] <-}\StringTok{ }\NormalTok{((labeldf[,}\DecValTok{8}\NormalTok{])}\OperatorTok{/}\DecValTok{2}\NormalTok{)}\OperatorTok{+}\KeywordTok{rowSums}\NormalTok{(labeldf[,}\DecValTok{6}\OperatorTok{:}\DecValTok{7}\NormalTok{])}
\NormalTok{labeldf[,}\DecValTok{7}\NormalTok{] <-}\StringTok{ }\NormalTok{((labeldf[,}\DecValTok{7}\NormalTok{])}\OperatorTok{/}\DecValTok{2}\NormalTok{)}\OperatorTok{+}\NormalTok{labeldf[,}\DecValTok{6}\NormalTok{]}
\end{Highlighting}
\end{Shaded}

Now we can add a series of ``annotate'' commands to add the values to
the plot. in these commands, the x location is the year, the y location
is the location computed in the above code (recall that our coordinates
are flipped), and the value comes from gss18 dataframe. I round the
percentages at two decimal points and add ``hjust=.5'' to center the
text properly. I also change the color two white for the most extreme
categories to make them more readable on the darker bars.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p_annotated <-}\StringTok{ }
\StringTok{  }\KeywordTok{ggplot}\NormalTok{() }\OperatorTok{+}\StringTok{  }
\StringTok{  }\KeywordTok{geom_bar}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ gss_att_lo, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x=}\NormalTok{year, }\DataTypeTok{y=}\NormalTok{value,}\DataTypeTok{fill=}\NormalTok{col),}
           \DataTypeTok{position=}\StringTok{"stack"}\NormalTok{, }\DataTypeTok{stat=}\StringTok{"identity"}\NormalTok{,}\DataTypeTok{width=}\NormalTok{.}\DecValTok{93}\NormalTok{)}\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{geom_bar}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ gss_att_hi, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x=}\NormalTok{year, }\DataTypeTok{y=}\NormalTok{value,}\DataTypeTok{fill=}\NormalTok{col),}
           \DataTypeTok{position=}\StringTok{"stack"}\NormalTok{, }\DataTypeTok{stat=}\StringTok{"identity"}\NormalTok{,}\DataTypeTok{width=}\NormalTok{.}\DecValTok{93}\NormalTok{)}\OperatorTok{+}
\StringTok{  }\KeywordTok{geom_hline}\NormalTok{(}\DataTypeTok{yintercept =} \DecValTok{0}\NormalTok{, }\DataTypeTok{color =}\KeywordTok{c}\NormalTok{(}\StringTok{"white"}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{scale_fill_identity}\NormalTok{(}\DataTypeTok{labels =}\NormalTok{ mylevels, }\DataTypeTok{breaks=}\NormalTok{legend.pal, }\DataTypeTok{guide=}\StringTok{"legend"}\NormalTok{)}\OperatorTok{+}
\StringTok{  }\KeywordTok{theme_classic}\NormalTok{()}\OperatorTok{+}
\StringTok{  }\KeywordTok{annotate}\NormalTok{(}\StringTok{"text"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ labeldf}\OperatorTok{$}\NormalTok{year,}\DataTypeTok{y =} \DecValTok{0}\NormalTok{,}\DataTypeTok{label =} \KeywordTok{round}\NormalTok{((}\DecValTok{2}\OperatorTok{*}\NormalTok{labeldf}\OperatorTok{$}\NormalTok{OnceMonthHi),}\DecValTok{1}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{annotate}\NormalTok{(}\StringTok{"text"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ labeldf}\OperatorTok{$}\NormalTok{year, }\DataTypeTok{y =}\NormalTok{ labeldf}\OperatorTok{$}\NormalTok{Never, }\DataTypeTok{hjust =} \FloatTok{.5}\NormalTok{, }
           \DataTypeTok{label =} \KeywordTok{round}\NormalTok{(gss18_attend}\OperatorTok{$}\NormalTok{Never,}\DecValTok{1}\NormalTok{), }\DataTypeTok{color =} \StringTok{"#f5f5f5"}\NormalTok{) }\OperatorTok{+}
\StringTok{  }\KeywordTok{annotate}\NormalTok{(}\StringTok{"text"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ labeldf}\OperatorTok{$}\NormalTok{year, }\DataTypeTok{y =}\NormalTok{ labeldf}\OperatorTok{$}\StringTok{`}\DataTypeTok{Lt once a year}\StringTok{`}\NormalTok{, }\DataTypeTok{hjust =} \FloatTok{.5}\NormalTok{, }
           \DataTypeTok{label =} \KeywordTok{round}\NormalTok{(gss18_attend}\OperatorTok{$}\StringTok{`}\DataTypeTok{Lt once a year}\StringTok{`}\NormalTok{,}\DecValTok{1}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{annotate}\NormalTok{(}\StringTok{"text"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ labeldf}\OperatorTok{$}\NormalTok{year, }\DataTypeTok{y =}\NormalTok{ labeldf}\OperatorTok{$}\StringTok{`}\DataTypeTok{Once a year}\StringTok{`}\NormalTok{, }\DataTypeTok{hjust =} \FloatTok{.5}\NormalTok{, }
           \DataTypeTok{label =} \KeywordTok{round}\NormalTok{(gss18_attend}\OperatorTok{$}\StringTok{`}\DataTypeTok{Once a year}\StringTok{`}\NormalTok{,}\DecValTok{1}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{annotate}\NormalTok{(}\StringTok{"text"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ labeldf}\OperatorTok{$}\NormalTok{year, }\DataTypeTok{y =}\NormalTok{ labeldf}\OperatorTok{$}\StringTok{`}\DataTypeTok{Sevrl times a yr}\StringTok{`}\NormalTok{, }\DataTypeTok{hjust =} \FloatTok{.5}\NormalTok{, }
           \DataTypeTok{label =} \KeywordTok{round}\NormalTok{(gss18_attend}\OperatorTok{$}\StringTok{`}\DataTypeTok{Sevrl times a yr}\StringTok{`}\NormalTok{,}\DecValTok{1}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{annotate}\NormalTok{(}\StringTok{"text"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ labeldf}\OperatorTok{$}\NormalTok{year, }\DataTypeTok{y =}\NormalTok{ labeldf}\OperatorTok{$}\StringTok{`}\DataTypeTok{2-3x a month}\StringTok{`}\NormalTok{, }\DataTypeTok{hjust =} \FloatTok{.5}\NormalTok{, }
           \DataTypeTok{label =} \KeywordTok{round}\NormalTok{(gss18_attend}\OperatorTok{$}\StringTok{`}\DataTypeTok{2-3x a month}\StringTok{`}\NormalTok{,}\DecValTok{1}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{annotate}\NormalTok{(}\StringTok{"text"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ labeldf}\OperatorTok{$}\NormalTok{year, }\DataTypeTok{y =}\NormalTok{ labeldf}\OperatorTok{$}\StringTok{`}\DataTypeTok{Nrly every week}\StringTok{`}\NormalTok{, }\DataTypeTok{hjust =} \FloatTok{.5}\NormalTok{, }
           \DataTypeTok{label =} \KeywordTok{round}\NormalTok{(gss18_attend}\OperatorTok{$}\StringTok{`}\DataTypeTok{Nrly every week}\StringTok{`}\NormalTok{,}\DecValTok{1}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{annotate}\NormalTok{(}\StringTok{"text"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ labeldf}\OperatorTok{$}\NormalTok{year, }\DataTypeTok{y =}\NormalTok{ labeldf}\OperatorTok{$}\StringTok{`}\DataTypeTok{Every week}\StringTok{`}\NormalTok{, }\DataTypeTok{hjust =} \FloatTok{.5}\NormalTok{, }
           \DataTypeTok{label =} \KeywordTok{round}\NormalTok{(gss18_attend}\OperatorTok{$}\StringTok{`}\DataTypeTok{Every week}\StringTok{`}\NormalTok{,}\DecValTok{1}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{annotate}\NormalTok{(}\StringTok{"text"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ labeldf}\OperatorTok{$}\NormalTok{year, }\DataTypeTok{y =}\NormalTok{ labeldf}\OperatorTok{$}\StringTok{`}\DataTypeTok{More thn once wk}\StringTok{`}\NormalTok{, }\DataTypeTok{hjust =} \FloatTok{.5}\NormalTok{, }
           \DataTypeTok{label =} \KeywordTok{round}\NormalTok{(gss18_attend}\OperatorTok{$}\StringTok{`}\DataTypeTok{More thn once wk}\StringTok{`}\NormalTok{,}\DecValTok{1}\NormalTok{), }\DataTypeTok{color=}\StringTok{"#f5f5f5"}\NormalTok{)}\OperatorTok{+}
\StringTok{  }\KeywordTok{scale_x_continuous}\NormalTok{(}\DataTypeTok{breaks=}\KeywordTok{c}\NormalTok{(}\DecValTok{1972}\OperatorTok{:}\DecValTok{1978}\NormalTok{,}\DecValTok{1980}\NormalTok{,}\DecValTok{1982}\OperatorTok{:}\DecValTok{1991}\NormalTok{,}\DecValTok{1993}\NormalTok{,}\KeywordTok{seq}\NormalTok{(}\DecValTok{1994}\NormalTok{,}\DecValTok{2018}\NormalTok{,}\DecValTok{2}\NormalTok{)))}\OperatorTok{+}
\StringTok{  }\KeywordTok{scale_y_continuous}\NormalTok{(}\DataTypeTok{breaks=}\KeywordTok{seq}\NormalTok{(}\OperatorTok{-}\DecValTok{50}\NormalTok{,}\DecValTok{50}\NormalTok{,}\DecValTok{10}\NormalTok{),}
                     \DataTypeTok{labels=}\NormalTok{abs_percent)}\OperatorTok{+}
\StringTok{  }\KeywordTok{geom_hline}\NormalTok{(}\DataTypeTok{yintercept=}\KeywordTok{c}\NormalTok{(}\OperatorTok{-}\DecValTok{50}\NormalTok{,}\DecValTok{50}\NormalTok{),}\DataTypeTok{linetype=}\StringTok{"dashed"}\NormalTok{,}\DataTypeTok{color=}\StringTok{"grey"}\NormalTok{)}\OperatorTok{+}
\StringTok{  }\KeywordTok{coord_flip}\NormalTok{()}\OperatorTok{+}
\StringTok{  }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{title =} \StringTok{"How often do you attend religious services?"}\NormalTok{,}
       \DataTypeTok{caption =} \StringTok{"General Social Survey 1972-2018"}\NormalTok{,}
       \DataTypeTok{y =} \StringTok{""}\NormalTok{,}
       \DataTypeTok{x =} \StringTok{"Year"}\NormalTok{)}\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.title =} \KeywordTok{element_blank}\NormalTok{())}\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.title.x =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{hjust=}\NormalTok{.}\DecValTok{54}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{plot.title=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{20}\NormalTok{),}
        \DataTypeTok{plot.caption=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{10}\NormalTok{),}
        \DataTypeTok{axis.title.y=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{15}\NormalTok{),}
        \DataTypeTok{axis.text.y=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{10}\NormalTok{),}
        \DataTypeTok{axis.text.x=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{10}\NormalTok{),}
        \DataTypeTok{legend.text=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{10}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

That's it!

\includegraphics{Visualizing-Religious-Change-over-Time-20191211_files/figure-latex/Religious Attendance 1972-2018 Annotated 20191211.jpg}

\hypertarget{making-the-plot-for-variables-with-an-even-number-of-categories}{%
\section{Making the plot for variables with an even number of
categories}\label{making-the-plot-for-variables-with-an-even-number-of-categories}}

The above examples assume a variable with an odd number of categories,
but we can create these plots for variables with even numbers of
categories too. For simplicity's sake, I show how this can be done using
the GSS's gender variable. However, as noted in the main document, a
different kind of plot may be preferable when the variable of interest
has only a small number of categories.

The process begins the same as above:

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18gender}\OperatorTok{$}\NormalTok{gender   <-}\StringTok{ }\KeywordTok{as.factor}\NormalTok{(gss18gender}\OperatorTok{$}\NormalTok{gender)}
\NormalTok{gss18gender<-gss18gender[}\OperatorTok{!}\NormalTok{(gss18gender}\OperatorTok{$}\NormalTok{gender}\OperatorTok{==}\StringTok{'Dk,na'}\NormalTok{),]}
\NormalTok{gss18gender}\OperatorTok{$}\NormalTok{gender <-}\StringTok{ }\KeywordTok{droplevels}\NormalTok{(gss18gender}\OperatorTok{$}\NormalTok{gender)}
\NormalTok{gss18gender<-gss18gender[}\OperatorTok{!}\NormalTok{(}\KeywordTok{is.na}\NormalTok{(gss18gender}\OperatorTok{$}\NormalTok{year)}\OperatorTok{==}\OtherTok{TRUE}\NormalTok{),] }
\KeywordTok{levels}\NormalTok{(gss18gender}\OperatorTok{$}\NormalTok{gender)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] "Female" "Male"
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18gender}\OperatorTok{$}\NormalTok{id <-}\StringTok{ }\KeywordTok{rownames}\NormalTok{(gss18gender)}
\NormalTok{gss18gender_l <-}\StringTok{ }\NormalTok{gss18gender }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{group_by}\NormalTok{(year, gender) }\OperatorTok{%>%}
\StringTok{  }\KeywordTok{tally}\NormalTok{()}
\NormalTok{gss18_gender <-}\StringTok{ }\KeywordTok{dcast}\NormalTok{(gss18gender_l,year}\OperatorTok{~}\NormalTok{gender)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Using n as value column: use value.var to override.
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gss18_gender}\OperatorTok{$}\NormalTok{tot <-}\StringTok{ }\KeywordTok{rowSums}\NormalTok{(gss18_gender[,}\KeywordTok{c}\NormalTok{(}\DecValTok{2}\OperatorTok{:}\DecValTok{3}\NormalTok{)])}
\NormalTok{gss18_gender[,}\OperatorTok{-}\DecValTok{1}\NormalTok{] <-}\StringTok{ }\KeywordTok{lapply}\NormalTok{(gss18_gender[,}\OperatorTok{-}\DecValTok{1}\NormalTok{], }\ControlFlowTok{function}\NormalTok{(x)\{(x}\OperatorTok{/}\NormalTok{gss18_gender}\OperatorTok{$}\NormalTok{tot)}\OperatorTok{*}\DecValTok{100}\NormalTok{\})}
\NormalTok{gss18_gender <-}\StringTok{ }\NormalTok{gss18_gender[,}\OperatorTok{-}\DecValTok{4}\NormalTok{]}
\KeywordTok{head}\NormalTok{(gss18_gender)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   year   Female     Male
## 1 1972 49.96900 50.03100
## 2 1973 53.39096 46.60904
## 3 1974 53.43666 46.56334
## 4 1975 55.03356 44.96644
## 5 1976 55.37025 44.62975
## 6 1977 54.70588 45.29412
\end{verbatim}

At this point, we can skip the work of splitting a middle category and
skip to final preparations. We could simplify this code a bit now that
our variable just has two categories, but for the sake of consistency I
adapted the code from above.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{gndr_pal_lo <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"#1696d2"}\NormalTok{)}
\NormalTok{gndr_pal_hi <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"#fdbf11"}\NormalTok{)}
\NormalTok{gss_gndr_lo <-}\StringTok{ }\NormalTok{gss18_gender[,}\DecValTok{1}\OperatorTok{:}\DecValTok{2}\NormalTok{]}
\NormalTok{gss_gndr_lo <-}\StringTok{ }\KeywordTok{melt}\NormalTok{(gss_gndr_lo,}\DataTypeTok{id=}\StringTok{"year"}\NormalTok{)}
\NormalTok{gss_gndr_lo}\OperatorTok{$}\NormalTok{value <-}\StringTok{ }\OperatorTok{-}\NormalTok{gss_gndr_lo}\OperatorTok{$}\NormalTok{value}
\NormalTok{gss_gndr_lo}\OperatorTok{$}\NormalTok{col <-}\StringTok{ }\KeywordTok{rep}\NormalTok{(gndr_pal_lo,}\DataTypeTok{each=}\NormalTok{(}\DecValTok{32}\OperatorTok{/}\DecValTok{1}\NormalTok{))}
\NormalTok{gss_gndr_hi <-}\StringTok{ }\NormalTok{gss18_gender[,}\KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{3}\NormalTok{)]}
\NormalTok{gss_gndr_hi <-}\StringTok{ }\KeywordTok{melt}\NormalTok{(gss_gndr_hi,}\DataTypeTok{id=}\StringTok{"year"}\NormalTok{)}
\NormalTok{gss_gndr_hi}\OperatorTok{$}\NormalTok{col <-}\StringTok{ }\KeywordTok{rep}\NormalTok{(gndr_pal_hi,}\DataTypeTok{each=}\NormalTok{(}\DecValTok{32}\OperatorTok{/}\DecValTok{1}\NormalTok{))}
\NormalTok{mylevels_gndr <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"Women"}\NormalTok{, }\StringTok{"Men"}\NormalTok{)}
\NormalTok{gndr_legend.pal <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\StringTok{"#1696d2"}\NormalTok{,}\StringTok{"#fdbf11"}\NormalTok{)}
\NormalTok{gndr_labeldf <-}\StringTok{ }\NormalTok{gss18_gender}
\NormalTok{gndr_labeldf}\OperatorTok{$}\NormalTok{Female <-}\StringTok{ }\NormalTok{gndr_labeldf}\OperatorTok{$}\NormalTok{Female}\OperatorTok{/}\DecValTok{2}
\NormalTok{gndr_labeldf}\OperatorTok{$}\NormalTok{Male <-}\StringTok{ }\NormalTok{gndr_labeldf}\OperatorTok{$}\NormalTok{Male}\OperatorTok{/}\DecValTok{2}
\end{Highlighting}
\end{Shaded}

Now we are ready to create the plot. I include annotations for the
values.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{p2 <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{() }\OperatorTok{+}\StringTok{  }
\StringTok{  }\KeywordTok{geom_bar}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ gss_gndr_lo, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x=}\NormalTok{year, }\DataTypeTok{y=}\NormalTok{value,}\DataTypeTok{fill=}\NormalTok{col), }
           \DataTypeTok{position=}\StringTok{"stack"}\NormalTok{, }\DataTypeTok{stat=}\StringTok{"identity"}\NormalTok{,}\DataTypeTok{width=}\NormalTok{.}\DecValTok{95}\NormalTok{)}\OperatorTok{+}\StringTok{ }
\StringTok{  }\KeywordTok{geom_bar}\NormalTok{(}\DataTypeTok{data =}\NormalTok{ gss_gndr_hi, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x=}\NormalTok{year, }\DataTypeTok{y=}\NormalTok{value,}\DataTypeTok{fill=}\NormalTok{col), }
           \DataTypeTok{position=}\StringTok{"stack"}\NormalTok{, }\DataTypeTok{stat=}\StringTok{"identity"}\NormalTok{,}\DataTypeTok{width=}\NormalTok{.}\DecValTok{95}\NormalTok{)}\OperatorTok{+}
\StringTok{  }\KeywordTok{geom_hline}\NormalTok{(}\DataTypeTok{yintercept =} \DecValTok{0}\NormalTok{, }\DataTypeTok{color =}\KeywordTok{c}\NormalTok{(}\StringTok{"white"}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{scale_fill_identity}\NormalTok{(}\DataTypeTok{labels =}\NormalTok{ mylevels_gndr, }\DataTypeTok{breaks=}\NormalTok{gndr_legend.pal, }\DataTypeTok{guide=}\StringTok{"legend"}\NormalTok{)}\OperatorTok{+}
\StringTok{  }\KeywordTok{theme_classic}\NormalTok{()}\OperatorTok{+}
\StringTok{  }\KeywordTok{annotate}\NormalTok{(}\StringTok{"text"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ gndr_labeldf}\OperatorTok{$}\NormalTok{year, }\DataTypeTok{y=}\OperatorTok{-}\NormalTok{gndr_labeldf}\OperatorTok{$}\NormalTok{Female, }\DataTypeTok{hjust =} \FloatTok{.5}\NormalTok{, }
           \DataTypeTok{label =} \KeywordTok{round}\NormalTok{((gndr_labeldf}\OperatorTok{$}\NormalTok{Female}\OperatorTok{*}\DecValTok{2}\NormalTok{),}\DecValTok{2}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{annotate}\NormalTok{(}\StringTok{"text"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ gndr_labeldf}\OperatorTok{$}\NormalTok{year, }\DataTypeTok{y=}\NormalTok{gndr_labeldf}\OperatorTok{$}\NormalTok{Male, }\DataTypeTok{hjust =} \FloatTok{.5}\NormalTok{, }
           \DataTypeTok{label =} \KeywordTok{round}\NormalTok{((gndr_labeldf}\OperatorTok{$}\NormalTok{Male}\OperatorTok{*}\DecValTok{2}\NormalTok{),}\DecValTok{2}\NormalTok{))}\OperatorTok{+}
\KeywordTok{scale_x_continuous}\NormalTok{(}\DataTypeTok{breaks=}\KeywordTok{c}\NormalTok{(}\DecValTok{1972}\OperatorTok{:}\DecValTok{1978}\NormalTok{,}\DecValTok{1980}\NormalTok{,}\DecValTok{1982}\OperatorTok{:}\DecValTok{1991}\NormalTok{,}\DecValTok{1993}\NormalTok{,}\KeywordTok{seq}\NormalTok{(}\DecValTok{1994}\NormalTok{,}\DecValTok{2018}\NormalTok{,}\DecValTok{2}\NormalTok{)))}\OperatorTok{+}
\KeywordTok{scale_y_continuous}\NormalTok{(}\DataTypeTok{breaks=}\KeywordTok{seq}\NormalTok{(}\OperatorTok{-}\DecValTok{50}\NormalTok{,}\DecValTok{50}\NormalTok{,}\DecValTok{10}\NormalTok{),}
                     \DataTypeTok{labels=}\NormalTok{abs_percent)}\OperatorTok{+}
\StringTok{  }\KeywordTok{geom_hline}\NormalTok{(}\DataTypeTok{yintercept=}\KeywordTok{c}\NormalTok{(}\OperatorTok{-}\DecValTok{50}\NormalTok{,}\DecValTok{50}\NormalTok{),}\DataTypeTok{linetype=}\StringTok{"dashed"}\NormalTok{,}\DataTypeTok{color=}\StringTok{"grey"}\NormalTok{)}\OperatorTok{+}
\StringTok{  }\KeywordTok{coord_flip}\NormalTok{()}\OperatorTok{+}
\StringTok{  }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{title =} \StringTok{"Gender of GSS Respondents"}\NormalTok{,}
       \DataTypeTok{caption =} \StringTok{"General Social Survey 1972-2018"}\NormalTok{,}
       \DataTypeTok{y =} \StringTok{""}\NormalTok{,}
       \DataTypeTok{x =} \StringTok{"Year"}\NormalTok{)}\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{legend.title =} \KeywordTok{element_blank}\NormalTok{())}\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{axis.title.x =} \KeywordTok{element_text}\NormalTok{(}\DataTypeTok{hjust=}\NormalTok{.}\DecValTok{54}\NormalTok{))}\OperatorTok{+}
\StringTok{  }\KeywordTok{theme}\NormalTok{(}\DataTypeTok{plot.title=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{20}\NormalTok{),}
        \DataTypeTok{plot.caption=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{10}\NormalTok{),}
        \DataTypeTok{axis.title.y=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{15}\NormalTok{),}
        \DataTypeTok{axis.text.y=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{10}\NormalTok{),}
        \DataTypeTok{axis.text.x=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{10}\NormalTok{),}
        \DataTypeTok{legend.text=}\KeywordTok{element_text}\NormalTok{(}\DataTypeTok{size=}\DecValTok{10}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

And there we have it!

\includegraphics{Visualizing-Religious-Change-over-Time-20191211_files/Gender 1972-2018 Annotated 20191211.jpg}


\end{document}
