Agenda


Explore aesthetics such as

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

Intro


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.

Libraries


library(ggplot2)
library(dplyr)
library(tidyr)

Map Color to Variable


ggplot(mtcars, aes(x = disp, y = mpg, color = factor(cyl))) +
  geom_point()

Map Color to Variable


ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(aes(color = factor(cyl)))

Color: Specify Value


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

Map Shape to Variable


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

Map Shape to Variable


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

Shape: Specify Value


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

Shape & Color


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

Fill


ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(shape = 5, fill = 'blue')

Fill: Specify Value


ggplot(mtcars, aes(x = disp, y = mpg)) +
  geom_point(shape = 22, fill = 'blue')

Geom Border Color


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

Map Size to Variable (Continuous)


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

Specify Size Value


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

Map Alpha to Variable


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

Other Plots


Before we wrap up, let us quickly see how we can map aesthetics to variables for

  • bar plots
  • histograms
  • box plots

Data


ecom <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/web.csv')
## # A tibble: 1,000 x 11
##       id referrer device bouncers n_visit n_pages duration        country
##    <int>    <chr>  <chr>    <chr>   <int>   <dbl>    <dbl>          <chr>
##  1     1   google laptop     true      10       1      693 Czech Republic
##  2     2    yahoo tablet     true       9       1      459          Yemen
##  3     3   direct laptop     true       0       1      996         Brazil
##  4     4     bing tablet    false       3      18      468          China
##  5     5    yahoo mobile     true       9       1      955         Poland
##  6     6    yahoo laptop    false       5       5      135   South Africa
##  7     7    yahoo mobile     true      10       1       75     Bangladesh
##  8     8   direct mobile     true      10       1      908      Indonesia
##  9     9     bing mobile    false       3      19      209    Netherlands
## 10    10   google mobile     true       6       1      208 Czech Republic
## # ... with 990 more rows, and 3 more variables: purchase <chr>,
## #   order_items <dbl>, order_value <dbl>

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)

Map Bar Color to Purchase


ggplot(ecom, aes(device, fill = purchase)) +
  geom_bar()

Map Histogram Color to Purchase


ggplot(ecom) +
  geom_histogram(aes(duration, fill = purchase), bins = 10)

Map Box Plot Color to Purchase


ggplot(ecom) +
  geom_boxplot(aes(device, duration, fill = purchase))