ENV221 L04
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
<- 1:10
x 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()
::plotcolors() beginr
Shape:
plot(iris$Sepal.Length, pch = 2)
::plotpch() beginr
Size:
plot(iris$Sepal.Length, cex = 2)
Type:
plot(iris$Sepal.Length, type = "l")
::plottype() beginr
4.4.3 Add more components
lines()
points()
abline()
arrows()
axis()
legend()
text()
mtext()
expression()
::plotlty()
beginr::plotcolorbar() beginr
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)
<- ggplot(iris) +
p1 geom_point(aes(x = Sepal.Length, y = Sepal.Width, color = Species))
ggplotly(p1)
<- ggpairs(iris, aes(colour=Species, alpha=0.5))
p2 ggplotly(p2)
4.7 The rgl package
<- seq(-10, 10, length = 20)
x <- x
y <- function(x, y) { r <- sqrt(x^2 + y^2); 10 * sin(r)/r }
f <- outer(x, y, f)
z is.na(z)] <- 1
z[
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
- In RStudio, click Plots - Export - Save as PDF. Or
- 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
- R for Data Science, Chapter 3 and 28