Agenda


  • create
    • simple bar plot
    • stacked bar plot
    • grouped bar plot
  • modify bar
    • direction
    • color
    • line color
    • width
    • labels
  • modify axis range
  • remove axes from the plot
  • specify the line type of the X axes
  • offset the Y axes
  • modify legend

Introduction


  • a bar plot represents data in rectangular bars
  • the length of the bars are proportional to the values they represent
  • bar plots can be either horizontal or vertical
  • the X axis of the plot represents the levels or the categories
  • and the Y axis represents the frequency/count of the variable

Libraries


library(readr)
library(dplyr)
library(magrittr)

Data


ecom <- read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/ecom.csv',
  col_types = list(col_factor(levels = c('Desktop', 'Mobile', 'Tablet')), 
  col_factor(levels = c(TRUE, FALSE)), col_factor(levels = c(TRUE, FALSE)), 
  col_factor(levels = c('Affiliates', 'Direct', 'Display', 'Organic', 'Paid', 'Referral', 'Social'))))
## # A tibble: 5,000 x 4
##     device bouncers purchase   referrer
##     <fctr>   <fctr>   <fctr>     <fctr>
##  1 Desktop    FALSE    FALSE Affiliates
##  2  Mobile    FALSE    FALSE Affiliates
##  3 Desktop     TRUE    FALSE    Organic
##  4 Desktop    FALSE    FALSE    Organic
##  5  Mobile     TRUE    FALSE     Direct
##  6 Desktop     TRUE    FALSE     Direct
##  7 Desktop    FALSE    FALSE   Referral
##  8  Tablet     TRUE    FALSE    Organic
##  9  Mobile     TRUE    FALSE     Social
## 10 Desktop     TRUE    FALSE    Organic
## # ... with 4,990 more rows

Data Dictionary


Below is the description of the data set:

  • device: device used to visit the website
  • bouncers: whether visit was a bouncer (exit website from landing page)
  • purchase: whether visitor purchased
  • referrer: referrer website/search engine

Using plot function


plot(ecom$device)

Using barplot function


barplot(table(ecom$device))