我试图制作一个盒子情节,并在其中添加一个传奇:
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))但是,我不能订购x轴滴答。如果我想把订单改为4-3-5,我该怎么办?您还可以演示如何使用ggplot2完成此操作吗?我和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)在这种情况下,我无法添加线型图例,但是我能够更改x轴标签的顺序。
发布于 2014-11-10 14:11:24
如果您想要排序您的盒图并有一个离散变量,则需要将其转换为因素:
library(ggplot2)
ord <- c(4,3,5)
md.mpg <- median(mtcars$mpg)
bp <- ggplot(mtcars,aes(x = as.factor(gear), y = mpg))
bp+geom_boxplot(aes(colour = as.factor(gear))) +
scale_x_discrete(limits = as.factor(ord)) +
geom_hline(yintercept = md.mpg, linetype = 2) +
annotate("text", x = -Inf, y = md.mpg, label = sprintf("md = %.1f", md.mpg), vjust = -1.2, hjust = -0.2) +
geom_hline(yintercept = 25,linetype = 8) +
annotate("text", x = -Inf, y = 25, label = "value = 25", vjust = -1.2, hjust = -0.2)在上面的例子中,您没有不同的行类型,至少没有映射到数据的行类型,那么您在寻找什么样的图例呢?
https://stackoverflow.com/questions/26838700
复制相似问题