Sunday, December 9, 2012

R: Replicate plot with ggplot2 (part 3) boxplot

This is to replicate the boxplot from UCLA ATS: Examples of box plots

First read in the data:

hsb2 <- read.table('http://www.ats.ucla.edu/stat/r/modules/hsb2.csv', header=T, sep=",")
attach(hsb2)
view raw gistfile1.r hosted with ❤ by GitHub
The first is to draw the boxplot with no features

# boxplot
ggplot(hsb2)+geom_boxplot(aes(1, y=write))
view raw gistfile1.r hosted with ❤ by GitHub

The second is filling the box area with blue color.

# add colour
ggplot(hsb2)+geom_boxplot(aes(1, write), fill="blue")
view raw gistfile1.r hosted with ❤ by GitHub

The third is the box plot splited by a categorical variable ses:

# write ~ ses
ggplot(hsb2)+geom_boxplot(aes(factor(ses), write))
view raw gistfile1.r hosted with ❤ by GitHub

Next is to rename the levels of the categorical variable:

# change factor labels
hsb2$ses=factor(hsb2$ses, labels=c("low", "median", "high"))
ggplot(hsb2)+geom_boxplot(aes(factor(hsb2$ses), write))
view raw gistfile1.r hosted with ❤ by GitHub

The fifth one is to set notch = TRUE

# notch=T
ses=factor(ses, labels=c("low", "median", "high"))
ggplot(hsb2)+geom_boxplot(aes(factor(ses), write), notch=T, fill="blue")
view raw gistfile1.txt hosted with ❤ by GitHub





The sixth is the plot on the interaction levels of more than one categorical variable:

# two factors
ggplot(hsb2)+geom_boxplot(aes(factor(interaction(ses,female)), write))
view raw gistfile1.txt hosted with ❤ by GitHub




A little more, if we want to add outliers on the plot, it should be like:

# outlier
# ggplot(hsb2)+geom_boxplot(aes(1,write), outlier.colour = "green", outlier.size = 3)
view raw gistfile1.r hosted with ❤ by GitHub



No comments:

Post a Comment