7

I am using the following code to plot the ROC curve after having run the logistic regression.

fit1 <- glm(formula=GB160M3~Behvscore, data=eflscr,family="binomial", na.action = na.exclude)
prob1=predict(fit1, type=c("response"))
eflscr$prob1 = prob1

library(pROC)
g1 <- roc(GB160M3~prob1, data=eflscr, plot=TRUE,  grid=TRUE, print.auc=TRUE)

The ROC curves plotted look like this (see link below)

enter image description here

  1. The x-axis scale does not fill the who chart.
  2. How can I change the x axis to report 1 - specifically?
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Manfred
  • 147
  • 2
  • 10
  • Note that question 1 is addressed in more details specifically in https://stackoverflow.com/questions/42057979/proc-roc-curves-remove-empty-space – Calimo Sep 04 '20 at 14:33

2 Answers2

7
  1. By default pROC sets asp = 1 to ensure the plot is square and both sensitivity and specificity are on the same scale. You can set it to NA or NULL to free the axis and fill the chart, but your ROC curve will be misshaped.

    plot(g1, asp = NA)
    

    Using par(pty="s") as suggested by Joe is probably a better approach

  2. This is purely a labeling problem: note that the x axis goes decreasing from 1 to 0, which is exactly the same as plotting 1-specificity on an increasing axis. You can set the legacy.axes argument to TRUE to change the behavior if the default one bothers you.

    plot(g1, legacy.axes = TRUE)
    
Calimo
  • 7,510
  • 4
  • 39
  • 61
1

A good shortcut to getting a square plot is to run the following before plotting:

par(pty="s")

This forces the shape of the plot region to be square. Set the plotting region back to maximal by simply resetting the graphics device and clearing the plot.

dev.off()

As pointed out by @Calimo, there is the legacy.axes argument to reverse the x-axis and the label is also changed automatically. You can run ?plot.roc to see all the pROC plotting options.

Example

# Get ROC object
data(aSAH)
roc1 <- roc(aSAH$outcome, aSAH$s100b)

# Plot
par(pty="s")
plot(roc1, grid = TRUE, legacy.axes = TRUE)

# Reset graphics device and clear plot
dev.off()
Joe
  • 8,073
  • 1
  • 52
  • 58
  • Thank you Joe. Let me try this – Manfred Dec 14 '16 at 10:45
  • One additional question. How do I change the actual scale. At the moment it it in descending order from 1 to 0. I want it from 0 to 1. – Manfred Dec 14 '16 at 13:45
  • 1
    The x axis in the OP's figure really *is* the specificity. Labeling it 1-Sp would be misleading without also reversing the axis. – Calimo Apr 24 '17 at 12:29
  • Agreed with@Calimo. Just forcing the label to change would be misleading. Never do that. Rather change the scale from 0 to 1 (increasing order) by using legacy. axis argument. – Dr Nisha Arora May 29 '21 at 05:00
  • legacy. axis takes value TRUE or FALSE(Default) indicating if the specificity axis (x axis) must be plotted as as decreasing “specificity” (FALSE, the default) or increasing “1 - specificity” (TRUE) as in most legacy software. This affects only the axis, not the plot coordinates – Dr Nisha Arora May 29 '21 at 05:01
  • @Joe, please also tell how to reset the plot setting after using par(pty="s") for this plot? – Dr Nisha Arora May 29 '21 at 05:04
  • `par(pty="m")`. I've added it to the answer above. – Joe May 30 '21 at 08:28
  • Actually you can also just reset the graphics device `dev.off()` as well. – Joe May 30 '21 at 09:26