Skip to content

Plotting Basics

Meghan Balk edited this page Apr 5, 2018 · 16 revisions

xy Plot

The general formula is y ~ x. This is useful annotation as it is used in the lm() and anova() functions as well.

> plot(Sepal.Length ~ Sepal.Width, data = iris)

Exercise: (using the help file)

  • Make the points triangles rather than circles.
  • Make the lines dashed.
  • Create a title. Color it blue.
  • Add a legend.
  • Make a horizontal line

We can add on linear regressions too:

> model.iris <- lm(Sepal.Length ~ Sepal.Width, data = iris)
> abline(model.iris)

Pairs

> pairs(iris)

Discuss: What is the output of this function?

Histogram

> hist(iris$Petal.Length)

Exercise:

  • Log-transform the histogram

Box Plot

This is where factors come in handy!

boxplot(Petal.Width ~ Species, data = iris)

#reorder so that it the opposite way
iris$Species <- factor(iris$Species, levels = c("virginica", "versicolor", "setosa"))

Exercise:

What is the spread of petal widths per species? What is the median petal width per species?