Data

Run the following first in your console to create the data example:

# Load packages
library(dplyr)
library(ggplot2)

# Create data frame
simple_ex <-
  data_frame(
    A = c(1, 2, 3, 4),
    B = c(1, 2, 3, 4),
    C = c(3, 2, 1, 2),
    D = c("a", "a", "b", "b")
  )

Let’s view the data frame, which is in tidy format:

View(simple_ex)
A B C D
1 1 3 a
2 2 2 a
3 3 1 b
4 4 2 b

Recall

  • A statistical graphic is a mapping of data variables to aes()thetic attributes of geom_etric objects.
  • A scatterplot has points as the geom_etric object
  • A linegraph has lines as the geom_etric object

1. Basic Scatterplot

  • the geom_etric objects are points
  • the aesthetic attributes are:
    • x-axis is variable A
    • y-axis is variable B
ggplot(data=simple_ex, aes(x=A, y=B)) + 
  geom_point()

2. Scatterplot with Color

  • the geom_etric objects are points
  • the aesthetic attributes are:
    • x-axis is variable A
    • y-axis is variable B
    • color is variable D
ggplot(data=simple_ex, aes(x=A, y=B, color=D)) + 
  geom_point()

3. Scatterplot with Sizes

  • the geom_etric objects are points
  • the aesthetic attributes are:
    • x-axis is variable A
    • y-axis is variable B
    • size is variable C
ggplot(data=simple_ex, aes(x=A, y=B, size=C)) + 
  geom_point()

4. Line Graph

  • the geom_etric objects are lines
  • the aesthetic attributes are:
    • x-axis is variable A
    • y-axis is variable B
ggplot(data=simple_ex, aes(x=A, y=B)) + 
  geom_line()

5. Line Graph with Color

  • the geom_etric objects are lines
  • the aesthetic attributes are:
    • x-axis is variable A
    • y-axis is variable B
    • color is variable D
ggplot(data=simple_ex, aes(x=A, y=B, color=D)) + 
  geom_line()