在基础R图中组合图例和粘贴命令

时间:2015-11-19 15:02:45

标签: r legend paste

我想在我的情节中paste()一些文字进入图例,但我无法让R正确阅读c()paste()命令。

plot(1:10,1:10,type="n")

a <- 23
b <- 32

legend("topleft",inset=0.05,
       legend=c("1-15: n=[23]\nEstimated","15+: n=[32]\nAveraged"),
       lty=c(1,1),col=c("black","red"),bty="n",cex=1.5)

legend("topright",inset=0.05,
       legend=c(paste("1-15: n=[",a,"]\nEstimated","15+: n=[32]\nAveraged")),
       lty=c(1,1),col=c("black","red"),bty="n",cex=1.5)

所以左边的图例是手动创建的,我想要的输出是在右边创建相同的。

1 个答案:

答案 0 :(得分:1)

你很亲密。我相信这就是你想要的:

plot(1:10, 1:10, type="n")
a <- 23
b <- 32

legend("topright", inset = 0.05,
       legend = c(paste("1-15: n=[", a, "]\nEstimated"),
                  paste(" 15+: n=[", b, "]\nAveraged")),
       lty = c(1,1),
       col = c("black","red"), bty = "n", cex = 1.5)