This lesson is being piloted (Beta version)

Box plots and Violin plots

Overview

Teaching: 20 min
Exercises: 0 min
Questions
  • What is box plot

Objectives
  • How to draw a nice box plot

Boxplot

Here I am plotting the boxplot of highway (hwy) fuel consumption (mpg) for each manufacturer.

ggplot(mpg,aes(x=manufacturer,y=hwy,
               fill=factor(manufacturer)))+
  geom_boxplot()+
  geom_jitter()+
  labs(title="Boxplot for Hwy per manufacturer",x="Manufacturer",y="Highway milage")+
  theme_bw()+coord_flip()+
  theme(legend.position = "none")

image

Violin plot

g <- ggplot(mpg, aes(class, cty))
g + geom_violin(aes(fill=class)) + 
  labs(title="Violin plot", 
       subtitle="City Mileage vs Class of vehicle",
       caption="Source: mpg",
       x="Class of Vehicle",
       y="City Mileage")

image

Key Points

  • Box plot

  • Violin plot