R语言调整ROC图横坐标
时间: 2023-11-27 19:04:09 浏览: 35
您可以使用 `ggplot2` 包中的 `coord_flip()` 函数来调整 ROC 图的横纵坐标。具体操作如下:
```R
library(ggplot2)
# 生成 ROC 数据
set.seed(123)
df <- data.frame(
y_true = sample(c(0, 1), 100, replace = TRUE),
y_score = runif(100)
)
df <- df[order(df$y_score, decreasing = TRUE), ]
df$tp <- cumsum(df$y_true == 1)
df$fp <- cumsum(df$y_true == 0)
df$sensitivity <- df$tp / sum(df$y_true == 1)
df$specificity <- 1 - df$fp / sum(df$y_true == 0)
# 绘制 ROC 图
ggplot(df, aes(x = 1 - specificity, y = sensitivity)) +
geom_line() +
geom_abline(intercept = 0, slope = 1, linetype = "dashed") +
labs(x = "False Positive Rate", y = "True Positive Rate") +
coord_flip()
```
其中,`x = 1 - specificity` 表示横坐标为 FPR(False Positive Rate),`y = sensitivity` 表示纵坐标为 TPR(True Positive Rate)。`geom_abline()` 函数用于绘制对角线,即 AUC(Area Under Curve)为 0.5 的线。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)