【问题标题】:R, how to use a variable to select different columns of a dataframe in ggplot2?R,如何使用变量在ggplot2中选择数据框的不同列?
【发布时间】:2021-05-27 03:04:06
【问题描述】:

我有一个数据框,其中包含通过 Matthews 相关系数、F1 分数和准确度测量的结果,我想使用ggplot2R 中的每个指标生成条形图。 我试图循环这三个速率,但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 变量上:显然不可能使用它来选择数据框中的正确列。 我该如何解决这个问题?

谢谢

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    当您传递列名的字符值时,使用.data 对其进行子集化。这可以为list_of_rates 创建单独的pdf

    library(data.table)
    library(ggplot2)
    
    data_table$cancer_type_factor <- factor(data_table$cancer_type, 
                                       levels = data_table$cancer_type)
    
    data_table$cancer_type <- data_table$cancer_type_factor
    ylim_low <- 0
    ylim_upp <- 1
    
    for(this_rate in list_of_rates){
      
      data_table <- data_table[order(-data_table[[this_rate]]),]
      p_this_rate_plot <- ggplot(data_table, 
                   aes(x=reorder(cancer_type, -.data[[this_rate]]), 
                                 y=.data[[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()) + 
        scale_y_continuous(limits=c(ylim_low, ylim_upp))
      
      pdfThisPlotFile <- paste0("this_barplot_", this_rate,".pdf")
      cat("We're going to save the ", pdfThisPlotFile, "file\n")
      ggsave(pdfThisPlotFile)
    }
    

    【讨论】:

    • 谢谢,这正是我所需要的!另一个问题:你知道我怎样才能在图中保持元素的颜色一致吗?现在每个情节的颜色顺序从浅粉色到紫色,但我希望相同的元素在每个情节中保持相同的颜色。我该怎么做?谢谢
    • 您可以创建一个命名变量并为每个值分配一个固定的颜色。看到这个帖子stackoverflow.com/questions/42891307/…
    猜你喜欢
    • 1970-01-01
    • 2022-01-17
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多