CSC 360 Module 5 Lecture Notes

Harold Nelson

April 14, 2016

library(ggplot2)

Scatterplots in base and ggplot2

plot(iris$Sepal.Width,iris$Petal.Length,main="base Version")

ggplot(data = iris,aes(x=Sepal.Width,y=Petal.Length)) +
  geom_point() +
  ggtitle("ggplot2 Version")

Enhance Scatterplots in ggplot2 with aesthestics

#qplot(Sepal.Width,Petal.Length,data=iris,
#      color=Species,main="qplot")
ggplot(data = iris,
       aes(x=Sepal.Width,y=Petal.Length,
           color=Species)) +
  geom_point() +
  ggtitle("ggplot with Color")

#qplot(Sepal.Width,Petal.Length,data=iris,
#      shape=Species,main="qplot")

ggplot(data = iris,
       aes(x=Sepal.Width,y=Petal.Length,
           shape=Species)) +
  geom_point() +
  ggtitle("ggplot with Shape")

Enhance Scatterplots in ggplot2 with facets

ggplot(data = iris,
       aes(x=Sepal.Width,y=Petal.Length,
           color=Species)) +
  geom_point() +
  ggtitle("ggplot with Color and Facet") +
  facet_wrap(~Species)

Aesthestics Examples using mpg from ggplot2

These examples use the simple qplot function available in the ggplot2 package.

qplot(displ,hwy,data=mpg)

qplot(displ,hwy,data=mpg,color=class)

qplot(displ,hwy,data=mpg,shape=class)
## Warning: The shape palette can deal with a maximum of 6 discrete values
## because more than 6 becomes difficult to discriminate; you have 7.
## Consider specifying shapes manually if you must have them.
## Warning: Removed 62 rows containing missing values (geom_point).
## Warning: The shape palette can deal with a maximum of 6 discrete values
## because more than 6 becomes difficult to discriminate; you have 7.
## Consider specifying shapes manually if you must have them.

qplot(displ,hwy,data=mpg,color=class,size=cyl)

Facet Examples from mpg

qplot(displ,hwy,data=mpg) +
  facet_grid(.~cyl)

qplot(displ,hwy,data=mpg) +
  facet_grid(drv~.)

qplot(displ,hwy,data=mpg) +
  facet_grid(drv~cyl)

qplot(displ,hwy,data=mpg,color=class) +
  facet_grid(drv~cyl)

qplot(displ,hwy,data=mpg) +
  facet_wrap(~class)

qplot(displ,hwy,data=mpg) +
  facet_grid(.~class)

qplot(displ,hwy,data=mpg) +
  facet_grid(class~.)

Scatterplots, etc, with alternative geoms

qplot(displ,hwy,data=mpg)

qplot(displ,hwy,data=mpg,geom="point") # Default with 2 variables

# Use the transparency parametet
qplot(cty,hwy,data=mpg,geom="point",alpha=I(1/10))

qplot(displ,hwy,data=mpg,geom="smooth")

qplot(displ,hwy,data=mpg,geom="density_2d")

qplot(displ,hwy,data=mpg,geom=c("point","smooth"))

qplot(displ,hwy,data=mpg,geom=c("point","density_2d"),alpha=I(1/10))

Categorical Independent and Quantitative Dependent

qplot(class,hwy,data=mpg)

qplot(class,hwy,data=mpg,geom="boxplot")

qplot(reorder(class,hwy),hwy,data=mpg,geom="boxplot")

qplot(reorder(class,hwy,FUN=median),hwy,data=mpg,geom="boxplot")

# Throw in a facet

qplot(reorder(class,hwy,FUN=median),hwy,data=mpg,geom="boxplot")+facet_wrap(~drv)

Distribution of a Categorical Variable

qplot(cut,data=diamonds)

qplot(color,data=diamonds,geom="bar",fill=cut)

qplot(cut,data=diamonds,geom="bar",fill=color)

Histograms of a Quantitative Variable

qplot(carat,data=diamonds,binwidth=1)

qplot(carat,data=diamonds,binwidth=.1)

qplot(carat,data=diamonds,binwidth=.01)

Frequency Distribution of a Quantitative Variable According to a Categorical Variable.

qplot(depth,data=diamonds)+facet_wrap(~cut)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qplot(depth,data=diamonds,color=cut)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qplot(depth,data=diamonds,fill=cut)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qplot(depth,data=diamonds,geom="freqpoly",color=cut)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qplot(depth,data=diamonds,geom="density",color=cut)