ggplot:如何 color/fill ROC 曲线和对角线之间的区域?
ggplot: How to color/fill area between ROC curves and diagonal?
我有这个 ROC 曲线
用这段代码写成:
ggplot(a, aes(y = TPR, x = FPR, color = model)) +
geom_line() +
geom_segment(aes(y = 0, yend = 1, x = 0, xend = 1), color = "grey50")
我想给红色和绿色曲线之间的 space 以及绿色曲线和对角线之间的区域着色。
我尝试徒手手动为预期的输出着色(我对艺术技巧表示歉意)
我使用 geom_area()
寻求解决方案,但无法解决问题。
如何填充这些区域?
这是我的数据样本。我对许多数据点表示歉意,但这是我可以重现达到 (0,0) 和 (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"))
您可以使用 geom_ribbon
。 ymax
将是 TPR,并且由于对角线出现在 TPR = FPR,因此 ymin
将是 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)
我有这个 ROC 曲线
用这段代码写成:
ggplot(a, aes(y = TPR, x = FPR, color = model)) +
geom_line() +
geom_segment(aes(y = 0, yend = 1, x = 0, xend = 1), color = "grey50")
我想给红色和绿色曲线之间的 space 以及绿色曲线和对角线之间的区域着色。
我尝试徒手手动为预期的输出着色(我对艺术技巧表示歉意)
我使用 geom_area()
寻求解决方案,但无法解决问题。
如何填充这些区域?
这是我的数据样本。我对许多数据点表示歉意,但这是我可以重现达到 (0,0) 和 (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"))
您可以使用 geom_ribbon
。 ymax
将是 TPR,并且由于对角线出现在 TPR = FPR,因此 ymin
将是 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)