This is continuation of my previous post available in Extracting the values from ROC curve
From the accepted answer,
library(ggplot2)
dat <- structure(list(threshold = c(-Inf, 0.035, 0.045, 0.055, 0.065, 0.075, 0.085, 0.095, 0.105, 0.115, 0.125, 0.135, 0.145, 0.155, 0.165, 0.175, 0.185, 0.205, 0.225, 0.235, 0.245, 0.255, 0.265, 0.275, 0.29, 0.31, 0.325, 0.335, 0.345, 0.365, 0.395, 0.42, 0.435, 0.445, 0.455, 0.465, 0.475, 0.485, 0.495, 0.51, 0.54, 0.57, 0.64, 0.705, 0.725, 0.755, 0.795, 0.84, 0.91, 1.515, Inf), specificity = c(0, 0, 0.0694444444444444, 0.111111111111111, 0.138888888888889, 0.222222222222222, 0.305555555555556, 0.388888888888889, 0.486111111111111, 0.541666666666667, 0.541666666666667, 0.583333333333333, 0.638888888888889, 0.694444444444444, 0.736111111111111, 0.763888888888889, 0.777777777777778, 0.805555555555556, 0.805555555555556, 0.805555555555556, 0.819444444444444, 0.819444444444444, 0.819444444444444, 0.819444444444444, 0.833333333333333, 0.833333333333333, 0.847222222222222, 0.861111111111111, 0.875, 0.875, 0.888888888888889, 0.888888888888889, 0.902777777777778, 0.902777777777778, 0.916666666666667, 0.930555555555556, 0.958333333333333, 0.972222222222222, 0.972222222222222, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), sensitivity = c(1, 0.975609756097561, 0.975609756097561, 0.975609756097561, 0.975609756097561, 0.902439024390244, 0.878048780487805, 0.829268292682927, 0.780487804878049, 0.75609756097561, 0.731707317073171, 0.682926829268293, 0.658536585365854, 0.658536585365854, 0.634146341463415, 0.634146341463415, 0.634146341463415, 0.634146341463415, 0.609756097560976, 0.585365853658537, 0.585365853658537, 0.560975609756098, 0.536585365853659, 0.51219512195122, 0.51219512195122, 0.48780487804878, 0.463414634146341, 0.439024390243902, 0.439024390243902, 0.414634146341463, 0.414634146341463, 0.390243902439024, 0.390243902439024, 0.341463414634146, 0.341463414634146, 0.341463414634146, 0.341463414634146, 0.317073170731707, 0.292682926829268, 0.292682926829268, 0.268292682926829, 0.24390243902439, 0.219512195121951, 0.195121951219512, 0.146341463414634, 0.121951219512195, 0.0975609756097561, 0.0731707317073171, 0.048780487804878, 0.024390243902439, 0)), class = "data.frame", row.names = c(NA, -51L))
ggplot(dat, aes(specificity, sensitivity)) +
geom_step() +
geom_abline(intercept = 1, slope = -1, colour = "gray") +
coord_trans(x = "reverse", expand = FALSE, clip = "off")
However when I try to add annotation_custom()
as below, it fails
annotation_custom(grob = grid::rectGrob(
x = unit(0.500, 'npc'),
y = unit(0.015, 'npc'),
width = unit(0.150, 'npc'),
height = unit(0.006, 'npc'),
gp = grid::gpar(fill = 'gray60', col = NA)))
I get error stating
`annotation_custom()` only works with `coord_cartesian()`
Could you please help to find a solution of above error?
If you are set on adding an annotation_custom
you could just rearrange the ordering of the specificity and sensitivity data so that it gives the correct output. This gets rid of the need for coord_trans
p <- ggplot(dat[order(1 - dat$specificity, dat$sensitivity),],
aes(1 - specificity, sensitivity)) +
geom_step() +
geom_abline(intercept = 0, slope = 1, colour = "gray")
p
You can then easily add whatever annotation_custom
you like.
p <- p + annotation_custom(grob = grid::rectGrob(
x = unit(0.500, 'npc'),
y = unit(0.015, 'npc'),
width = unit(0.150, 'npc'),
height = unit(0.006, 'npc'),
gp = grid::gpar(fill = 'gray60', col = NA)))
p
If you prefer a reversed x axis as in the default plot.roc
output, you could reverse it manually using
p + scale_x_continuous("specificity", labels = ~ 1 - .x)