【发布时间】:2021-05-27 03:04:06
【问题描述】:
我有一个数据框,其中包含通过 Matthews 相关系数、F1 分数和准确度测量的结果,我想使用ggplot2 为R 中的每个指标生成条形图。
我试图循环这三个速率,但R 不允许我使用变量 (this_rate) 为我的绘图选择正确的数据框列。
这是我的(不工作的)代码;
library("ggplot2")
# For the printed files
num_to_return <- 1
exe_num <- sample(1:as.numeric(10000), num_to_return)
data_table <- data.frame(cancer_type = c("'aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll"), MCC=c(0.085, 0.051, 0.013, 0.124, 0.156, 0.124, 0.573, 0.091, 0.513, -0.074, 0.388, 0.305), accuracy=c(0.095, 0.021, 0.023, 0.224, 0.256, 0.124, 0.576, 0.051, 0.543, -0.374, 0.588, 0.205), F1_score=c(0.065, 0.051, 0.073, 0.274, 0.276, 0.127, 0.577, 0.057, 0.547, -0.574, 0.588, 0.605))
list_of_rates <- colnames(data_table)[2:4]
for(this_rate in list_of_rates){
ylim_low <- 0
ylim_upp <- 1
data_table <- data_table[order(-data_table[c(this_rate)]),]
data_table$"cancer_type_factor" <- factor(data_table$"cancer_type", levels = data_table$"cancer_type")
data_table$"cancer_type" <- data_table$"cancer_type_factor"
p_this_rate_plot <- ggplot(data_table, aes(x=reorder(cancer_type, -this_rate), y=this_rate, fill=cancer_type)) + geom_bar(stat="identity", color="black", position=position_dodge()) + ylab(paste0("mean ", this_rate)) + xlab("") + ggtitle("survival binary prediction") + theme(plot.title = element_text(hjust = 0.5), axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank())
p_this_rate_plot <- p_this_rate_plot + scale_y_continuous(limits=c(ylim_low, ylim_upp)) #, breaks=c(1:10))
pdfThisPlotFile <- paste0("this_barplot_", exe_num,".pdf")
cat("We're going to save the ", pdfThisPlotFile, "file\n")
ggsave(pdfThisPlotFile)
}
这是日志问题:
-this_rate 中的错误:一元运算符的参数无效
问题发生在ggplot() 函数调用中两次出现的this_rate 变量上:显然不可能使用它来选择数据框中的正确列。
我该如何解决这个问题?
谢谢
【问题讨论】: