繁体   English   中英

线型图例

[英]Legends of linetypes

我正在尝试制作箱形图,并向其中添加图例:

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轴标签的顺序。

如果要订购箱形图并具有离散变量,则需要转换为系数:

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)

在上面的示例中,您没有不同的线型,至少没有线型映射到数据,那么您要寻找哪种图例?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM