Reputation: 391
How can we convert a box plot generated from
# Boxplot of MPG by Car Cylinders
boxplot(mpg~cyl,data=mtcars, main="Car Milage Data",
xlab="Number of Cylinders", ylab="Miles Per Gallon")
to an error plot (for 25th and 75th quantile) with minimum effort?
Upvotes: 2
Views: 383
Reputation: 3862
The link of @Keniajin sets you on the right track, but you will need functions for your quantiles. It's a ggplot solution:
require(ggplot2)
First, we create functions to calculate the quantiles25 en 75
Q25 <- function(x) {quantile(x, .25)}
Q75 <- function(x) {quantile(x, .75)}
Then we make the plot, using stat_summary
and these functions. Note that you can replace the function with median, min, max of stderr if you wish.
ggplot(data=mtcars, aes(x=cyl,y=mpg)) +
stat_summary(fun.y=mean,fun.ymin=Q25, fun.ymax=Q75) +
ggtitle("Car Milage Data") +
xlab("Number of Cylinders") +
ylab("Miles Per Gallon")
Upvotes: 2