在R中使用sapply绘制并排图 [英] using sapply in R for ploting side by side graph
本文介绍了在R中使用sapply绘制并排图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
dat<-read.table(text =主题项目C10 C14 C03 C11 C16 C08T1 P1 0.24 0.00 0.00 0.04 0.04 0.00T2 P1 0.00 0.30 0.00 0.00 0.00 0.00T3 P1 0.04 0.04 0.00 0.24 0.00 0.00T4 P1 0.00 0.00 0.00 0.04 0.33 0.04T5 P1 0.00 0.09 0.21 0.00 0.00 0.00T6 P1 0.00 0.09 0.00 0.00 0.00 0.34T1 P2 0.20 0.00 0.00 0.04 0.00 0.04T2 P2 0.00 0.22 0.04 0.00 0.00 0.00T3 P2 0.04 0.00 0.00 0.24 0.00 0.00T4 P2 0.00 0.00 0.04 0.00 0.33 0.00T5 P2 0.04 0.00 0.21 0.00 0.00 0.00T6 P2 0.00 0.04 0.00 0.00 0.00 0.34,标头= TRUE)布局(矩阵(c(1,2,5,3,4,5),nrow = 2,byrow = TRUE))#[,1] [,2] [,3]#[1,] 1 2 5#[2,] 3 4 5barcols<-c(红色",蓝色",绿色",橙色",黑色",黄色")sapply(3:8,函数(x){bp<-barplot(矩阵(dat [,x],nrow = 2,byrow = TRUE),ylim = c(0,0.5),side = TRUE,col = barcols)标题(main = names(dat [x]))axis(1,at = colMeans(bp),c("T1","T2","T3","T4","T5","T6"),lwd = 0,lwd.tick = 1)背斜(h = 0)})图(NA,xlim = c(0,1),ylim = c(0,1),ann = FALSE,轴= FALSE)图例(0,0.6,c("C10","C10","C03","C11","C16","C08"),fill = barcols,cex = 1.5)
以上代码应使用并排功能绘制两个条形图,如下所示:
不幸的是,我只能得到一个图,这是不正确的,并且代码是从
I have the following code:
dat <- read.table(text="Topic Project C10 C14 C03 C11 C16 C08
T1 P1 0.24 0.00 0.00 0.04 0.04 0.00
T2 P1 0.00 0.30 0.00 0.00 0.00 0.00
T3 P1 0.04 0.04 0.00 0.24 0.00 0.00
T4 P1 0.00 0.00 0.00 0.04 0.33 0.04
T5 P1 0.00 0.09 0.21 0.00 0.00 0.00
T6 P1 0.00 0.09 0.00 0.00 0.00 0.34
T1 P2 0.20 0.00 0.00 0.04 0.00 0.04
T2 P2 0.00 0.22 0.04 0.00 0.00 0.00
T3 P2 0.04 0.00 0.00 0.24 0.00 0.00
T4 P2 0.00 0.00 0.04 0.00 0.33 0.00
T5 P2 0.04 0.00 0.21 0.00 0.00 0.00
T6 P2 0.00 0.04 0.00 0.00 0.00 0.34",
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","green","orange","black","yellow")
sapply(3:8,
function(x) {
bp <- barplot(matrix(dat[,x],nrow=2,byrow=TRUE),ylim=c(0, 0.5),beside=TRUE,col=barcols)
title(main=names(dat[x]))
axis(1,at=colMeans(bp),c("T1","T2","T3","T4","T5","T6"),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("C10","C10","C03","C11","C16","C08"),fill=barcols,cex=1.5)
The above code should plot two barplots using the side-by-side feature as follow:
unfortunatly I only get one plot, which is incorrect, and the code is inspired from this post
解决方案
The easiest way for me to get the ggplot-like faceting is to write a function that takes a subset of data and use that on a split
, eg,
dat <- read.table(text="Topic Project C10 C14 C03 C11 C16 C08
T1 P1 0.24 0.00 0.00 0.04 0.04 0.00
T2 P1 0.00 0.30 0.00 0.00 0.00 0.00
T3 P1 0.04 0.04 0.00 0.24 0.00 0.00
T4 P1 0.00 0.00 0.00 0.04 0.33 0.04
T5 P1 0.00 0.09 0.21 0.00 0.00 0.00
T6 P1 0.00 0.09 0.00 0.00 0.00 0.34
T1 P2 0.20 0.00 0.00 0.04 0.00 0.04
T2 P2 0.00 0.22 0.04 0.00 0.00 0.00
T3 P2 0.04 0.00 0.00 0.24 0.00 0.00
T4 P2 0.00 0.00 0.04 0.00 0.33 0.00
T5 P2 0.04 0.00 0.21 0.00 0.00 0.00
T6 P2 0.00 0.04 0.00 0.00 0.00 0.34",
header=TRUE)
layout(matrix(c(1,1,2,2,3,3),nrow=2))
barcols <- c("red","blue","green","orange","black","yellow")
sp <- split(dat, dat$Project)
sapply(seq_along(sp),
function(x) {
dd <- sp[[x]]
m <- t(`rownames<-`(as.matrix(dd[, -(1:2)]), dd[, 1]))
bp <- barplot(m,ylim=c(0, 0.5),beside=TRUE,col=barcols)
title(main=names(sp[x]))
abline(h=0)
}
)
plot(NA,xlim=c(0,1),ylim=c(0,1),ann=FALSE,axes=FALSE)
legend(0,0.6,c("C10","C10","C03","C11","C16","C08"),fill=barcols,cex=1.5)
这篇关于在R中使用sapply绘制并排图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文