class: center, middle, inverse, title-slide # Data and visualization
📉 ### Dr. Lucy D’Agostino McGowan --- layout: true <div class="my-footer"> <span> <img src = "img/dsbox-logo.png" width = "30"> </img> Slides adapted from <a href="https://datasciencebox.org" target="_blank">datasciencebox.org</a> by Dr. Lucy D'Agostino McGowan </span> </div> --- ## <i class="fas fa-laptop"></i> `Starwars` - Go to the [sta-363-s20 GitHub organization](https://github.com/sta-363-s20) and search for `appex-02-starwars` - Clone this repository into RStudio Cloud --- class: center, middle # Exploratory data analysis --- ## What is EDA? - Exploratory data analysis (EDA) is an approach to analyzing data sets to summarize its main characteristics. - Often, this is visual. That's what we're focusing on today. - But we might also calculate summary statistics and perform data wrangling/manipulation/transformation at (or before) this stage of the analysis. That's what we'll focus on next. --- class: center, middle # Data visualization --- ## Data visualization > *"The simple graph has brought more information to the data analyst’s mind than any other device." — John Tukey* - Data visualization is the creation and study of the visual representation of data. - There are many tools for visualizing data (R is one of them), and many approaches/systems within R for making data visualizations (**ggplot2** is one of them, and that's what we're going to use). --- ## ggplot2 `\(\in\)` tidyverse .pull-left[ ![](img/02/ggplot2-part-of-tidyverse.png) ] .pull-right[ - **ggplot2** is tidyverse's data visualization package - The `gg` in "ggplot2" stands for Grammar of Graphics - It is inspired by the book **Grammar of Graphics** by Leland Wilkinson <sup>†</sup> - A grammar of graphics is a tool that enables us to concisely describe the components of a graphic ![](img/02/grammar-of-graphics.png) ] .footnote[ <sup>†</sup> Source: [BloggoType](http://bloggotype.blogspot.com/2016/08/holiday-notes2-grammar-of-graphics.html) ] --- .question[ Which function creates the plot? ] .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass)) + geom_point() + labs(title = "Mass vs. height of Starwars characters", x = "Height (cm)", y = "Weight (kg)") ``` ``` ## Warning: Removed 28 rows containing missing values (geom_point). ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-2-1.png)<!-- --> ] --- .question[ What is the dataset being plotted? ] .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass)) + geom_point() + labs(title = "Mass vs. height of Starwars characters", x = "Height (cm)", y = "Weight (kg)") ``` ``` ## Warning: Removed 28 rows containing missing values (geom_point). ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-3-1.png)<!-- --> ] --- .question[ Which variables are on the x-axis and y-axis? ] .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass)) + geom_point() + labs(title = "Mass vs. height of Starwars characters", x = "Height (cm)", y = "Weight (kg)") ``` ``` ## Warning: Removed 28 rows containing missing values (geom_point). ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-4-1.png)<!-- --> ] --- .question[ What about that warning? ] .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass)) + geom_point() + labs(title = "Mass vs. height of Starwars characters", x = "Height (cm)", y = "Weight (kg)") ``` ``` ## Warning: Removed 28 rows containing missing values (geom_point). ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-5-1.png)<!-- --> ] --- .question[ What does `geom_smooth()` do? What else changed between the previous plot and this one? ] .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass)) + geom_point() + * geom_smooth() + labs(title = "Mass vs. height of Starwars characters", x = "Height (cm)", y = "Weight (kg)") ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-6-1.png)<!-- --> ] --- ## Hello ggplot2! ![](img/02/ggplot2_masterpiece.png) .my-footer[ <font size="2"> Artwork by @allison_horst </font> ] --- ## ggplot2 components - **data** - **aes**thetic mapping - layer(s) --- ## ggplot2 premise ![](img/02/ggplot-premise.png) --- ## Hello ggplot2! ![](img/02/layer-1.png) --- ## Hello ggplot2! ![](img/02/layer-2.png) --- ## Hello ggplot2! ![](img/02/layer-3.png) --- ## Hello ggplot2! ![](img/02/layer-4.png) --- ## Hello ggplot2! ![](img/02/layer-5.png) --- ## Hello ggplot2! - `ggplot()` is the main function in ggplot2 and plots are constructed in layers - The structure of the code for plots can often be summarized as ```r ggplot + geom_xxx ``` -- or, more precisely .small[ ```r ggplot(data = [dataset], mapping = aes(x = [x-variable], y = [y-variable])) + geom_xxx() + other options ``` ] --- ## Hello ggplot2! - To use ggplot2 functions, first load tidyverse ```r library(tidyverse) ``` -- - For help with the ggplot2, see [ggplot2.tidyverse.org](http://ggplot2.tidyverse.org/) --- class: center, middle # Visualizing Star Wars --- ## Dataset terminology .question[ What does each row represent? What does each column represent? ] ```r starwars ``` .small[ ``` ## # A tibble: 5 x 13 ## name height mass hair_color skin_color eye_color birth_year gender homeworld ## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr> ## 1 Luke… 172 77 blond fair blue 19 male Tatooine ## 2 C-3PO 167 75 <NA> gold yellow 112 <NA> Tatooine ## 3 R2-D2 96 32 <NA> white, bl… red 33 <NA> Naboo ## 4 Dart… 202 136 none white yellow 41.9 male Tatooine ## 5 Leia… 150 49 brown light brown 19 female Alderaan ## # … with 4 more variables: species <chr>, films <list>, vehicles <list>, ## # starships <list> ``` ] -- - Each row is an **observation** - Each column is a **variable** --- ## Luke Skywalker ![luke-skywalker](img/02/luke-skywalker.png) --- ## What's in the Star Wars data? Take a `glimpse` at the data: ```r glimpse(starwars) ``` ``` ## Observations: 87 ## Variables: 13 ## $ name <chr> "Luke Skywalker", "C-3PO", "R2-D2", "Darth Vader", "Leia O… ## $ height <int> 172, 167, 96, 202, 150, 178, 165, 97, 183, 182, 188, 180, … ## $ mass <dbl> 77.0, 75.0, 32.0, 136.0, 49.0, 120.0, 75.0, 32.0, 84.0, 77… ## $ hair_color <chr> "blond", NA, NA, "none", "brown", "brown, grey", "brown", … ## $ skin_color <chr> "fair", "gold", "white, blue", "white", "light", "light", … ## $ eye_color <chr> "blue", "yellow", "red", "yellow", "brown", "blue", "blue"… ## $ birth_year <dbl> 19.0, 112.0, 33.0, 41.9, 19.0, 52.0, 47.0, NA, 24.0, 57.0,… ## $ gender <chr> "male", NA, NA, "male", "female", "male", "female", NA, "m… ## $ homeworld <chr> "Tatooine", "Tatooine", "Naboo", "Tatooine", "Alderaan", "… ## $ species <chr> "Human", "Droid", "Droid", "Human", "Human", "Human", "Hum… ## $ films <list> [<"Revenge of the Sith", "Return of the Jedi", "The Empir… ## $ vehicles <list> [<"Snowspeeder", "Imperial Speeder Bike">, <>, <>, <>, "I… ## $ starships <list> [<"X-wing", "Imperial shuttle">, <>, <>, "TIE Advanced x1… ``` --- ## What's in the Star Wars data? .question[ How many rows and columns does this dataset have? What does each row represent? What does each column represent? ] Run the following **in the Console** to view the help ```r ?starwars ``` <img src="img/02/starwars-help.png" width="435" /> --- ## Mass vs. height .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass)) + geom_point() ``` ``` ## Warning: Removed 28 rows containing missing values (geom_point). ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-15-1.png)<!-- --> ] --- ## What's that warning? - Not all characters have height and mass information (hence 28 of them not plotted) .small[ ``` ## Warning: Removed 28 rows containing missing values (geom_point). ``` ] - Going forward I'll suppress the warning to save room on slides, but it's important to note it --- ## Mass vs. height .question[ How would you describe this relationship? What other variables would help us understand data points that don't follow the overall trend? Who is the not so tall but really chubby character? ] .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass)) + geom_point() + * labs(title = "Mass vs. height of Starwars characters", * x = "Height (cm)", y = "Weight (kg)") ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-16-1.png)<!-- --> ] --- ## Jabba! <img src="img/02/jabbaplot.png" width="768" /> --- ## Additional variables We can map additional variables to various features of the plot: - aesthetics - color - size - shape - alpha (transparency) - faceting: small multiples displaying different subsets --- class: center, middle # Aesthetics --- ## Aesthetics options Visual characteristics of plotting characters that can be **mapped to a specific variable** in the data are - `color` - `size` - `shape` - `alpha` (transparency) --- ## Mass vs. height + gender .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass, color = gender)) + geom_point() ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-18-1.png)<!-- --> ] --- ## Mass vs. height + gender Let's map the size to birth_year: .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass, color = gender, * size = birth_year )) + geom_point() ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-19-1.png)<!-- --> ] --- ## Mass vs. height + gender Let's now increase the size of all points **not** based on the values of a variable in the data: .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass, color = gender)) + * geom_point(size = 2) ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-20-1.png)<!-- --> ] --- ## Aesthetics summary - Continuous variable are measured on a continuous scale - Discrete variables are measured (or often counted) on a discrete scale aesthetics | discrete | continuous ------------- | ------------------------ | ------------ color | rainbow of colors | gradient size | discrete steps | linear mapping between radius and value shape | different shape for each | shouldn't (and doesn't) work - Use aesthetics for mapping features of a plot to a variable, define the features in the geom for customization **not** mapped to a variable --- class: center, middle # Faceting --- ## Faceting options - Smaller plots that display different subsets of the data - Useful for exploring conditional relationships and large data .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass)) + * facet_grid(. ~ gender) + geom_point() + labs(title = "Mass vs. height of Starwars characters", * subtitle = "Faceted by gender", x = "Height (cm)", y = "Weight (kg)") ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-21-1.png)<!-- --> ] --- ## Dive further... .question[ In the next few slides describe what each plot displays. Think about how the code relates to the output. ] -- <br><br><br> .alert[ The plots in the next few slides do not have proper titles, axis labels, etc. because we want you to figure out what's happening in the plots. But you should always label your plots! ] --- .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass)) + geom_point() + facet_grid(gender ~ .) ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-22-1.png)<!-- --> ] --- .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass)) + geom_point() + facet_grid(. ~ gender) ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-23-1.png)<!-- --> ] --- .small[ ```r ggplot(data = starwars, mapping = aes(x = height, y = mass)) + geom_point() + facet_wrap(~ eye_color) ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-24-1.png)<!-- --> ] --- ## Facet summary - `facet_grid()`: - 2d grid - `rows ~ cols` - use `.` for no split - `facet_wrap()`: 1d ribbon wrapped into 2d --- class: center, middle ## More ggplot2 info: ## ggplot2 in 2 ### [https://leanpub.com/ggplot2in2/c/sta-212-f19](https://leanpub.com/ggplot2in2/c/sta-212-f19) --- ## <i class="fas fa-laptop"></i> `Starwars` - Go to the [sta-363-s20 GitHub organization](https://github.com/sta-363-s20) and search for `appex-02-starwars` - Clone this repository into RStudio Cloud - Complete the exercises - **Commit** your changes - **Push** them back to GitHub --- class: center, middle # Identifying variables --- ## Types of variables - **Numerical variables** (quantitative variables) can be classified as **continuous** or **discrete** based on whether or not the variable can take on an infinite number of values or only non-negative whole numbers, respectively. - If the variable is **categorical**, we can determine if it is **ordinal** based on whether or not the levels have a natural ordering. --- class: center, middle # Visualizing numerical data --- ## Describing shapes of numerical distributions * shape: * skewness: right-skewed, left-skewed, symmetric (skew is to the side of the longer tail) * modality: unimodal, bimodal, multimodal, uniform * center: mean (`mean`), median (`median`), mode (not always useful) * spread: range (`range`), standard deviation (`sd`), inter-quartile range (`IQR`) * unusual observations --- ## Histograms .small[ ```r ggplot(data = starwars, mapping = aes(x = height)) + geom_histogram(binwidth = 10) ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-25-1.png)<!-- --> ] --- ## Density plots .small[ ```r ggplot(data = starwars, mapping = aes(x = height)) + geom_density() ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-26-1.png)<!-- --> ] --- ## Side-by-side box plots .small[ ```r ggplot(data = starwars, mapping = aes(y = height, x = gender)) + geom_boxplot() ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-27-1.png)<!-- --> ] --- ## Side-by-side box plots .small[ ```r ggplot(data = starwars, mapping = aes(y = height, x = gender)) + geom_boxplot(outlier.shape = NA) + geom_jitter() ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-28-1.png)<!-- --> ] --- class: center, middle ## Show your data! --- class: center, middle # Visualizing categorical data --- ## Bar plots .small[ ```r ggplot(data = starwars, mapping = aes(x = gender)) + geom_bar() ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-29-1.png)<!-- --> ] --- ## Segmented bar plots, counts .small[ ```r ggplot(data = starwars, mapping = aes(x = gender, fill = hair_color)) + geom_bar() ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-30-1.png)<!-- --> ] --- ## Segmented bar plots, proportions .small[ ```r ggplot(data = starwars, mapping = aes(x = gender, fill = hair_color)) + geom_bar(position = "fill") + labs(y = "proportion") ``` ![](03-data-visualizing_files/figure-html/unnamed-chunk-31-1.png)<!-- --> ] --- ## Which plot is more appropriate? .question[ Which plot is a more useful representation for visualizing the relationship between gender and height? ] ![](03-data-visualizing_files/figure-html/unnamed-chunk-32-1.png)<!-- -->![](03-data-visualizing_files/figure-html/unnamed-chunk-32-2.png)<!-- -->