Graphs with ggplot2

Illya Mowerman

2/13/2018

Some code before we get started

Note that you will need to change the path for reading in the data

library(tidyverse)

hr <- read_csv("~/Dropbox/Bridgeport/2018 Spring/Data/HR_comma_sep.csv")

affairs <- read_csv("~/Dropbox/Bridgeport/2018 Spring/Data/affairs.csv")

Topics

Histograms are used to visualize distributions

ggplot(affairs) + 
  geom_histogram(aes(rating))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(affairs) + 
  geom_histogram(aes(rating) , binwidth = 1)

Box plots are like histograms, but with additional information

ggplot(hr) +
  geom_boxplot(aes(y = satisfaction_level  , x = ''))

ggplot(hr) +
  geom_boxplot(aes(y = satisfaction_level  , x = sales))

Scatter plots describe the relationships between two continuous variables

ggplot(hr) +
  geom_point(aes(satisfaction_level , last_evaluation))

ggplot(hr) +
  geom_smooth(aes(satisfaction_level , last_evaluation))
## `geom_smooth()` using method = 'gam'

Bar graphs are used to describe counts for categorical variables

ggplot(affairs) +
  geom_bar(aes(occupation))

Pie chart are used soley to describe proportions

ggplot(affairs) +
  geom_bar(aes(x = '', fill = factor(occupation)) , width = 1)

ggplot(affairs) +
  geom_bar(aes(x = '', fill = factor(occupation)) , width = 1) + 
  coord_polar(theta = "y")

Bubble plots are scatter plots with an aditional continuous variable plotted as the size of the dot, which converts the dot (an observation) into a bubble

ggplot(affairs) +
  geom_point(aes(x = rating , y = religiousness , size = affairs))

ggplot(affairs) +
  geom_jitter(aes(x = rating , y = religiousness , size = affairs))