如何为位于绘图区域外的以下代码添加图例。 这是一个可预测的代码:
par(pty="s")
library(ROCR)
data(ROCR.simple)
pred <- prediction( ROCR.simple$predictions, ROCR.simple$labels )
pred2 <- prediction(abs(ROCR.simple$predictions +
rnorm(length(ROCR.simple$predictions), 0, 0.1)),
ROCR.simple$labels)
perf <- performance(pred, "tpr", "fpr" )
perf2 <- performance(pred2, "tpr", "fpr")
# Plot pred 1
plot(perf, col="red")
# plot pred 2
plot(perf2, add = TRUE, col="blue")
如有任何建议,我们将不胜感激。谢谢!
最佳答案
您可以执行此操作的一种方法是使用 par
来增加顶部的边距,并允许在绘图区域之外进行书写。然后,您可以使用带有负 inset
的 legend
。
## Your graph
par(mar=c(5.1,4.1,6,2.1), xpd=TRUE)
plot(perf, col="red")
plot(perf2, add = TRUE, col="blue")
## Add Legend
legend("topright", c("Pred1", "Pred2"), lty=1,
col = c("red", "blue"), bty="n", inset=c(0,-0.15))
关于r - 向情节添加图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46186850/