+ - 0:00:00
Notes for current slide
Notes for next slide



Bayesian Statistics and Computing

Lecture 4: Data Visualization in R

Yanfei Kang | BSC | 2021 Spring

1 / 28

"The simple graph has brought more information to the data analyst’s mind than any other device."

— John Tukey

2 / 28

Objectives

  1. basic graphics with R

  2. elegant graphics with ggplot2

3 / 28

Basic graphics with R

4 / 28

mpg dataframe

mpg contains observations collected by the US Environment Protection Agency on 38 models of car. You can see more details via ?mpg. Among the variables in mpg are:

  1. displ, a car’s engine size, in litres.
  2. hwy, a car's fuel efficiency on the highway, in miles per gallon (mpg). A car with a low fuel efficiency consumes more fuel than a car with a high fuel efficiency when they travel the same distance.
  3. ...
5 / 28

Basic plots

Practice and look at the help document of plot.

library(ggplot2)
plot(mpg$displ, mpg$hwy)
abline(lm(hwy ~ displ, data = mpg))
title("Regression of MPG on engine size")

6 / 28

Histograms and density plots

Histograms
hist(mpg$hwy)

Density plots
d <- density(mpg$hwy) ## returns the density data
plot(d)

7 / 28

Piechart

car.table <- table(mpg$manufacturer)
pie(car.table)

8 / 28

Boxplots

## Boxplot of MPG
boxplot(mpg$hwy, main = "Boxplot of MPG")
## Boxplot of MPG by Car Cylinders
boxplot(hwy ~ cyl, data = mpg, main = "Car Milage Data", xlab = "Number of Cylinders", ylab = "Miles Per Gallon")

9 / 28

Correlation plot

library(corrplot)
M <- cor(mtcars)
corrplot(M, addCoef.col = "grey")

10 / 28

Time series

plot(AirPassengers)

11 / 28

Multiple plots

par(mfrow = c(1, 2))
plot(AirPassengers)
boxplot(ggplot2::mpg$hwy, data = ggplot2::mpg, main = "Boxplot of MPG")

12 / 28

Elegant graphics with ggplot2

13 / 28

Creating a ggplot

library(ggplot2)
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))

14 / 28

Creating a ggplot

ggplot(data = <DATA>) +
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
  1. With ggplot2, you begin a plot with the function ggplot(). ggplot() creates a coordinate system that you can add layers to.

  2. You complete your graph by adding one or more layers to ggplot(). For example, geom_point() adds a layer of points to your plot, which creates a scatterplot. You can specify the color, size and shape of these points. Each geom function in ggplot2 takes a mapping argument.

15 / 28

Change the color, size and shape of points

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = 2, size = 3, shape = 18)

16 / 28

Facets

  • Another way, particularly useful for categorical variables, is to split your plot into facets, subplots that each display one subset of the data.
  • To facet your plot by a single variable, use facet_wrap(). The first argument should be a formula, which you create with ~ followed by a variable name.
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_wrap(~class, nrow = 2)

17 / 28

Facets

To facet your plot on the combination of two variables, add facet_grid() to your plot call.

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_grid(drv ~ cyl)

18 / 28

Pairwise plots

library(GGally)
ggpairs(subset(mtcars, select = c(1, 3, 4, 5, 6)))

19 / 28

Geometric objects

## left
p1 <- ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
## right
p2 <- ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy))
## put the two plots in one row
gridExtra::grid.arrange(p1, p2, ncol = 2)
20 / 28

Geometric objects

21 / 28

Geometric objects

  • A geom is the geometrical object that a plot uses to represent data.

  • Bar charts use bar geoms, line charts use line geoms, boxplots use boxplot geoms, and so on.

  • You can also add multiple geom functions.

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + geom_smooth(mapping = aes(x = displ, y = hwy))
22 / 28

Bar chart

The following chart displays the total number of cars in the mpg dataset, grouped by drv.

ggplot(data = mpg) + geom_bar(mapping = aes(x = drv, fill = drv))

23 / 28

Histogram

ggplot(data = mpg) + geom_histogram(mapping = aes(x = hwy))

24 / 28

Boxplots

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot()

25 / 28

Time series

library(ggplot2)
library(forecast)
autoplot(AirPassengers)

26 / 28

Summary

  • We have seen basic R plots and plots from ggplot2.

  • In this course, you will also use animated plots (e.g., gifs), interactive plots, etc.

27 / 28

References

R documentation of ggplot2.

28 / 28

"The simple graph has brought more information to the data analyst’s mind than any other device."

— John Tukey

2 / 28
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow