2024

Load the ggplot2 package

# Don't forget to load the ggplot2 package
library(ggplot2)

The aes function

# The aes function is used to map variables to aesthetics
ggplot(mpg, aes(x = displ, y = hwy)) + 
    geom_point()

Common aesthetics

  • x and y for the x and y axes
  • color for the color of the points
  • shape for the shape of the points
  • size for the size of the points
  • alpha for the transparency of the points
  • linetype for the type of line
  • fill for the fill color of the points

Color

ggplot(mpg, aes(x = displ, y = hwy, color = class)) + 
    geom_point()

Shape

ggplot(mpg, aes(x = displ, y = hwy, shape = class)) + 
    geom_point()

Size

ggplot(mpg, aes(x = displ, y = hwy, size = class)) + 
    geom_point()

Alpha

ggplot(mpg, aes(x = displ, y = hwy, alpha = class)) + 
    geom_point()

Linetype

ggplot(mpg, aes(x = displ, y = hwy, linetype = class)) + 
    geom_point()

Fill

ggplot(mpg, aes(x = displ, y = hwy, fill = class)) + 
    geom_point()

Question

Make a ggplot encoding the class variable in the mpg dataset using the fill, shape, and color aesthetics.

Answer

Click here for the answer
ggplot(mpg, aes(x = displ, 
                y = hwy, 
                fill=class, 
                shape=class,
                color=class)) + 
    geom_point()

Answer