rplotlegendpar

Add common legend to multiple pROC plots


I've plotted multiple ROC curves with pROC and now I want to add a common legend horizontally across the bottom-centre of the plot space. I typically use ggplot and now I'm lost with base R plotting.

Here's my code:

# Set plotting parameters
par(mfrow=c(1,2), pty = "s", oma = c(4,1,1,1))

# ROC plot 1
roc(logit_A$y, logit_A$fitted.values, plot=TRUE, legacy.axes=FALSE, percent=TRUE, col="salmon", lwd=2, print.auc=TRUE)
plot.roc(logit_B$y, logit_B$fitted.values, percent=TRUE, col="goldenrod", lwd=2, print.auc=TRUE, add=TRUE, print.auc.y=40)
plot.roc(logit_C$y, logit_C$fitted.values, percent=TRUE, col="lightsteelblue", lwd=2, print.auc=TRUE, add=TRUE, print.auc.y=30)
title(main = "Model 1", line = 2.5)

# ROC plot 2
roc(logit_A2$y, logit_A2$fitted.values, plot=TRUE, legacy.axes=FALSE, percent=TRUE, col="salmon", lwd=2, print.auc=TRUE)
plot.roc(logit_B2$y, logit_B2$fitted.values, percent=TRUE, col="goldenrod", lwd=2, print.auc=TRUE, add=TRUE, print.auc.y=40)
plot.roc(logit_C2$y, logit_C2$fitted.values, percent=TRUE, col="lightsteelblue", lwd=2, print.auc=TRUE, add=TRUE, print.auc.y=30)
title(main = "Model 2", line = 2.5)

# Add legend
legend("bottom",
       legend=c("A", "B", "C"),
       col=c("salmon", "goldenrod", "lightsteelblue"),
       lwd=4, cex =0.4, xpd = TRUE, horiz = TRUE)

enter image description here How do I make it so the legend is centered along the bottom between both plots?


Solution

  • Do you mean something like this :

      m <- matrix(c(1,2,3,3),nrow = 2,ncol = 2,byrow = TRUE)
    
    layout(mat = m,heights = c(0.4,0.4))
    
    for (i in 1:2){
      par(mar = c(2,2,1,1))
      plot(runif(5),runif(5),xlab = "",ylab = "")
    }
    
    
    par(mar=c(0,0,1,0))
    plot(1, type = "n", axes=FALSE, xlab="", ylab="")
    plot_colors <- c("blue","green","pink")
    legend(x = "top",inset = 0,
           legend = c("AUC:72.9%", "AUC: 71.0%", "AUC:80.0%"), 
           col=plot_colors, lwd=7, cex=.7, horiz = TRUE)
    

    enter image description here