简体   繁体   中英

White margins within R plot using plot.roc() function

I was trying to create a figure similar to one on a website. However, I encountered a more basic issue: there is some white space within my R plot that I would like to get rid of. This is using the package pROC . I've included a reproducible example that illustrates the issue:

library("pROC")
plot.roc(c(1, 1, 0, 0, 1), c(3, 4, 5, 6, 7), legacy.axes=TRUE)

The above code ends up looking like the following image:

plot.roc()问题:可重现的示例#1输出

As you can see, the image has a lot of white space between the y-axis and the left-most part of the plot, and similarly for the right side of the plot. The issue has only occurred when using plot.roc() . I even tried making the plot that I was trying to emulate (using the provided code on the website), and still ended up with a different image (code for the image described in the first paragraph is included below):

library(pROC)
data(aSAH)

rocobj1 <- plot.roc(aSAH$outcome, aSAH$s100, main="Statistical comparison", percent=TRUE, col="#1c61b6")
rocobj2 <- lines.roc(aSAH$outcome, aSAH$ndka, percent=TRUE, col="#008600")
testobj <- roc.test(rocobj1, rocobj2)

text(50, 50, labels=paste("p-value =", format.pval(testobj$p.value)), adj=c(0, .5))

legend("bottomright", legend=c("S100B", "NDKA"), col=c("#1c61b6", "#008600"), lwd=2)

plot.roc()问题:可重现的示例#2输出

There is no white space in the original picture that used the exact same code .

There may be something wrong with my R settings, though the issues persisted when the code was run on a second computer. Is anyone able to assist?

The answer has been given in in comments to the question, but I think it is worth writing it down in a proper answer. It should also be noted that both you and the linked example omitted to specify anything about the graphical device used to save the plot, so saying that you "used the exact same code" is a bit misleading.

Typically ROC curves should be plotted within the unit square, so that the sensitivity and specificity take up the same space, making the visualization and comparison easier (typically ROC curves serve to display the tradeoff between specificity and sensitivity and giving one of them more space would make this comparison more difficult).

The pROC package does that by setting asp=1 in the internal call to plot.window . You have two options to proceed:

  1. Set asp=NA (or similar) and "free" the axis (note that your ROC curve will not be a unit square but in a unit rectangle, making it potentially more difficult to interpret):

     rocobj1 <- plot.roc(aSAH$outcome, aSAH$s100, asp = NA) 

    用asp = NA的ROC曲线

  2. Changing the graphical parameter pty to s before calling plot.roc so that the margins are outside of the plot:

     par(pty = "s") rocobj1 <- plot.roc(aSAH$outcome, aSAH$s100) 

    带有par(pty =“s”)的ROC曲线

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM