gpt4 book ai didi

R图: Displaying both point type and line type in legend

转载 作者:行者123 更新时间:2023-12-04 13:47:28 24 4
gpt4 key购买 nike

我已经用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)

最佳答案

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

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))

test

使用 NA来控制您要显示的内容。

后续

I forgot to include the pch in the legend(). When I included that the point are displaying at the right tip of each line in the legend. Is there some way of centring them?



伟大的!现在,您已将代码包含在问题中。问题在于您对 legend()的最终调用。不要使用/设置参数 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"))

这将做您想要的。

关于R图: Displaying both point type and line type in legend,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37897984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com