class: center, middle, inverse, title-slide .title[ # Visualization - Aesthetic Mapping ] --- class: inverse, middle, center --- # A visualization: -- - is a geometry object (a geom) -- - whose aesthetics -- - represents variables -- - from a data set --- # Aesthetics mean -- - “something you can see”. -- Examples include: - position (i.e., on the x and y axes) - color (“outside” color) - fill (“inside” color) - shape (of points) - size --- # Aesthetics Mapping -- - map -- - variables -- - to aesthetics --- class: inverse, middle, center # Examples --- ```r library(tidyverse) library(knitr) # For knitting document and include_graphics function df <- read_csv('https://bryantstats.github.io/math421/data/all-states-history.csv') library(lubridate) df <- df %>% filter(positiveIncrease>0) %>% mutate(year = year(date), quarters = quarters(date), month = month(date), day = wday(date), day_type = case_when(day < 6 ~ 'weekday', TRUE~'weekend')) ``` --- ```r df %>% filter(state=='RI'|state=='MA') %>% ggplot()+ geom_point(mapping = aes(x = date, y = positiveIncrease, color = state)) ``` ![](6_viz_files/figure-html/unnamed-chunk-3-1.png)<!-- --> --- ```r df %>% filter(state=='RI'|state=='MA') %>% ggplot()+ geom_line(mapping = aes(x = date, y = positiveIncrease, color = state)) ``` ![](6_viz_files/figure-html/unnamed-chunk-4-1.png)<!-- --> --- ```r df %>% ggplot()+ geom_smooth(mapping = aes(x = positiveIncrease, y = deathIncrease, color = day_type)) ``` ![](6_viz_files/figure-html/unnamed-chunk-5-1.png)<!-- --> --- ```r df %>% filter(month >8, year==2020) %>% ggplot()+ geom_smooth(mapping = aes(x = positiveIncrease, y = deathIncrease, color = day_type)) ``` ![](6_viz_files/figure-html/unnamed-chunk-6-1.png)<!-- --> --- ```r df %>% filter(state=='RI'|state=='MA') %>% ggplot()+ geom_density(mapping = aes(x = positiveIncrease, color = state)) ``` ![](6_viz_files/figure-html/unnamed-chunk-7-1.png)<!-- --> --- ```r df %>% filter(state=='RI'|state=='MA') %>% ggplot()+ geom_bar(mapping = aes(x = day_type, fill = state)) ``` ![](6_viz_files/figure-html/unnamed-chunk-8-1.png)<!-- --> --- # Aesthetic of a geom - A geom has its list of own aesthetics - Use `?geom_point()` to check for the list of `geom_point` - Some aesthetics are required, some are not --- class: inverse, middle, center # Required Aesthetics --- ```r df %>% filter(state=='RI'|state=='MA') %>% ggplot()+ geom_point(mapping = aes(x = date, y = positiveIncrease, color = state)) ``` ![](6_viz_files/figure-html/unnamed-chunk-9-1.png)<!-- --> --- ```r df %>% filter(state=='RI'|state=='MA') %>% ggplot()+ geom_point(mapping = aes(x = date, y = positiveIncrease)) ``` ![](6_viz_files/figure-html/unnamed-chunk-10-1.png)<!-- --> --- ```r df %>% filter(state=='RI'|state=='MA') %>% ggplot()+ geom_point(mapping = aes(x = date)) ``` - Will produce an error since `y` aesthetic is required for `geom_point`. --- class: inverse, middle, center # Common Visualization Practices --- # One Continuous Variable - Density: `geom_density` - Histogram: `geom_histogram` - Boxplot: `geom_boxplot` --- **One Continuous Variable: Density** ```r df %>% ggplot()+ geom_density(mapping = aes(x = positiveIncrease)) ``` ![](6_viz_files/figure-html/unnamed-chunk-12-1.png)<!-- --> --- **One Continuous Variable: Histogram** ```r df %>% ggplot()+ geom_histogram(mapping = aes(x = positiveIncrease)) ``` ![](6_viz_files/figure-html/unnamed-chunk-13-1.png)<!-- --> --- **One Continuous Variable: Boxplot** ```r df %>% ggplot()+ geom_boxplot(mapping = aes(y = positiveIncrease)) ``` ![](6_viz_files/figure-html/unnamed-chunk-14-1.png)<!-- --> --- **One Continuous Variable: Boxplot** ```r df %>% filter(positiveIncrease<1000) %>% ggplot()+ geom_boxplot(mapping = aes(y = positiveIncrease)) ``` ![](6_viz_files/figure-html/unnamed-chunk-15-1.png)<!-- --> --- **One Categorical Variable: Bar chart** ```r df %>% ggplot()+ geom_bar(mapping = aes(x = day_type)) ``` ![](6_viz_files/figure-html/unnamed-chunk-16-1.png)<!-- --> --- # Two Continuous Variables - Scatter Plot: `geom_point` - Line Plot: `geom_line` - Smooth Plot: `geom_smooth` --- # One Continuous Variable + One Categorical Variable - Density - BoxPlot --- **One Continuous + One Categorical: Density** ```r df %>% ggplot()+ geom_density(mapping = aes(x = positiveIncrease, color = day_type)) ``` ![](6_viz_files/figure-html/unnamed-chunk-17-1.png)<!-- --> --- **One Continuous + One Categorical: Density** ```r df %>% filter(positiveIncrease<1000) %>% ggplot()+ geom_density(mapping = aes(x = positiveIncrease, color = day_type)) ``` ![](6_viz_files/figure-html/unnamed-chunk-18-1.png)<!-- --> --- **One Continuous + One Categorical: Boxplot** ```r df %>% ggplot()+ geom_boxplot(mapping = aes(x = positiveIncrease, y = day_type)) ``` ![](6_viz_files/figure-html/unnamed-chunk-19-1.png)<!-- --> --- **One Continuous + One Categorical: Boxplot** ```r df %>% filter(positiveIncrease<1000) %>% ggplot()+ geom_boxplot(mapping = aes(x = positiveIncrease, y = day_type)) ``` ![](6_viz_files/figure-html/unnamed-chunk-20-1.png)<!-- --> --- # One Categorical Variable + One Categorical Variable - Barplot --- **One Categorical + One Categorical: Barplot** ```r df %>% ggplot()+ geom_bar(mapping=aes(x=month, fill=day_type), position='dodge') ``` ![](6_viz_files/figure-html/unnamed-chunk-21-1.png)<!-- --> --- # More - [ggplot cheat sheet](https://raw.githubusercontent.com/rstudio/cheatsheets/main/data-visualization.pdf), or [backup](data-visualization.pdf)