0

I am trying to use the code below to plot many ROC plots on one plot. x is a list of vectors (each containing x-coordinates) and y is a list vectors (each containing y-coordinates).

plotROC<- function(x, y, label=NA) {
  p <- ggplot() + ggtitle("ROC Plot") + xlab("False Positive Rate") + 
    ylab("True Positive Rate") + xlim(c(0,1)) + ylim(c(0, 1)) + 
    geom_segment(aes(x=0, y=0, xend=1, yend=1), colour="black")
  for (i in 1:length(x)) {
    auc <- AUC(x[[i]], y[[i]])
    lab <- ifelse(is.na(label[i]), "", paste(label[i], "\n", sep=""))
    p <- p + geom_line(
      aes(x[[i]], y[[i]], colour=paste(lab, "AUC: ", 
                             formatC(auc, format="f", 
                                     digits=2), sep="")))
  }
  p + scale_color_brewer(palette=palette) + 
    theme(legend.title=element_blank())
}

For some reason the the function only plots the final ROC plot. If anybody could help I would be very appreciative!

Thanks,

Jack

  • The function does not print any plot at all. When the function returns, it returns the final plot. That is then automatically printed by the REPL loop. Call `print` explicitly on your plot objects if you want to see them. –  Apr 04 '18 at 15:18
  • Possible duplicate of [Generate multiple graphics from within an R function](https://stackoverflow.com/questions/2547306/generate-multiple-graphics-from-within-an-r-function) –  Apr 04 '18 at 15:20
  • At the moment I set `p <- plotROC(x=list(x1, x2), y=list(y1, y2))` and then type `p` into the console. It only shows `(x2, y2)`. I am not trying to print them in the function. –  Apr 04 '18 at 15:24
  • Maybe the problem is ```colour=paste(lab, "AUC: ", formatC(auc, format="f", digits=2), sep="")```. This is a label not a colour. For conding style, use ```return``` in your function, do not hesitate to back to the line, and add empty lines for readibility. Moreover, ```palette``` seems to be an argument of your function (or you shloud hardcoded that). – Thomas Apr 04 '18 at 16:01
  • Possible duplicate of ["for" loop only adds the final ggplot layer](https://stackoverflow.com/questions/26235825/for-loop-only-adds-the-final-ggplot-layer) – Calimo Apr 04 '18 at 18:31

1 Answers1

0

You should use data.frame to work with ggplot2, it will be far easier for you.

For example, you can easily convert your data into data.frame :

x = list(runif(4), runif(4))
y = list(runif(4), runif(4))

df = Reduce(rbind, mapply(function(x, y, z) {
  data.frame(x=x, y=y, z=z)
}, x, y, 1:length(x), SIMPLIFY = FALSE))
df$z = as.factor(df$z)

Note, that I add a z variable, that could correspond to whatever you want. I convert it into factor in order to ggplot consider it as factor and not continuous.

Then your plot can be define like this :

ggplot(df, aes(x=x, y=y, color=z)) + 
  geom_line() +
  ggtitle("ROC Plot") +
  xlab("False Positive Rate") + 
  ylab("True Positive Rate") + 
  xlim(c(0,1)) + ylim(c(0, 1)) + 
  geom_segment(aes(x=0, y=0, xend=1, yend=1), colour="black")

With the for loop (this is not a good idea to stay with a for loop), this code works for me :

library(ggplot2)

plotROC<- function(x, y, label=NA) {

  p <- ggplot() + 
    ggtitle("ROC Plot") +
    xlab("False Positive Rate") + 
    ylab("True Positive Rate") + 
    xlim(c(0,1)) + ylim(c(0, 1)) + 
    geom_segment(aes(x=0, y=0, xend=1, yend=1), colour="black")

  for (i in 1:length(x)) {
    p <- p + geom_line(
      aes(x[[i]], y[[i]]))
  }

  return(
    p + 
      scale_color_brewer(palette="YlOrRd") + 
      theme(legend.title=element_blank())
  )

}

x = list(runif(4), runif(4))
y = list(runif(4), runif(4))

plotROC(x, y)
Thomas
  • 1,164
  • 13
  • 41
  • Thank you for your reply. I will use dataframes in the future. Do you why my function won't work as I want it to though? As I wrote in earlier comment, I don't want to print the plots when I run the function. I will store the result of a function and then print that in the console. –  Apr 04 '18 at 15:43
  • I comment on your question above – Thomas Apr 04 '18 at 15:59
  • @Jack the reason is ggplot's lazy evaluation: https://stackoverflow.com/q/26235825/333599 – Calimo Apr 04 '18 at 18:33