下面是2x2条形图。请注意,在左下角到页面末尾的图例之间有一个很大的空间。我怎样才能减少呢?
当然,我可以做手工裁剪。但由于要生成许多数字,因此更自然地自动处理它。
该图是使用以下代码生成的:
dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)
pdf(file="Temp.pdf",height=9,width=6);
colnames(dat) <- c("Method", "Metric", "error 0%", "error 1%", "error 2%", "error 4%")
# Define layout
layout(matrix(c(1,2,3,4,5,5),nrow=3,byrow = TRUE))
par(omi=c(0,0.3,0,0.3))
barcols <- c("#D8B365","#5AB4AC")
# Generate some plots
sapply(3:6,
function(x) {
par(las=2);
bp <- barplot(matrix(dat[,x],nrow=2,byrow=TRUE),beside=TRUE,col=barcols,border=NA)
title(main=names(dat[x]),cex.main=1.0,font.main=1)
axis(1,at=colMeans(bp),c("Method-XXX","Method-YYY"," Method-ZZZ","Method-XZZZ"," Method-XZZZY"),lwd=0,lwd.tick=1)
abline(h=0)
}
)
plot(NA,xlim=c(0,1),ylim=c(0,1),ann=FALSE,axes=FALSE)
par(omi=c(0.5,0,0,0))
legend("topleft",c("Precision","Recall"),box.col="white",fill=barcols,cex=1.0,border=NA)
dev.off()
答案 0 :(得分:2)
原因在于您的布局。你的布局看起来像这样
但是您希望使用heights
参数为每一行提供不同的高度,例如
layout(matrix(c(1,2,3,4,5,5),nrow=3,byrow = TRUE), heights=c(2,2,1))
会给你这个
现在,你的传奇不再占用那么多空间了。在设置图例的绘图窗口之前,您可能需要稍微调整边距,添加par(mar=c(0,1,2,0))
。然后你得到
完整代码
dat <- read.table("http://dpaste.com/1563769/plain/",header=TRUE)
pdf(file="Temp.pdf",height=7,width=6);
colnames(dat) <- c("Method", "Metric", "error 0%", "error 1%", "error 2%", "error 4%")
# Define layout
layout(matrix(c(1,2,3,4,5,5),nrow=3,byrow = TRUE), heights=c(2,2,.6))
par(omi=c(0,0.3,0,0.3))
barcols <- c("#D8B365","#5AB4AC")
# Generate some plots
sapply(3:6,
function(x) {
par(las=2);
bp <- barplot(matrix(dat[,x],nrow=2,byrow=TRUE),beside=TRUE,col=barcols,border=NA)
title(main=names(dat[x]),cex.main=1.0,font.main=1)
axis(1,at=colMeans(bp),c("Method-XXX","Method-YYY"," Method-ZZZ","Method-XZZZ"," Method-XZZZY"),lwd=0,lwd.tick=1)
abline(h=0)
}
)
par(mar=c(0,2,2,0))
plot(NULL,xlim=c(0,1),ylim=c(0,1),ann=FALSE,axes=FALSE)
legend("topleft",c("Precision","Recall"),box.col="white",fill=barcols,cex=1.0,border=NA)
dev.off()