Plot confusion matrix in R using ggplot

前端 未结 5 2060
情歌与酒
情歌与酒 2020-12-29 08:38

I have two confusion matrices with calculated values as true positive (tp), false positives (fp), true negatives(tn) and false negatives (fn), corresponding to two different

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-29 09:00

    A slightly more modular solution based on MYaseen208's answer. Might be more effective for large datasets / multinomial classification:

    confusion_matrix <- as.data.frame(table(predicted_class, actual_class))
    
    ggplot(data = confusion_matrix
           mapping = aes(x = predicted_class,
                         y = Var2)) +
      geom_tile(aes(fill = Freq)) +
      geom_text(aes(label = sprintf("%1.0f", Freq)), vjust = 1) +
      scale_fill_gradient(low = "blue",
                          high = "red",
                          trans = "log") # if your results aren't quite as clear as the above example
    

提交回复
热议问题