rggplot2roc

Plotting lines behind ROC curves


I am trying to plot ROC curves with ggroc and the 45 degree line as well as a box around the curves:

ggroc(list(roc1, roc2, roc3)) +
  scale_color_manual(
    values = c("red", "blue", "yellow"),
    legacy.axes = T) +
  geom_segment(
    aes(x = 0, xend = 1, y = 0, yend = 1),
    linetype = "dashed",
    color = "grey") +
  geom_rect(
    aes(xmin = 0, xmax = 1, ymin = 0, ymax = 1),
    color = "grey", fill = NA)

Basically my data shows that the test I´m plotting the curves for doesn´t work so the ROC lines are partially crossing the 45 degree line and are obscured at the beginning and the end by the geom_rect.

I tried annotate, custom_annotation and after_stat = FALSE with the geom objects and couldn´t find any other promising solutions. Is there a way to get the line and the box behind the ROC lines that keeps ggroc? It would probably be possible to rebuild the plot with ggplot and change the order of the layers but I would be happier with a ggroc solution.


Solution

  • Not sure whether this works for ggroc. But in general you can change the order of the geom layers by manipulating the ggplot object.

    Here is a fake example where I add a geom_segment and geom_rect on top of a geom_point and save the ggplot as a variable p:

    library(ggplot2)
    
    p <- ggplot(data.frame(
      x = seq(0, 1, length.out = 3),
      y = seq(0, 1, length.out = 3)
    ), aes(x, y)) +
      geom_point() +
      geom_segment(
        aes(x = 0, xend = 1, y = 0, yend = 1),
        linetype = "dashed",
        color = "grey"
      ) +
        geom_rect(
          aes(xmin = 0, xmax = 1, ymin = 0, ymax = 1),
          color = "grey", fill = "white"
        )
    
    p
    

    Now we can change the order of the geom layers by rearranging the list of layers like so, e.g. putting the geom_segment and the geom_point on top of the geom_rect:

    p$layers <- p$layers[c(3, 2, 1)]
    
    p
    

    To figure out the order of the layers you can simply print p$layers:

    p$layers
    #> [[1]]
    #> geom_point: na.rm = FALSE
    #> stat_identity: na.rm = FALSE
    #> position_identity 
    #> 
    #> [[2]]
    #> mapping: x = 0, y = 0, xend = 1, yend = 1 
    #> geom_segment: arrow = NULL, arrow.fill = NULL, lineend = butt, linejoin = round, na.rm = FALSE
    #> stat_identity: na.rm = FALSE
    #> position_identity 
    #> 
    #> [[3]]
    #> mapping: xmin = 0, xmax = 1, ymin = 0, ymax = 1 
    #> geom_rect: linejoin = mitre, na.rm = FALSE
    #> stat_identity: na.rm = FALSE
    #> position_identity