Side-By-Side boxplots are used to display the distribution of several quantitative variables or a single quantitative variable along with a categorical variable.
# Data comes from the mtcars dataset
boxplot(mtcars$mpg ~ mtcars$gear, col="orange", main="Distribution of Gas Mileage", ylab="Miles per Gallon", xlab="Number of Gears")
# Data comes from the mtcars datasets.
boxplot(mtcars$mpg ~ mtcars$gear, col="violet", main="Distribution of Gas Mileage", xlab="Miles per Gallon", ylab="Number of Gears", horizontal=TRUE)
library(ggplot2)
mtcars$gear <- factor(mtcars$gear) # converts gear to a categorical variable
my.bp <<-ggplot(data=mtcars, aes(y= mpg, x=gear, fill=gear ) ) # Creates boxplots
my.bp <- my.bp + geom_boxplot() # Adds color
my.bp <- my.bp + ggtitle("Distribution of Gas Mileage") # Adds a title
my.bp <- my.bp + ylab("Miles per Gallon") + xlab("Number of Gears") # Adds kaveks
my.bp # displays the boxplots
You can rotate the previously created plot by adding the coord_flip()
arguement.
my.bp <- my.bp + coord_flip() # rotates the boxplot
my.bp
Mathematicss, Computer Science, and Statistics Department Gustavus Adolphus College