2016-06-18 26 views
6

Tôi có hai ma trận nhầm lẫn với các giá trị được tính là dương (tp), sai dương (fp), âm thực (tn) và âm bản sai (fn), tương ứng với hai phương pháp khác nhau. Tôi muốn đại diện cho họ là enter image description hereMa trận nhầm lẫn lô trong R sử dụng ggplot

Tôi tin rằng lưới khía cạnh hoặc vỏ khía cạnh có thể làm điều này, nhưng tôi thấy khó bắt đầu. Đây là dữ liệu của hai ma trận nhầm lẫn tương ứng với method1 và method2

dframe<-structure(list(label = structure(c(4L, 2L, 1L, 3L, 4L, 2L, 1L, 
3L), .Label = c("fn", "fp", "tn", "tp"), class = "factor"), value = c(9, 
0, 3, 1716, 6, 3, 6, 1713), method = structure(c(1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L), .Label = c("method1", "method2"), class = "factor")), .Names = c("label", 
"value", "method"), row.names = c(NA, -8L), class = "data.frame") 

Trả lời

9

Đây có thể là một khởi đầu tốt

library(ggplot2) 
ggplot(data = dframe, mapping = aes(x = label, y = method)) + 
    geom_tile(aes(fill = value), colour = "white") + 
    geom_text(aes(label = sprintf("%1.0f",value)), vjust = 1) + 
    scale_fill_gradient(low = "white", high = "steelblue") 

Sửa

TClass <- factor(c(0, 0, 1, 1)) 
PClass <- factor(c(0, 1, 0, 1)) 
Y  <- c(2816, 248, 34, 235) 
df <- data.frame(TClass, PClass, Y) 

library(ggplot2) 
ggplot(data = df, mapping = aes(x = TClass, y = PClass)) + 
    geom_tile(aes(fill = Y), colour = "white") + 
    geom_text(aes(label = sprintf("%1.0f", Y)), vjust = 1) + 
    scale_fill_gradient(low = "blue", high = "red") + 
    theme_bw() + theme(legend.position = "none") 

enter image description here

0

Một giải pháp hơi mô-đun hơn dựa trên câu trả lời của MYaseen208. Có thể hiệu quả hơn đối với các bộ dữ liệu lớn/phân loại đa thức:

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 
Các vấn đề liên quan