Home | Prev | Next

1 Base R

1.1 Intro

R has built-in graphics capabilities but these take quite a bit of work to get your head around.

1.2 pairs

Pairs is a pair-wise scatter plot - this is very handy for providing a visual inspection of the data and showing any correlations or clusters of data.

pairs(iris)

1.3 Diagnostic plots

some models have methods for producing diagnostic plots for visual inspection.

plot(lm(Sepal.Length~Petal.Length, iris))

2 ggplot2

2.1 Glossary

Term Explanation Example(s)
plot A plot using the grammar of graphics ggplot()
aesthetics attributes of the chart colour, x, y
mapping relating a column in your data to an aesthetic
statistical transformation a translation of the raw data into a refined summary stat_density()
geometry the display of aesthetics geom_line(), geom_bar()
scale the range of values axes, legends
coordinate system how geometries get laid out coord_flip()
facet a means of subsetting the chart facet_grid()
theme display properties theme_minimal()

2.2 Charts - step by step

  1. Create the base plot (doesn’t work on it’s own)
library(ggplot2)

p <- ggplot(data=iris)
  1. Add aesthetic mappings (doesn’t work on it’s own)
p <- ggplot(data=iris, aes(x=Sepal.Width, y=Sepal.Length, colour=Species))
  1. Add a geometry
p <- p + geom_point()
p

  1. (Optional) Add a statistic
p <- p + stat_boxplot(fill="transparent")
p
## Warning: position_dodge requires non-overlapping x intervals

  1. (Optional) Alter coordinate system
p <- p + coord_flip()
p
## Warning: position_dodge requires non-overlapping x intervals

  1. (Optional) Facet the chart
p <- p + facet_grid(.~Species)
p

  1. (Optional) Amend look and feel
p <- p + theme_minimal()
p

2.3 Constructing a chart - a one-step process

ggplot(data=iris, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) + 
  geom_point() +
  stat_boxplot(fill="transparent") +
  # coord_flip() + # Commented out
  facet_grid(.~Species) +
  theme_minimal()

2.4 Exercises

  1. Construct a chart showing a histogram of iris$Sepal.Width split by species

2.5 Answers

library(ggplot2)
ggplot(iris,aes(x=Sepal.Width))+
  geom_histogram()+
  facet_wrap(~Species)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

2.6 More resources