R plot:在图例中显示点类型和线型

G11*_*24E 6 plot r legend

我已经生成了一个带plot功能的情节.我添加了点标记pch()和行类型lty.在图例部分,我想将点和线合并在一起.我用过,merge=TRUE但没用.它仅显示线型.同样的事情merge=FALSE.在这两种情况下,框图例框宽度只有轻微的变化.这就对了.任何的想法?

以下是示例代码:

m<-1:10
n<-runif(1:5)

plot(m,type = "o", col="blue",main = "plot",xlab = "distance",ylab = "height")
lines(n+2,type="o", pch=22,lty=6,col="red")
lines(m-3,type="o", pch=17,lty=5,col="forestgreen")

legend(x=2,y=8,c("R","S","T"),lty=c(1,6,5),pch=c(5,22,17),
       cex=.8, col=c("blue","red","forestgreen"),merge = FALSE)
Run Code Online (Sandbox Code Playgroud)

李哲源*_*李哲源 10

人们要求你输入一些玩具代码.这很重要,因为它为人们提供了一个帮助你的起点.实际上这并不难.考虑以下:

set.seed(0); x1 <- rnorm(10); x2 <- rnorm(10); x3 <- rnorm(10)
plot(x1, type = "b", pch = 19, lty = 1, col = 1,
     ylim = range(c(x1,x2,x3)))  ## both points and lines
points(x2, pch = 19, col = 2)  ## only points
lines(x3, lty = 2, col = 3)  ## only lines
legend(6, 0.9*max(c(x1,x2,x3)), legend = c("x1", "x2", "x3"),
       pch = c(19, 19, NA), lty = c(1, NA, 2),
       col = c(1,2,3), text.col = c(1,2,3))
Run Code Online (Sandbox Code Playgroud)

测试

使用NA控制要显示的内容.


跟进

我忘了包括pchlegend().当我包括该点显示在图例中每行的右端时.有没有办法集中他们?

大!现在,您已将代码包含在问题中.问题出在你的最后一次电话会议上legend().不要使用/ set参数merge:

legend(x=2,y=8,c("R","S","T"),lty=c(1,6,5),pch=c(5,22,17),
       cex=.8, col=c("blue","red","forestgreen"))
Run Code Online (Sandbox Code Playgroud)

这将做你想要的.