class: center, middle, inverse, title-slide # R for Data Science With Sports Applications ### 2023-11-08 --- ## Grammar of graphics: layers of a plot - Data - Geoms - Aesthetic mappings - Facets --- ### Data - The underlying data frame. - Think rows and columns. --- ### Geoms - Shapes to represent the data. - Individual geoms use one shape per row. - Group geoms use multiple rows to create a shape. --- ### Aesthetic mappings - Map between the aesthetic properties of a geom and column of the data. --- ### Facets - Also called small multiples. - Slice the rows into multiple sub populations. --- ### Identify the components of the grammar of graphics <img src="index_files/figure-html/unnamed-chunk-1-1.png" width="100%" /> --- <img src="index_files/figure-html/unnamed-chunk-2-1.png" width="100%" /> --- ### Identify the components (2) <img src="index_files/figure-html/unnamed-chunk-3-1.png" width="100%" /> --- <img src="index_files/figure-html/unnamed-chunk-4-1.png" width="100%" /> --- ### Scatter plot: Data - Call the `ggplot` function and pass in a data frame. ```r ggplot(df) ``` <img src="index_files/figure-html/unnamed-chunk-5-1.png" width="100%" /> --- ### Scatter plot: Aesthetic mappings - Pass the aes function with the mapping as a second argument. ```r ggplot(df, aes(x=AST, y=TOV)) ``` <img src="index_files/figure-html/unnamed-chunk-6-1.png" width="100%" /> --- ### Scatter plot: Geoms - Add a layer to use one point to represent one row. ```r ggplot(df, aes(x=AST, y=TOV)) + geom_point() ``` <img src="index_files/figure-html/unnamed-chunk-7-1.png" width="100%" /> --- # More mappings: - Shape and color ```r ggplot(df, aes(x=AST, y=TOV, color=Playoff, shape=Playoff)) + geom_point() ``` <img src="index_files/figure-html/unnamed-chunk-8-1.png" width="100%" /> <!-- ### How to make a barchart --> <!-- # ```{r} --> <!-- # library(ggplot2) --> <!-- # --> <!-- # df %>% --> <!-- # arrange(-PTS) %>% --> <!-- # head(5) %>% --> <!-- # ggplot(aes(PTS, Team, fill=Conference)) + --> <!-- # geom_col() --> <!-- # ``` --> --- ### Lab 2 - Create a plot to understand the relationship between two pointers made and three pointers made. - Think about the data and the aesthetic mappings that you want. - Put it in an Rmarkdown file.