First read in the data:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hsb2 <- read.table('http://www.ats.ucla.edu/stat/r/modules/hsb2.csv', header=T, sep=",") | |
attach(hsb2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# boxplot | |
ggplot(hsb2)+geom_boxplot(aes(1, y=write)) |
The second is filling the box area with blue color.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add colour | |
ggplot(hsb2)+geom_boxplot(aes(1, write), fill="blue") |
The third is the box plot splited by a categorical variable ses:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# write ~ ses | |
ggplot(hsb2)+geom_boxplot(aes(factor(ses), write)) |
Next is to rename the levels of the categorical variable:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# change factor labels | |
hsb2$ses=factor(hsb2$ses, labels=c("low", "median", "high")) | |
ggplot(hsb2)+geom_boxplot(aes(factor(hsb2$ses), write)) |
The fifth one is to set notch = TRUE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# notch=T | |
ses=factor(ses, labels=c("low", "median", "high")) | |
ggplot(hsb2)+geom_boxplot(aes(factor(ses), write), notch=T, fill="blue") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# two factors | |
ggplot(hsb2)+geom_boxplot(aes(factor(interaction(ses,female)), write)) |
A little more, if we want to add outliers on the plot, it should be like:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# outlier | |
# ggplot(hsb2)+geom_boxplot(aes(1,write), outlier.colour = "green", outlier.size = 3) |
No comments:
Post a Comment