Home | Prev | Next

Base R {.tabset}

Intro

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

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)

Diagnostic plots

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

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

ggplot2 {.tabset}

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()

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
  1. (Optional) Alter coordinate system
p <- p + coord_flip()
p
  1. (Optional) Facet the chart
p <- p + facet_grid(.~Species)
p
  1. (Optional) Amend look and feel
p <- p + theme_minimal()
p

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()

Exercises

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

Answers

library(ggplot2)
ggplot(iris,aes(x=Sepal.Width))+
  geom_histogram()+
  facet_wrap(~Species)

More resources



stephlocke/RMSFTDP documentation built on May 30, 2019, 3:36 p.m.