ENV221 L04

Author

Peng Zhao

1 Overview of the module and R

2 R Basic Operations

3 R Programming

4 Statistical Graphs

4.1 Learning objectives

In this lecture, you will

  • learn the usage of scatter plots, pie charts, bar plots, box plots and other graphic types,
  • learn the usage of the R ggplot2 package, and
  • use these graphics to visualize and analyze data.

4.2 Why graphs

Data visualization is the presentation of data in a pictorial or graphical format.

  • Using charts or graphs to visualize large amounts of complex data is easier than poring over spreadsheets or reports (example).
  • Grasp difficult concepts or identify new patterns (example data, example figure).
  • With interactive visualization, you can take the concept a step further by using technology to drill down into charts and graphs for more detail, interactively changing what data you see and how it’s processed (example).

4.3 Graph components

  • Graph area

    • Plotting area
    • Axes
    • Legends
  • Figure number + caption (not required in this module)

4.4 Basic functions for graphing

4.4.1 High level functions

x <- 1:10
plot(x)

plot(iris$Sepal.Length)

plot(iris$Sepal.Length, iris$Sepal.Width)

barplot(table(iris$Species))

hist(iris$Sepal.Length)

boxplot(iris$Sepal.Length)

pairs(iris[, 1:4])

4.4.2 Control the details

Color:

plot(iris$Sepal.Length, col = "red")

plot(iris$Sepal.Length, col = ifelse(iris$Species == "setosa", "red", "blue"))

colors()
beginr::plotcolors()

Shape:

plot(iris$Sepal.Length, pch = 2)

beginr::plotpch()

Size:

plot(iris$Sepal.Length, cex = 2)

Type:

plot(iris$Sepal.Length, type = "l")

beginr::plottype()

4.4.3 Add more components

lines()
points()
abline()
arrows()
axis()
legend()
text()
mtext()
expression()
beginr::plotlty()
beginr::plotcolorbar()

4.5 The ggplot2 package

4.5.1 Overview

What can ggplot2 do?

4.5.2 Template

## mandatory:
ggplot() + geom_xxx() + 
# optional
  scale_xxx() + 
  coordinate_xxx() + 
  facet_xxx() + 
  theme() + 
  ... 

4.5.3 Components

  • point: geom_point()
  • bar: geom_bar() geom_col()
  • line: geom_line() geom_abline()
  • box: geom_boxplot()
  • text: geom_text() geom_label()

4.5.4 Scatterplot

Basic: ggplot() + geom_point()

library(ggplot2)
ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width))

Color:

ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width), color = "blue")

ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width, color = Species))

ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width, color = "blue"))

Size:

ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width), size = 3)

ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width, size = Species))

Shape:

ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width), shape = 2)

# beginr::plotpch()
ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width, shape = Species))

Smooth:

ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_smooth(aes(x = Sepal.Length, y = Sepal.Width))

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  geom_smooth(method = "lm")

Facet:

ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width)) + 
  facet_wrap(~ Species)

Theme:

ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width)) + 
  theme_light()

4.5.5 Bar plot

Basic:

ggplot(iris) + 
  geom_bar(aes(Species))

Coordinate systems:

ggplot(iris) + 
  geom_bar(aes(Species)) +
  coord_flip()

4.5.6 Histogram

ggplot(iris) + geom_histogram(aes(Sepal.Length))

4.5.7 Boxplot

Basic:

ggplot(iris) + geom_boxplot(aes(Sepal.Length))

4.5.8 Pair plot

# install.packages("GGally")
library(GGally)
ggpairs(iris, aes(colour=Species, alpha=0.5))

4.6 The plotly package

library(plotly)
plot_ly(x = iris$Sepal.Length, y = iris$Sepal.Width)
p1 <- ggplot(iris) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width, color = Species))
ggplotly(p1)
p2 <- ggpairs(iris, aes(colour=Species, alpha=0.5))
ggplotly(p2)

4.7 The rgl package

x <- seq(-10, 10, length = 20)
y <- x
f <- function(x, y) { r <- sqrt(x^2 + y^2); 10 * sin(r)/r }
z <- outer(x, y, f)
z[is.na(z)] <- 1

library(rgl)
persp3d(x, y, z, col = "darkgreen")

4.7.1 Advanced

msg("mtcars-smooth")

Stats:

msg("diamonds-hex")
msg("ggplot2-violin")

Scale:

msg("iris-ggplot2")
msg("quake6-bubbles")

Coordinates:

msg("diamonds-cut")
msg("diamonds-polar")

4.8 Save graphs

  1. In RStudio, click Plots - Export - Save as PDF. Or
  2. Use functions:
pdf('df_plot.pdf')
plot(iris$Sepal.Length)
dev.off()
ggpairs(iris, aes(colour=Species, alpha=0.5))
ggsave("df_ggpairs.pdf")

4.9 Readings