geom_tile和geom_text以及facet_wrap产生不正确的混淆矩阵

时间:2015-09-22 09:04:59

标签: r ggplot2

我的数据框如下所示:

method1 = c("cam","sce","cam","sce","cam","sce","cam","sce","cam","sce","cam","sce","cam","sce","cam","sce");
method2 = c("cam","cam","sce","sce","cam","cam","sce","sce","cam","cam","sce","sce","cam","cam","sce","sce");
p = c(0,0.191781,0.780822,0,0,0.082192,0.890411,0,0,0.383562,0.616438,0,0,0.054795,0.863014,0);
participant = c("LucO","LucO","LucO","LucO","ad","ad","ad","ad","arthur","arthur","arthur","arthur","evgeny","evgeny","evgeny","evgeny");
mydata = data.frame(method1,method2,p,participant);

我试图将每个参与者的混淆矩阵绘制为:

p <- ggplot( mydata, aes( x=method1, y=method2, fill=method2 ) ) + 
     geom_tile( aes( fill = mydata$p ) ) +
     geom_text( aes( fill = mydata$p, label = round( mydata$p, 3 ) * 100 ) ) +
     facet_wrap( ~participant, ncol=3, scales="free_x" );

然而,现在对于每个participant,绘制了错误的混淆矩阵。你能帮我修一下这个问题吗?谢谢。

1 个答案:

答案 0 :(得分:3)

只是丢失mydata$p并只添加p即可。

正确调用该函数是:

library(ggplot2)
p <- ggplot( mydata, aes( x=method1, y=method2, fill=method2 ) ) + 
  geom_tile( aes( fill = p ) ) +
  geom_text( aes( fill = p, label = round( p, 3 ) * 100 ) ) +
  facet_wrap( ~participant, ncol=3, scales="free_x" );
p

输出:

enter image description here

数据(用于与图表比较)

> mydata
   method1 method2        p participant
1      cam     cam 0.000000        LucO
2      sce     cam 0.191781        LucO
3      cam     sce 0.780822        LucO
4      sce     sce 0.000000        LucO
5      cam     cam 0.000000          ad
6      sce     cam 0.082192          ad
7      cam     sce 0.890411          ad
8      sce     sce 0.000000          ad
9      cam     cam 0.000000      arthur
10     sce     cam 0.383562      arthur
11     cam     sce 0.616438      arthur
12     sce     sce 0.000000      arthur
13     cam     cam 0.000000      evgeny
14     sce     cam 0.054795      evgeny
15     cam     sce 0.863014      evgeny
16     sce     sce 0.000000      evgeny