I am trying to make a boxplot and also add a legend to it:
boxplot(mpg~factor(gear),data=mtcars,par(las=2),range=0,col=rainbow(3))
abline(h=median(mtcars$mpg),lty=3)
abline(h=25,lty=6)
legend("bottomright",c("Median mileage","Mileage@25"),lty=c(3,6))
However, I am not being able to order the x-axis ticks. What do I do if I want to change the order to 4-3-5? Can you also show how to do this using ggplot2? My trial with ggplot2:
bp <- ggplot(mtcars,aes(x=gear,y=mpg))
order <- c(4,3,5)
bp+geom_boxplot(aes(colour=gear))+scale_x_discrete(limits=order)+geom_hline(yintercept=median(mtcars$mpg),linetype=2)+geom_hline(yintercept=25,linetype=8)
I am not being able to add the linetype legend in this case, however am able to change the order of the x-axis labels.