Nic Cage Mood-o-meter: https://www.eatliver.com/mood-board/

Nic Cage Mood-o-meter: https://www.eatliver.com/mood-board/

R Notes

ggplot2: Mapping Aesthetics

The Grammar of Graphcis: All data visualizations map data to aesthetic attributes (location, shape, color) of geometric objects (lines, points, bars)

  • Quantitative (continuous, discrete, time) mapped to position, shape size/line width, color, transparency, …
  • Qualitiative (ordered, unordered, text) mapped to position, shape/line type, color

Scales control the mapping from data to aesthetics and provide tools to read the plot (axes, legends). Geometric objects are drawn in a specific coordinate system.

A plot can contains statistical transformations of the data (counts, means, medians) and faceting can be used to generate the same plot for different subsets of the data.

Basic Syntax

ggplot(data, aes(x=, y=, color=, shape=, size=)) +
  geom_point() # or geom_histogram() or geom_boxplot(), etc.
  • The data must be a data frame.
  • The aes() function maps columns of the data frame to aesthetic properties of geometric shapes to be plotted.
  • ggplot() defines the plot; the geoms show the data
  • layers are added with +

For example:

ggplot(data, aes(x = var1, y = var2)) +
  geom_point(aes(color = var3)) +
  geom_smooth(color = "red") +
  labs(title = "Helpful Title", 
       x = "x-axis label")

# geom_histogram(), geom_boxplot(), geom_bar(), etc.
  • Building layers (data, geoms, scales, labels, etc.)
  • Mapping versus setting aesthetics
  • Global versus geom-specific aesthetics

Beyond ggplot + geoms

  • Create small multiples based on subsets of data:
    • facet_wrap()
    • facet_grid()
  • Modify the coordinate system:
    • coord_cartesian allows us to zoom in on a plot
    • coord_flip allows us to flip the x and y axis


xkcd


To the Script!!!