在r情节中重叠的传说

时间:2018-02-02 14:48:57

标签: r plot graphics

我已尝试使用此代码,但图例与图表重叠。我想把图例放在图片的底部(以图为中心),我也想调整图例中示例线的大小,因为它也会重叠文本。

plot(Portfolio.z, plot.type ="single", main ="Portfolio",
          col =c("red", "blue", "green", "black", "yellow", "purple"),
          lty =c("solid", "solid", "solid", "solid", "solid", "solid"),
          lwd =2, ylab = "Price")
par(xpd=TRUE)
legend("bottom",legend = c("JP Morgan", "Goldman Sachs",
          "BP", "ExxonMobil", "Lockheed Martin", "Boeing"),
          text.width = min(sapply(c("JP Morgan", "Goldman Sachs",
          "BP", "ExxonMobil", "Lockheed Martin", "Boeing"), strwidth)),
          col=c("red", "blue", "black", "green", "purple",
          "yellow"), lwd=5, horiz = TRUE)

enter image description here

1 个答案:

答案 0 :(得分:0)

您应该阅读图例的手册页 - ?legend:您可以完全控制展示位置和字体大小的各个方面。首先,除非删除x轴标签或增加底部的边距(?par,参数mar=),否则x轴下方的图例没有空间。图例中的inset=参数会调整位置,因此类似inset=c(0, -.15)的内容会将其置于轴下方,但您可能需要调整-15向上或向下。您还需要包含xpd=TRUE,以便R知道将图例放在图表下方的边距中是可以的。最后,cex=控制文本的扩展,默认值为1:

plot(NA, xlim=c(0, 1), ylim=c(0, 1), xlab="")
legend("bottom", "A very long legend under the plot",
    xpd=TRUE, inset=c(0, -.15), cex=.8)

NSKeyedArchiver.setClassName(_:for:)