R for Data Science With Sports Applications

2023-09-28

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

Identify the components (2)

Scatter plot: Data

  • Call the ggplot function and pass in a data frame.
ggplot(df)

Scatter plot: Aesthetic mappings

  • Pass the aes function with the mapping as a second argument.
ggplot(df, aes(x=AST, y=TOV))

Scatter plot: Geoms

  • Add a layer to use one point to represent one row.
ggplot(df, aes(x=AST, y=TOV)) + geom_point()

More mappings:

  • Shape and color
ggplot(df, aes(x=AST, y=TOV, color=Playoff, shape=Playoff)) + 
  geom_point()

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.