class: center, middle, inverse, title-slide .title[ # Introduction to R for Data Analysis ] .subtitle[ ## Data Visualization - Part 1 ] .author[ ### Johannes Breuer, Stefan Jünger, & Veronika Batzdorfer ] .date[ ### 2022-08-17 ] --- layout: true --- ## Why should we use data visualization? In general: - contribute to a better understanding of your analysis results - understand your data in the first place - Generating a plot is easy as you will see - ... Making good plots, however, can take a while --- ## Plots in `R` - `R` is fun, and so is creating plots in `R` - Almost every plot type is supported in `R` - either in your standard installation or through additional packages - A large number of exports formats are supported - `.png`, `.jpg`, `.tiff`, `.svg`, `.bmp`, `.pdf`, `.eps`, etc. --- ## We'll start rather basic <img src="data:image/png;base64,#https://github.com/jobreu/r-intro-gesis-2021/blob/main/content/img/trump.jpg?raw=true" width="85%" style="display: block; margin: auto;" /> .footnote[https://twitter.com/katjaberlin/status/1290667772779913218] --- ## Content of the visualization sessions .pull-left[ **`Base R` visualization** - Standard plotting procedures in R - very short ] .pull-right[ **`tidyverse`/`ggplot2` visualization** - Modern interface to graphics - grammar of graphics ] There's more that we won't cover: - [`lattice`](https://cran.r-project.org/web/packages/lattice/index.html) plots, for example --- ## Data for this session As before, we will use the data from the *GESIS German General Social Survey (ALLBUS) 2021*. ```r library(sjlabelled) library(tidyverse) library(haven) allbus_2021_dvI <- haven::read_sav( "./data/allbus_2021ZA5280_v1-0-0.sav" ) %>% sjlabelled::set_na(na = c(-1:-99, 97, 98)) %>% rowwise() %>% mutate( sum_trust = sum( c_across(pt01:pt20), na.rm = TRUE ) ) %>% remove_all_labels() %>% ungroup() %>% as_tibble() ``` --- ## Graphics in `R` Since the early days, graphics are a first-class citizen in `R`. A standard `R` installation doesn't require any additional packages for creating graphics. The `graphics` package is already included for that. .pull-left[ ```r hist(allbus_2021_dvI$sum_trust, breaks =15) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-2-1.png" style="display: block; margin: auto;" /> ] --- ## Ok, but let's start from the beginning The most basic function to plot in R is `plot()`. .pull-left[ ```r plot(allbus_2021_dvI$sum_trust) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-3-1.png" style="display: block; margin: auto;" /> ] --- ## We can turn this into a bivariate scatterplot .pull-left[ ```r plot( allbus_2021_dvI$agec, allbus_2021_dvI$sum_trust ) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> ] --- ## Add some jitter and also change the points type .pull-left[ ```r plot( jitter(allbus_2021_dvI$agec, 2), jitter(allbus_2021_dvI$sum_trust, 2), pch = 1 ) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> ] --- ## Adding stuff to the plot: titles & labels .pull-left[ ```r plot( jitter(allbus_2021_dvI$agec, 2), jitter(allbus_2021_dvI$sum_trust, 2), pch = 1, main = "Relationship Age and Trust in Institutions", xlab = "Age of Respondents", ylab = "Trust in Institutions" ) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> ] --- ## Where to go from here with `base R` graphics? .pull-left[ Using similar procedures, we can add more and more stuff to our plot or edit its elements: - regression lines - legends - annotations - colors - etc. ] .pull-right[ We can also create different *plot types*, such as - histograms - barplots - boxplots - densities - pie charts - etc. ] --- ## Example: A simple boxplot .pull-left[ ```r boxplot( allbus_2021_dvI$sum_trust ~ allbus_2021_dvI$eastwest ) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-7-1.png" style="display: block; margin: auto;" /> ] --- class: center, middle # [Exercise](https://stefanjuenger.github.io/r-intro-gesis-2022/exercises/Exercise_3_2_1_Simple_Plots.html) time 🏋️♀️💪🏃🚴 ## [Solutions](https://stefanJuenger.github.io/r-intro-gesis-2022/solutions/Exercise_3_2_1_Simple_Plots.html) --- ## The `par()` and `dev.off()` functions for plots `par()` stands for graphical parameters and is called before the actual plotting function. It prepares the graphics device in `R`. The most commonly used options are for "telling" the device that 2, 3, 4, or `x` plots have to be printed. We can, e.g., use `mfrow` for specifying how many rows (the first value in the vector) and columns (the second value in the vector) we aim to plot. ```r par(mfrow = c(2, 2)) ``` One caveat of using this function is that we actively have to turn off the device before generating another independent plot. ```r dev.off() ``` --- ## Exporting plots It's nice that `R` provides such pleasant plotting opportunities. However, to include them in our papers, we need to export them. As said in the beginning, numerous export formats are available in `R`. --- ## Exporting with *RStudio* <img src="data:image/png;base64,#https://github.com/jobreu/r-intro-gesis-2021/blob/main/content/img/saveGraphic.PNG?raw=true" style="display: block; margin: auto;" /> --- ## Saving plots via a command Alternatively, you can also export plots with the commands `png()`, `pdf()` or `jpeg()`, for example. For this purpose, you first have to wrap the plot call between one of those functions and a `dev.off()`call. ```r png("Plot.png") plot(allbus_2021_dvI$agec) dev.off() ``` ```r pdf("Plot.pdf") plot(allbus_2021_dvI$agec) dev.off() ``` ```r jpeg("Plot.jpeg") plot(allbus_2021_dvI$agec) dev.off() ``` --- class: center, middle # [Exercise](https://stefanjuenger.github.io/r-intro-gesis-2022/exercises/Exercise_3_2_2_Handling_Multiple_Plots.html) time 🏋️♀️💪🏃🚴 ## [Solutions](https://stefanjuenger.github.io/r-intro-gesis-2022/solutions/Exercise_3_2_2_Handling_Multiple_Plots.html) --- ## A personal note on `base R` plotting Hopefully, you have gotten the feeling that the `base R` base techniques for plotting already are well-suited for your daily data exploration needs. But to be honest: I do not use all the other functions that often. The syntax is sometimes cumbersome with all the `par()` or `dev.off()` calls, and manipulating parameters simply feels somewhat "outdated". In the following, we will turn towards more modern techniques using the `ggplot2` package. Yet, we still believe that it is worthwhile to become comfortable with `base R` plotting since `ggplot2`, e.g., may sometimes be "too much" for simple quick data exploration. **As so often, in the end, it's also a matter of taste.** --- ## What is `ggplot2`? `ggplot2` is another `R` package for creating plots and is part of the `tidyverse`. It uses the *grammar of graphics*. Some things to note about `ggplot2`: - it is well-suited for multi-dimensional data - it expects data (frames) as input - components of the plot are added as layers ```r plot_call + layer_1 + layer_2 + ... + layer_n ``` --- ## `ggplot2` examples .pull-left[ <img src="data:image/png;base64,#https://github.com/jobreu/r-intro-gesis-2021/blob/main/content/img/143_radar_chart_multi_indiv_2.png?raw=true" style="display: block; margin: auto;" /> ] .pull-right[ <img src="data:image/png;base64,#https://github.com/jobreu/r-intro-gesis-2021/blob/main/content/img/21_ggplot2_ddensity_plot.png?raw=true" style="display: block; margin: auto;" /> ] <small><small>Sources: https://www.r-graph-gallery.com/wp-content/uploads/2016/05/143_radar_chart_multi_indiv_2.png and https://www.r-graph-gallery.com/wp-content/uploads/2015/09/21_ggplot2_ddensity_plot.png</small></small> --- ## `ggplot2` examples .pull-left[ <img src="data:image/png;base64,#https://github.com/jobreu/r-intro-gesis-2021/blob/main/content/img/51_scatterplot_linear_model_with_CI_ggplot2.png?raw=true" style="display: block; margin: auto;" /> ] .pull-right[ <img src="data:image/png;base64,#https://github.com/jobreu/r-intro-gesis-2021/blob/main/content/img/328_Hexbin_map_USA_4.png?raw=true" style="display: block; margin: auto;" /> ] <small><small>Sources: https://www.r-graph-gallery.com/wp-content/uploads/2015/11/51_scatterplot_linear_model_with_CI_ggplot2-300x300.png and https://www.r-graph-gallery.com/wp-content/uploads/2017/12/328_Hexbin_map_USA_4-300x200.png</small></small> --- ## Barplots as in `base R` .tinyish[ .pull-left[ ```r ggplot(allbus_2021_dvI , aes(x = agec)) + geom_bar() ``` ] ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto;" /> ] --- ## Boxplots as in `base R` .tinyish[ .pull-left[ ```r ggplot( allbus_2021_dvI , aes( x = as.factor(agec), y = sum_trust) ) + geom_boxplot() ``` ] ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-15-1.png" style="display: block; margin: auto;" /> ] --- ## Components of a plot According to Wickham (2010, 8)* a layered plot consists of the following components: <span class="footnote"> <small><small><span class="red bold">*</span> http://dx.doi.org/10.1198/jcgs.2009.07098</small></small> </span> - data and aesthetic mappings, - geometric objects, - scales, - and facet specification ```r plot_call + data + aesthetics + geometries + scales + facets ``` --- ## Data requirements You can use one single data frame to create a plot in `ggplot2`. This creates a smooth workflow from data wrangling to the final presentation of the results. <br> <img src="data:image/png;base64,#https://github.com/jobreu/r-intro-gesis-2021/blob/main/content/img/data-science_man.png?raw=true" width="65%" style="display: block; margin: auto;" /> <small><small>Source: http://r4ds.had.co.nz</small></small> --- ## Why the long format? 🐴 `ggplot2` prefers data in long format (**NB**: of course, only if this is possible and makes sense for the data set at hand) .pull-left[ We may want to get used to it as this format has some benefits: - every element we aim to plot is an observation - no thinking required how a specific variable relates to an observation - most importantly, the long format is more parsimonious - it requires less memory and less disk space ] .pull-right[ <img src="data:image/png;base64,#https://github.com/jobreu/r-intro-gesis-2021/blob/main/content/img/long.png?raw=true" width="40%" style="display: block; margin: auto;" /> <small><small>Source: https://github.com/gadenbuie/tidyexplain#tidy-data</small></small> ] --- ## Before we start The architecture of building plots in `ggplot` is similar to standard `R` graphics. There is an initial plotting call, and subsequently, more stuff is added to the plot. However, in `base R`, it is sometimes tricky to find out how to add (or remove) certain plot elements. For example, think of removing the axis ticks in the scatter plot. We will systematically explore which elements are used in `ggplot` in this session. --- ## Scatterplot from earlier .tinyish[ .pull-left[ ```r ggplot( data = allbus_2021_dvI, aes( x = agec, y = sum_trust ) ) + geom_point() ``` ] ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-16-1.png" style="display: block; margin: auto;" /> ] --- ## Scatterplot from earlier with jitter .tinyish[ .pull-left[ ```r ggplot( data = allbus_2021_dvI, aes( x = agec, y = sum_trust ) ) + geom_jitter() ``` ] ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-17-1.png" style="display: block; margin: auto;" /> ] --- ## Creating your own plot We do not want to give a lecture on the theory behind data visualization (if you want that, we suggest having a look at the excellent book [*Fundamentals of Data Visualization*](https://serialmentor.com/dataviz/) by Claus O. Wilke). Creating plots is all about practice... and 'borrowing' code from others. Three components are important: - Plot initiation and data input - aesthetics definition - so-called geoms --- ## Plot initiation Now, let's start from the beginning and have a closer look at the *grammar of graphics*. .pull-left[ `ggplot()` is the most basic command to create a plot: ```r ggplot() ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-18-1.png" style="display: block; margin: auto;" /> ] **But it doesn't show anything...** --- ## What now? Data input! .pull-left[ ```r ggplot(data = allbus_2021_dvI ) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-19-1.png" style="display: block; margin: auto;" /> ] **Still nothing there...** --- ## `aes`thetics! .pull-left[ `ggplot` requires information about the variables to plot. ```r ggplot(data = allbus_2021_dvI ) + aes(x = agec, y = sum_trust) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-20-1.png" style="display: block; margin: auto;" /> ] **That's a little bit better, right?** --- ## `geom`s! .pull-left[ Finally, `ggplot` needs information *how* to plot the variables. ```r ggplot(data = allbus_2021_dvI ) + aes(x = agec, y = sum_trust) + geom_point() ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-21-1.png" style="display: block; margin: auto;" /> ] **A scatter plot!** --- ## Add a fancy `geom` .pull-left[ We can also add more than one `geom`. ```r ggplot(data = allbus_2021_dvI) + aes(x = agec, y = sum_trust) + geom_jitter() + geom_smooth(method = "lm", se = FALSE) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-22-1.png" style="display: block; margin: auto;" /> ] **A regression line!** (without confidence intervals; the regression behind this operation is run automatically) --- class: center, middle # [Exercise](https://stefanjuenger.github.io/r-intro-gesis-2022/exercises/Exercise_3_2_3_Plotting_Repeats.html) time 🏋️♀️💪🏃🚴 ## [Solutions](https://stefanjuenger.github.io/r-intro-gesis-2022/solutions/Exercise_3_2_3_Plotting_Repeats.html) --- ## Going further: Working with grouping variables For creating grouped plots, we need grouping variables... ```r allbus_2021_dvI <- allbus_2021_dvI %>% sjlabelled::remove_all_labels() %>% mutate( pol_leaning_cat = case_when( between(pa01, 0, 3) ~ "left", between(pa01, 4, 7) ~ "center", pa01 > 7 ~ "right" ) ) %>% filter(pol_leaning_cat != "NA") ``` --- ## Going further: adding group `aes`thetics .pull-left[ We can add different colors for different groups in our data. ```r ggplot( data = allbus_2021_dvI, aes( x = agec, y = sum_trust, group = pol_leaning_cat ) ) + geom_smooth(method = "lm", se = FALSE) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-23-1.png" style="display: block; margin: auto;" /> ] --- ## Manipulating group `aes`thetics .pull-left[ We can also change the colors that are used in the plot. ```r ggplot( data = allbus_2021_dvI , aes( x = agec, y = sum_trust, group = pol_leaning_cat, color = pol_leaning_cat ) ) + geom_smooth(method = "lm", se = FALSE) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-24-1.png" style="display: block; margin: auto;" /> ] The legend is drawn automatically, that's handy! --- ## Using another color palette .pull-left[ ```r ggplot( data = allbus_2021_dvI , aes( x = agec, y = sum_trust, color = pol_leaning_cat, group = pol_leaning_cat ) ) + geom_smooth(method = "lm", se = FALSE) + scale_color_brewer( palette = "Dark2" ) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-25-1.png" style="display: block; margin: auto;" /> ] --- ## Difference between `color` and `fill` Notably, there are two components of the plot or `geom` associated with colors: `color` and `fill`. Generally, `color` refers to the geometry borders, such as a line. `fill` refers to a geometry area, such as a polygon. Have this difference in mind when you use `scale_color_brewer` or `scale_fill_brewer` in your plots. Manipulating these colors and their corresponding legends in an elaborate plot can get a little tricky. --- ## Colors and `theme`s One particular strength of `ggplot2` lies in its immense theming capabilities. The package has some built-in theme functions that makes theming a plot fairly easy, e.g., - `theme_bw()` - `theme_dark()` - `theme_void()` - etc. See: https://ggplot2.tidyverse.org/reference/ggtheme.html If you want to, you can play around with some of those themes in the exercises for this session. In general, the [`r-color-palettes` repository by Emil Hvitfeldt](https://github.com/EmilHvitfeldt/r-color-palettes) is a good resource for choosing color palettes in `R` and there are many collections of full `ggplot2` themes out there (e.g., the [`hrbrthemes` package](https://github.com/hrbrmstr/hrbrthemes)). --- ## Alternative to being too colorful: facets .pull-left[ ```r ggplot( data = allbus_2021_dvI , aes( x = agec, y = sum_trust ) ) + geom_smooth( color = "black", method = "lm", se = FALSE ) + facet_wrap(~pol_leaning_cat, ncol = 3) + theme_bw() ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-26-1.png" style="display: block; margin: auto;" /> ] --- ## The `theme()` argument in general The most direct interface for manipulating your theme is the `theme()` argument. Here you can change the appearance of: - axis labels - captions and titles - legend - grid layout - the wrapping strips - ... --- ## Example: changing the grid layout & axis labels .pull-left[ ```r ggplot( data = allbus_2021_dvI, aes( x = agec, y = sum_trust ) ) + geom_smooth( color = "black", method = "lm", se = FALSE ) + facet_wrap(~pol_leaning_cat, ncol = 3) + theme_bw() + theme( panel.grid.major = element_blank(), panel.grid.minor = element_blank(), strip.background = element_rect(fill = "white") ) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-27-1.png" style="display: block; margin: auto;" /> ] --- ## Example: changing axis labels .pull-left[ ```r ggplot( data = allbus_2021_dvI , aes( x = agec, y = sum_trust ) ) + geom_smooth( color = "black", method = "lm", se = FALSE ) + facet_wrap(~pol_leaning_cat, ncol = 3) + theme_bw() + theme( panel.grid.major = element_blank(), panel.grid.minor = element_blank(), strip.background = element_rect(fill = "white") ) + ylab("Trust Score") + xlab("Age") ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-28-1.png" style="display: block; margin: auto;" /> ] --- ## A note on plotting options .pull-left[ Working with combined aesthetics and different data inputs can become challenging. Particularly, plotting similar aesthetics which interfere with the automatic procedures can create conflicts. Some 'favourites' include: - Multiple legends - and various color scales for similar `geoms` ] .pull-right[ <img src="data:image/png;base64,#https://github.com/jobreu/r-intro-gesis-2021/blob/main/content/img/800px-The_Scream.jpg?raw=true" style="display: block; margin: auto;" /> ] .right[ <small><small>Source: https://de.wikipedia.org/wiki/Der_Schrei#/media/File:The_Scream.jpg</small></small> ] --- ## `ggplot` plots are 'simple' objects In contrast to standard `R` plots, `ggplot2` outputs are standard objects like any other object in `R` (they are lists). So there is no graphics device involved from which we have to record our plot to re-use it later. We can just use it directly. ```r my_fancy_plot <- ggplot(data = allbus_2021_dvI, aes( x = agec, y = sum_trust ) ) + geom_point() my_fancy_plot <- my_fancy_plot + geom_smooth() ``` Additionally, there is also no need to call `dev.off()` --- ## It makes combining plots easy As of today, there are now a lot of packages that help to combine `ggplot2`s fairly easily. For example, the [`cowplot` package](https://cran.r-project.org/web/packages/cowplot/index.html) provides a really flexible framework. Yet, fiddling with this package can become quite complicated. A very easy-to-use package for combining `ggplot`s is [`patchwork` package](https://cran.r-project.org/web/packages/patchwork/index.html). --- ## Plotting side by side in one row .pull-left[ ```r library(patchwork) my_barplot <- ggplot( allbus_2021_dvI , aes(x = agec) ) + geom_bar() my_boxplot <- ggplot( allbus_2021_dvI , aes(y = agec) ) + geom_boxplot() my_barplot | my_boxplot ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-30-1.png" style="display: block; margin: auto;" /> ] --- ## Plotting in two columns .pull-left[ ```r my_barplot / my_boxplot ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-31-1.png" style="display: block; margin: auto;" /> ] --- ## Combining with `base R` graphics .pull-left[ ```r library(gridGraphics) (my_barplot | ~barplot(table(allbus_2021_dvI$agec)) ) / (my_boxplot | ~boxplot(allbus_2021_dvI$agec) ) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-32-1.png" style="display: block; margin: auto;" /> ] --- ## There's more You can also annotate plots with titles, subtitles, captions, and tags. You can nest plots and introduce more complex layouts. If you're interested in this, you should check out the [`patchwork` repository on *GitHub*](https://github.com/thomasp85/patchwork) as everything is really well-documented there. --- ## Exporting ggplot graphics Exporting `ggplot2` graphics is fairly easy with the `ggsave()` function. It automatically detects the file format. You can also define the plot height, width, and dpi, which is particularly useful to produce high-class graphics for publications. ```r nice_plot <- ggplot( allbus_2021_dvI , aes(x = agec) ) + geom_bar() ggsave("nice_plot.png", nice_plot, dpi = 300) ``` Or: ```r ggsave("nice_plot.tiff", nice_plot, dpi = 300) ``` --- ## Visual exploratory data analysis In the session on *Exploratory Data Analysis* (EDA), we have said that visualization should be part of EDA. We can use `ggplot2` for this, but there also are many packages out there that offer helpful visualization functions. We will look at two of those, `visdat` (for visualizing missing data patterns) and `GGAlly` (for visualizing correlations) in the following. Many of these packages build on `ggplot2` and their output can, hence, be further customized or extended using `ggplot2` or its extension packages. --- ## Plotting the structure of missing data .pull-left[ ```r library(visdat) vis_miss(allbus_2021_dvI [,3:20]) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-35-1.png" style="display: block; margin: auto;" /> ] --- ## Fancier barplots: Relative frequencies .pull-left[ ```r library(scales) allbus_2021_dvI %>% ggplot( aes( x = educ, fill = educ ) ) + geom_bar( aes( y = (..count..)/sum(..count..) ) ) + scale_y_continuous( labels = percent ) + ylab("Relative Frequencies") ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-36-1.png" style="display: block; margin: auto;" /> ] --- ## Fancier barplots: Percentages & formatting .pull-left[ ```r allbus_2021_dvI %>% mutate( choice_of_party = recode_factor( pv01, `1` = "CDU/CSU", `2` = "SPD", `42` = "AFD", `3` = "FDP", `6` = "Die Linke", `4` = "Gruene", `90` = "Other", `91`= "no voting" ) ) %>% filter(!is.na(choice_of_party)) %>% ggplot(aes(x = choice_of_party, fill = choice_of_party)) + geom_bar(aes(y = (..count..)/sum(..count..))) + scale_y_continuous(labels = percent, expand = expansion(mult = c(0, 0.1))) + ylab("Relative Frequencies") + xlab("") ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-37-1.png" style="display: block; margin: auto;" /> ] --- ## Correlation plots I .pull-left[ ```r library(correlation) library(ggraph) allbus_2021_dvI %>% select(pt01:pt20) %>% correlation() %>% plot() ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-38-1.png" style="display: block; margin: auto;" /> ] --- ## Correlation plots II .pull-left[ ```r library(GGally) allbus_2021_dvI %>% select(pt01:pt20) %>% ggcorr( label = TRUE, label_round = 2 ) ``` ] .pull-right[ <img src="data:image/png;base64,#3_2_Data_Visualization_Part_1_files/figure-html/unnamed-chunk-39-1.png" style="display: block; margin: auto;" /> ] --- ## Some additional resources - [ggplot2 - Elegant Graphics for Data Analysis](https://www.springer.com/gp/book/9783319242750) by Hadley Wickham - [Chapter 3](https://r4ds.had.co.nz/data-visualisation.html) in *R for Data Science* - [Fundamentals of Data Visualization](https://serialmentor.com/dataviz/) by Claus O. Wilke - [Data Visualization - A Practical Introduction](https://press.princeton.edu/titles/13826.html) by Kieran Healy - [data-to-viz](https://www.data-to-viz.com/) - [R Graph Gallery](https://www.r-graph-gallery.com/) - [BBC Visual and Data Journalism cookbook for R graphics](https://bbc.github.io/rcookbook/#how_to_create_bbc_style_graphics) - [List of `ggplot2` extensions](https://exts.ggplot2.tidyverse.org/) --- class: center, middle # [Exercise](https://stefanjuenger.github.io/r-intro-gesis-2022/exercises/Exercise_3_2_4_GGood_Plots.html) time 🏋️♀️💪🏃🚴 ## [Solutions](https://stefanjuenger.github.io/r-intro-gesis-2022/solutions/Exercise_3_2_4_GGood_Plots.html)