class: center, middle, inverse, title-slide # Data Visualization and Design ## Using ggplot2
### Omni Analytics Group --- class: center, middle # BUILDING PLOTS --- class: center, middle # Underlying theory --- # What is a graphic? ggplot2 uses the idea that you can build every graph from the same components: 1. a **data** set 2. a **coordinate system** 3. **geoms** - visual marks that represent data - to display values, map variables in the data to visual properties of the geom (**aesthetics**) like **size**, **color**, and **x** and **y** locations ![](images/build1.png)![](images/build2.png) --- # How to build a graph Complete the template below to build a graph .pull-left[ ![](images/ggplot2-notation.png) ] .pull-right[ <p align="center"> <img src="images/Cut_outs/Cut_out_05.png" width="200px" height="160px"> </p> ] --- # How to build a graph `ggplot(cryptoart, aes(x = Platform, y = End.Price))` - This will begin a plot that you can finish by adding layers to. - You can add one geom per layer <img src="building-plots_files/figure-html/plots-4-1.png" /> --- # What is a geom? In ggplot2, we use a geom function to represent data points, and use the geom's aesthetic properties to represent variables. <img src="building-plots_files/figure-html/unnamed-chunk-2-1.png" /> Once our data is formatted and we know what type of variables we are working with, we can select the correct geom for our visualization. --- # Available geoms <img src="building-plots_files/figure-html/unnamed-chunk-3-1.png" style="display: block; margin: auto;" /> --- # What is a layer? - it determines the physical representation of the data - Together, the data, mappings, statistical transformation, and geometric object form a layer - A plot may have multiple layers <img src="building-plots_files/figure-html/unnamed-chunk-4-1.png" /> --- # Alternative method of building layers: Stats A stat builds a new variable to plot (e.g., count and proportion) .pull-left[ <img src="building-plots_files/figure-html/unnamed-chunk-5-1.png" /> ] .pull-right[ <img src="building-plots_files/figure-html/unnamed-chunk-6-1.png" /> ] --- # Faceting A way to extract subsets of data and place them side-by-side in graphics ```r ggplot(cryptoart, aes(x = Start.Price, y = End.Price, colour = Platform))+ geom_point() ggplot(cryptoart, aes(x = Start.Price, y = End.Price, colour = Platform)) + geom_point() +facet_grid(.~Platform) ``` <img src="building-plots_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> --- # Faceting Options - `facet_grid(. ~ b)`:facet into columns based on b - `facet_grid(a ~ .)`:facet into columns based on a - `facet_grid(a ~ b)`:facet into both rows and columns - `facet_wrap( ~ fl)`:wrap facets into a rectangular layout You can set scales to let axis limits vary across facets: - `facet_grid(y ~ x, scales = "free")`: x and y axis limits adjust to individual facets - "free_x" - x axis limits adjust - "free_y" - y axis limits adjust You can also set a labeller to adjust facet labels: - `facet_grid(. ~ fl, labeller = label_both)` - `facet_grid(. ~ fl, labeller = label_bquote(alpha ^ .(x)))` - `facet_grid(. ~ fl, labeller = label_parsed)` --- # Position Adjustments Position adjustments determine how to arrange geoms that would otherwise occupy the same space - **Dodge**: Arrange elements side by side - **Fill**: Stack elements on top of one another, normalize height - **Stack**: Stack elements on top of one another `ggplot(cryptoart, aes(Times.Sold, fill = Growth)) + geom_bar(position = "")` <img src="building-plots_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> --- # Position Adjustments: Jitter - **Jitter**: Add random noise to X & Y position of each element to avoid overplotting - There is also a jitter geom <img src="building-plots_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> --- ## Coordinate Systems - `coord_cartesian()`: The default cartesian coordinate system - `coord_fixed()`: Cartesian with fixed aspect ratio between x & y units - `coord_flip()`: Flipped Cartesian coordinates - `coord_polar()`: Polar coordinates - `coord_trans()`: Transformed cartesian coordinates. - `coord_map()`: Map projections from the mapproj package (mercator (default), azequalarea, lagrange, etc.) <img src="building-plots_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> --- ## ggplot2 extensions There are many ggplot2 extensions available. For example, * gganimate * GGally You can find other extensions here: https://exts.ggplot2.tidyverse.org/gallery/ --- ## gganimate .pull-left[ gganimate extends the grammar of graphics as implemented by ggplot2 to include the description of animation. ```r ggplot(cryptoart, aes(x = Platform, y = End.Price)) + geom_boxplot()+theme(axis.text.x = element_text(angle = 45))+ transition_states( Times.Sold, transition_length = 2, state_length = 1 ) + enter_fade() + exit_shrink() + ease_aes('sine-in-out') ``` ] .pull-right[ ![](building-plots_files/figure-html/p1-out-1.gif)<!-- --> ] --- ## GGally .pull-left[ GGally extends ggplot2 by adding several functions to reduce the complexity of combining geoms with transformed data. ```r ggpairs(cryptoart) ``` ] .pull-right[ ![](building-plots_files/figure-html/p2-out-1.png)<!-- --> ]