ggplot2: Aesthetics

Introduction

In this module, we will focus on the aesthetics i.e. color, shape, size, alpha, line type, line width etc. We can map these to variables or specify values for them. If we want to map the above to variables, we have to specify them within the aes() function. We will look at both methods. Explore aesthetics such as

  • color
  • shape
  • size
  • fill
  • alpha
  • width

Data

Introduction


ecom

Data Dictionary


  • id: row id
  • referrer: referrer website/search engine
  • os: operating system
  • browser: browser
  • device: device used to visit the website
  • n_pages: number of pages visited
  • duration: time spent on the website (in seconds)
  • repeat: frequency of visits
  • country: country of origin
  • purchase: whether visitor purchased
  • order_value: order value of visitor (in dollars)

Color

Introduction


Map color to variable


Example


# map color to variable
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(aes(color = factor(gear)))

Instructions


  • map color to cyl
# map color to cyl
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(____)
# set color to 'blue'
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(aes(color = factor(cyl)))

Specify value for color


Example


# map color to variable
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(color = 'red')

Instructions


  • set color to 'blue'
# set color to 'blue'
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(____)
# set color to 'blue'
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(color = 'blue')

Shape

Introduction


Map shape to variable


Example


ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(aes(shape = factor(gear)))

Instructions


  • map shape to cyl
# map shape to cyl
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(____)
# map shape to cyl
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(aes(shape = factor(cyl)))

Map shape to value


Example


ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(shape = 2)

Instructions


  • set shape to 5
# set shape to 5
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(____)
# set shape to 5
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(shape = 5)

Fill

Introduction


Example


Instructions


  • set fill to 'blue'
# set fill to 'blue'
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(shape = 5, fill = ____)

Instructions


  • set fill to 'blue'
# set fill to 'blue'
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(shape = 22, fill = ____)

Instructions


  • map fill to purchase
# map fill to purchase
ggplot(ecom) +
  geom_bar(aes(device, fill = ____))

Instructions


  • set fill to 'blue'
# set color to 'blue'
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(shape = 22, color = ____)
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(shape = 5, fill = 'blue')
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(shape = 22, fill = 'blue')
ggplot(ecom, aes(device, fill = purchase)) +
  geom_bar()
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(shape = 22, color = 'blue')

Size

Introduction


Map size to variable


Example


ggplot(mtcars) +
  geom_point(aes(x = disp, y = mpg, size = wt))

Instructions


  • map size to hp
ggplot(mtcars) +
  geom_point(aes(x = disp, y = mpg, ____))
ggplot(mtcars) +
  geom_point(aes(x = disp, y = mpg, size = hp))

Map size to value


Example


ggplot(mtcars) +
  geom_point(aes(x = disp, y = mpg, size = 2))

Instructions


  • set size to 4
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(____)
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(size = 4)

Alpha

Introduction


Map alpha to variable


Example


ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(aes(alpha = factor(gear)), color = 'blue')
## Warning: Using alpha for a discrete variable is not advised.

Instructions


  • map alpha to cyl
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(aes(______), color = 'blue')
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(aes(alpha = factor(cyl)), color = 'blue')

Map alpha to value


Example


ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(alpha = 0.7, color = 'blue')

Instructions


  • set alpha to 0.3
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(alpha = , color = 'blue')
ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(alpha = 0.3, color = 'blue')