Home > Software engineering >  ggplot: How to color/fill area between ROC curves and diagonal?
ggplot: How to color/fill area between ROC curves and diagonal?

Time:06-04

I have this ROC curve

enter image description here

Written with this code:

ggplot(a, aes(y = TPR, x = FPR, color = model))  
  geom_line()  
  geom_segment(aes(y = 0, yend = 1, x = 0, xend = 1), color = "grey50") 

I want to color the space between red and green curve, and the area between the green curve and the diagonal.

I tried to color the expected output manually in free hand (my apologies for the artistic skills)

enter image description here

I sought solutions using geom_area() but could not get it work.

How can I fill these area?

Here is my data sample. My apologies for many datapoints, but that was the only way I could reproduce "the full curves" reaching (0,0) and (1,1).

a <- structure(list(model = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
                                   3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                   3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                   3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                   3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                   3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c("Null model", 
                                                                                           "SSA ", "SSA-"), class = "factor"), risk = c(1, 1, 1, 1, 1, 0.99, 
                                                                                                                                        0.99, 0.99, 0.98, 0.98, 0.97, 0.97, 0.97, 0.96, 0.95, 0.95, 0.94, 
                                                                                                                                        0.93, 0.92, 0.91, 0.91, 0.91, 0.91, 0.9, 0.89, 0.89, 0.88, 0.87, 
                                                                                                                                        0.87, 0.85, 0.85, 0.81, 0.81, 0.8, 0.78, 0.77, 0.76, 0.76, 0.76, 
                                                                                                                                        0.76, 0.75, 0.74, 0.72, 0.69, 0.69, 0.69, 0.67, 0.66, 0.65, 0.65, 
                                                                                                                                        0.64, 0.63, 0.63, 0.6, 0.59, 0.58, 0.58, 0.57, 0.57, 0.57, 0.53, 
                                                                                                                                        0.53, 0.52, 0.5, 0.46, 0.46, 0.46, 0.45, 0.44, 0.42, 0.41, 0.4, 
                                                                                                                                        0.4, 0.39, 0.38, 0.37, 0.35, 0.31, 0.29, 0.27, 0.27, 0.26, 0.24, 
                                                                                                                                        0.23, 0.2, 0.19, 0.19, 0.18, 0.18, 0.16, 0.15, 0.15, 0.11, 0.11, 
                                                                                                                                        0.09, 0.07, 0.06, 0.04, 0.93, 0.92, 0.92, 0.91, 0.91, 0.9, 0.9, 
                                                                                                                                        0.9, 0.9, 0.89, 0.86, 0.86, 0.86, 0.86, 0.86, 0.85, 0.85, 0.84, 
                                                                                                                                        0.83, 0.82, 0.81, 0.81, 0.81, 0.8, 0.79, 0.78, 0.78, 0.77, 0.77, 
                                                                                                                                        0.76, 0.75, 0.74, 0.74, 0.74, 0.73, 0.72, 0.71, 0.7, 0.66, 0.65, 
                                                                                                                                        0.65, 0.64, 0.63, 0.61, 0.6, 0.59, 0.56, 0.54, 0.52, 0.51, 0.51, 
                                                                                                                                        0.5, 0.47, 0.45, 0.45, 0.43, 0.42, 0.42, 0.38, 0.36, 0.34, 0.32, 
                                                                                                                                        0.32, 0.31, 0.3, 0.3, 0.29, 0.28, 0.27, 0.27, 0.26, 0.24, 0.23, 
                                                                                                                                        0.18, 0.16, 0.14, 0.13, 0.13, 0.12, 0.09), TPR = c(0.02, 0.03, 
                                                                                                                                                                                           0.05, 0.07, 0.08, 0.1, 0.11, 0.13, 0.15, 0.16, 0.18, 0.2, 0.21, 
                                                                                                                                                                                           0.23, 0.25, 0.26, 0.28, 0.3, 0.31, 0.33, 0.34, 0.34, 0.36, 0.38, 
                                                                                                                                                                                           0.38, 0.39, 0.41, 0.43, 0.44, 0.44, 0.44, 0.46, 0.48, 0.49, 0.49, 
                                                                                                                                                                                           0.51, 0.52, 0.54, 0.56, 0.57, 0.59, 0.61, 0.62, 0.62, 0.64, 0.66, 
                                                                                                                                                                                           0.67, 0.69, 0.7, 0.72, 0.74, 0.74, 0.75, 0.75, 0.77, 0.77, 0.79, 
                                                                                                                                                                                           0.8, 0.8, 0.82, 0.82, 0.82, 0.84, 0.84, 0.84, 0.85, 0.85, 0.87, 
                                                                                                                                                                                           0.89, 0.9, 0.92, 0.92, 0.93, 0.93, 0.95, 0.95, 0.95, 0.97, 0.98, 
                                                                                                                                                                                           0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 1, 1, 1, 
                                                                                                                                                                                           1, 1, 1, 1, 1, 1, 1, 0.03, 0.05, 0.07, 0.08, 0.1, 0.11, 0.11, 
                                                                                                                                                                                           0.13, 0.15, 0.15, 0.16, 0.18, 0.21, 0.23, 0.25, 0.25, 0.26, 0.26, 
                                                                                                                                                                                           0.28, 0.31, 0.33, 0.33, 0.33, 0.34, 0.38, 0.39, 0.43, 0.49, 0.51, 
                                                                                                                                                                                           0.56, 0.59, 0.61, 0.62, 0.66, 0.69, 0.7, 0.7, 0.72, 0.72, 0.74, 
                                                                                                                                                                                           0.75, 0.75, 0.77, 0.77, 0.79, 0.79, 0.79, 0.8, 0.82, 0.84, 0.84, 
                                                                                                                                                                                           0.85, 0.87, 0.89, 0.89, 0.89, 0.89, 0.9, 0.92, 0.93, 0.93, 0.93, 
                                                                                                                                                                                           0.93, 0.93, 0.93, 0.95, 0.98, 0.98, 0.98, 0.98, 1, 1, 1, 1, 1, 
                                                                                                                                                                                           1, 1, 1, 1, 1), FPR = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                                                                                                                                   0, 0, 0, 0, 0, 0, 0, 0, 0.03, 0.03, 0.03, 0.05, 0.05, 0.05, 0.05, 
                                                                                                                                                                                                                   0.05, 0.08, 0.11, 0.11, 0.11, 0.11, 0.13, 0.13, 0.13, 0.16, 0.16, 
                                                                                                                                                                                                                   0.16, 0.16, 0.16, 0.16, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 
                                                                                                                                                                                                                   0.18, 0.21, 0.21, 0.24, 0.24, 0.26, 0.26, 0.26, 0.29, 0.29, 0.32, 
                                                                                                                                                                                                                   0.34, 0.34, 0.37, 0.39, 0.39, 0.42, 0.42, 0.42, 0.42, 0.42, 0.45, 
                                                                                                                                                                                                                   0.45, 0.47, 0.47, 0.5, 0.53, 0.53, 0.53, 0.55, 0.58, 0.61, 0.63, 
                                                                                                                                                                                                                   0.66, 0.68, 0.71, 0.74, 0.76, 0.76, 0.79, 0.82, 0.84, 0.87, 0.89, 
                                                                                                                                                                                                                   0.92, 0.95, 0.97, 1, 0, 0, 0, 0, 0, 0, 0.03, 0.03, 0.03, 0.05, 
                                                                                                                                                                                                                   0.05, 0.05, 0.05, 0.05, 0.05, 0.08, 0.08, 0.11, 0.11, 0.11, 0.11, 
                                                                                                                                                                                                                   0.13, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.18, 0.18, 0.18, 0.18, 
                                                                                                                                                                                                                   0.18, 0.18, 0.21, 0.24, 0.26, 0.26, 0.29, 0.29, 0.29, 0.32, 0.32, 
                                                                                                                                                                                                                   0.34, 0.34, 0.37, 0.39, 0.39, 0.39, 0.39, 0.42, 0.42, 0.45, 0.45, 
                                                                                                                                                                                                                   0.47, 0.5, 0.53, 0.53, 0.53, 0.53, 0.55, 0.58, 0.61, 0.63, 0.66, 
                                                                                                                                                                                                                   0.66, 0.66, 0.71, 0.74, 0.76, 0.76, 0.79, 0.82, 0.84, 0.87, 0.89, 
                                                                                                                                                                                                                   0.92, 0.95, 0.97, 1)), row.names = c(NA, -178L), class = c("data.table", 
                                                                                                                                                                                                                                                                              "data.frame"))

CodePudding user response:

You can use geom_ribbon. The ymax will be TPR, and since the diagonal occurs at TPR = FPR, the ymin will be FPR.

ggplot(a, aes(y = TPR, x = FPR))  
  geom_ribbon(aes(ymin = FPR, ymax = TPR, fill = model))  
  geom_line(aes(group = model), color = "black")  
  geom_segment(aes(y = 0, yend = 1, x = 0, xend = 1), color = "grey50")  
  scale_fill_manual(values = c("#ba6329", "#5f7c37"))  
  coord_equal()  
  theme_light(base_size = 16)

enter image description here

  • Related