Section 28 Numerics & Factor: geom_boxplot
28.1 Box plot: Example 1
data(iris)
# boxplot
g <- ggplot(data=iris, mapping=aes(x=Species, y=Sepal.Length))
g <- g + geom_boxplot()
g
# fill & colour
g <- ggplot(data=iris, mapping=aes(x=Species, y=Sepal.Length))
g <- g + geom_boxplot(fill='lightblue', colour='purple')
g
# labs
g <- ggplot(data=iris, mapping=aes(x=Species, y=Sepal.Length))
g <- g + geom_boxplot(fill='lightblue', colour='purple')
g <- g + labs(title='Boxplot of iris data',
x='Species',
y='Sepal Length (cm)')
g
# linetype, size, theme_bw
g <- ggplot(data=iris, mapping=aes(x=Species, y=Sepal.Length))
g <- g + geom_boxplot(fill='lightblue',
colour='purple',
linetype=5,
size=1.25)
g <- g + labs(title='Boxplot of iris data',
x='Species',
y='Sepal Length (cm)')
g + theme_bw()
28.2 Box plot: Example 2
Using aesthetics option aes
data(iris)
# Without fill
g <- ggplot(data=iris,
mapping=aes(x=Species, y=Sepal.Length))
g <- g + geom_boxplot(fill='lightblue', colour='purple')
g <- g + labs(title='Boxplot of iris data',
x='Species',
y='Sepal Length (cm)')
g
# With fill
g <- ggplot(data=iris,
mapping=aes(x=Species, y=Sepal.Length, fill=Species))
g <- g + geom_boxplot()
g <- g + labs(title='Boxplot of iris data',
x='Species',
y='Sepal Length (cm)')
g
# guides(fill=FALSE)
g <- ggplot(data=iris,
mapping=aes(x=Species, y=Sepal.Length, fill=Species))
g <- g + geom_boxplot() + guides(fill=FALSE)
g <- g + labs(title='Boxplot of iris data',
x='Species',
y='Sepal Length (cm)')
g
# coord_flip()
g <- ggplot(data=iris,
mapping=aes(x=Species, y=Sepal.Length, fill=Species))
g <- g + geom_boxplot() + guides(fill=FALSE) + coord_flip()
g <- g + labs(title='Boxplot of iris data',
x='Species',
y='Sepal Length (cm)')
g
# stat_summary
g <- ggplot(data=iris,
mapping=aes(x=Species, y=Sepal.Length, fill=Species))
g <- g + geom_boxplot() + guides(fill=FALSE)
g <- g + stat_summary(fun.y=mean, geom='point', shape=16, size=4, colour='yellow')
g <- g + labs(title='Boxplot of iris data',
x='Species',
y='Sepal Length (cm)')
g + theme_bw()
28.3 Box plot: Example 3
data(mpg)
Prepare a box plot of
cty
for each level ofcyl
Fill the box with your own choice of colour
Remove the legend