简体   繁体   English

R图中的白色边距使用plot.roc()函数

[英]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. 但是,我遇到了一个更基本的问题:我的R图中有一些空白区域,我想摆脱它。 This is using the package pROC . 这是使用包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. 如您所见,图像在y轴和绘图的最左边部分之间有很多空白区域,对于绘图的右侧也是如此。 The issue has only occurred when using plot.roc() . 只有在使用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. 我的R设置可能有问题,但在第二台计算机上运行代码时问题仍然存在。 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). 通常ROC曲线应该在单位平方内绘制,因此灵敏度和特异性占用相同的空间,使得可视化和比较更容易(通常ROC曲线用于显示特异性和灵敏度之间的权衡,并给予其中一个更多空间使这种比较更加困难)。

The pROC package does that by setting asp=1 in the internal call to plot.window . pROC包通过在plot.window的内部调用中设置asp=1plot.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): 设置asp=NA (或类似)并“释放”轴(请注意,您的ROC曲线不是单位正方形,而是单位矩形,因此可能更难以解释):

     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: 在调用plot.roc之前将图形参数pty更改为splot.roc使边距位于绘图之外:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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