Open w28924461701 opened 1 year ago
library(pROC) library(ggplot2) library(gridExtra)
set.seed(123) gene_expression <- rnorm(1000) label <- sample(c(0, 1), 100, replace = TRUE) tumor_type <- sample(c("lung", "breast", "colon"), 100, replace = TRUE)
data <- data.frame(gene_expression, label, tumor_type)
roc_list <- list() auc_list <- list()
for (t in unique(data$tumor_type)) { roc_obj <- roc(data$label[data$tumor_type == t], data$gene_expression[data$tumor_type == t]) auc_value <- auc(roc_obj)
if (auc_value > 0.5) { # 过滤 AUC 值大于 0.5 的肿瘤类型 roc_list[[t]] <- roc_obj auc_list[[t]] <- auc_value } }
plot_list <- list()
for (t in names(roc_list)) { # 遍历过滤后的肿瘤类型 auc_value <- round(auc_list[[t]], 2) plot_obj <- ggroc(roc_list[[t]]) + ggtitle(paste("ROC Curve for ", t, " Cancer, AUC=", auc_value)) plot_list[[t]] <- plot_obj }
ggroc(roc_list[[t]]) + ggtitle(paste("ROC Curve for ", t, " Cancer, AUC=", auc_value)) + theme(plot.title = element_text(size = 10, hjust = 0.5, vjust = 0.05))
pdf("myplot.pdf", onefile = TRUE) # 设置生成一个 PDF 文件 grid.arrange(grobs = plot_list, ncol = 2, nrow = 2) # 指定 ncol 和 nrow 参数以生成两个页面 dev.off()
library(pROC) library(ggplot2) library(gridExtra)
create sample data
set.seed(123) gene_expression <- rnorm(1000) label <- sample(c(0, 1), 100, replace = TRUE) tumor_type <- sample(c("lung", "breast", "colon"), 100, replace = TRUE)
data <- data.frame(gene_expression, label, tumor_type)
calculate ROC curve and AUC for each tumor type
roc_list <- list() auc_list <- list()
for (t in unique(data$tumor_type)) { roc_obj <- roc(data$label[data$tumor_type == t], data$gene_expression[data$tumor_type == t]) auc_value <- auc(roc_obj)
if (auc_value > 0.5) { # 过滤 AUC 值大于 0.5 的肿瘤类型 roc_list[[t]] <- roc_obj auc_list[[t]] <- auc_value } }
plot ROC curves for each tumor type
plot_list <- list()
for (t in names(roc_list)) { # 遍历过滤后的肿瘤类型 auc_value <- round(auc_list[[t]], 2) plot_obj <- ggroc(roc_list[[t]]) + ggtitle(paste("ROC Curve for ", t, " Cancer, AUC=", auc_value)) plot_list[[t]] <- plot_obj }
ggroc(roc_list[[t]]) + ggtitle(paste("ROC Curve for ", t, " Cancer, AUC=", auc_value)) + theme(plot.title = element_text(size = 10, hjust = 0.5, vjust = 0.05))
arrange plots into a grid and save as PDF
pdf("myplot.pdf", onefile = TRUE) # 设置生成一个 PDF 文件 grid.arrange(grobs = plot_list, ncol = 2, nrow = 2) # 指定 ncol 和 nrow 参数以生成两个页面 dev.off()