如何创建 2x2 条形图 - 在 R 中并排成对条 [英] How to create 2x2 bar plot - with side by side pairwise bar in R

查看:53
本文介绍了如何创建 2x2 条形图 - 在 R 中并排成对条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据:

  Method    Metric        E0        E1        E2        E4
1     M1 Precision 0.9661017 0.9622642 1.0000000 0.9655172
2     M2 Precision 0.5330000 0.5350000 0.3780000 0.2140000
3     M1    Recall 0.9736842 0.9736842 0.9473684 0.9473684
4     M2    Recall 1.0000000 1.0000000 1.0000000 0.6670000

我想要做的是将上述数据创建成下图:

What I want to do is to create the above data into the following plot:

在这个草图中,条的高度肯定没有反映上面的数据.

In this sketch the height of the bar doesn't reflect the data above, surely.

有什么办法呢?最好不使用 ggplot 或lattice 等外部库.

What's the way to do it? Prefferably without using external libraries like ggplot or lattice.

推荐答案

给你:

导入:

dat <- read.table(text="Method  Metric  E0  E1  E2  E4
M1  Precision   0.9661017   0.9622642   1   0.9655172
M2  Precision   0.533   0.535   0.378   0.214
M1  Recall  0.9736842   0.9736842   0.9473684   0.9473684
M2  Recall  1   1   1   0.667",header=TRUE)

定义布局和一些颜色:

layout(matrix(c(1,2,5,3,4,5),nrow=2,byrow = TRUE))
#     [,1] [,2] [,3]
#[1,]    1    2    5
#[2,]    3    4    5
barcols <- c("red","blue")

生成一些图:

sapply(3:6, 
  function(x) {
    bp <- barplot(matrix(dat[,x],nrow=2,byrow=TRUE),beside=TRUE,col=barcols)
    title(main=names(dat[x]))
    axis(1,at=colMeans(bp),c("M1","M2"),lwd=0,lwd.tick=1)
    abline(h=0)
  }
)

添加图例:

plot(NA,xlim=c(0,1),ylim=c(0,1),ann=FALSE,axes=FALSE)
legend(0,0.6,c("Precision","Recall"),fill=barcols,cex=1.5)

结果:

这篇关于如何创建 2x2 条形图 - 在 R 中并排成对条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆