如果ROC曲線的橫縱坐標重疊,可能是因為ROC曲線的橫縱坐標的范圍不匹配,需要調整坐標軸的范圍。
可以使用ggplot2包中的scale_x_continuous()和scale_y_continuous()函數來調整坐標軸的范圍。
例如,可以使用以下代碼來設置x和y軸范圍相同,并使其從0到1:
library(ggplot2)
# 生成假數據
set.seed(1234)
n <- 1000
df <- data.frame(
prob = runif(n),
label = rbinom(n, 1, 0.5)
)
# 計算ROC曲線
library(pROC)
roc_result <- roc(df$label, df$prob)
# 畫ROC曲線
ggplot(df, aes(d = label, m = prob)) +
stat_roc() +
geom_abline(linetype = "dashed") +
xlim(0, 1) +
ylim(0, 1) +
xlab("False Positive Rate") +
ylab("True Positive Rate") +
ggtitle("ROC Curve") +
theme_bw()
# 使x, y軸范圍相同
ggplot(df, aes(d = label, m = prob)) +
stat_roc() +
geom_abline(linetype = "dashed") +
scale_x_continuous(limits = c(0, 1)) +
scale_y_continuous(limits = c(0, 1)) +
xlab("False Positive Rate") +
ylab("True Positive Rate") +
ggtitle("ROC Curve") +
theme_bw()
在第二個ggplot()函數中,使用scale_x_continuous()和scale_y_continuous()函數來限制x和y軸的范圍,并使它們相同。