2014-01-22 1 views
0

아래는 2x2 막대 그림입니다. 왼쪽 하단의 범례와 페이지 끝의 범례 사이에 큰 공간이 있음에 유의하십시오. 어떻게 줄일 수 있습니까?R 줄거리에서 용지 가장자리쪽으로 아래쪽 범례 사이의 공간 축소

물론 수동 수작업을 할 수 있습니다. 그러나 생성 될 숫자가 많으므로 자동으로 처리하는 것이 훨씬 더 바람직합니다.

enter image description here

그림이 코드로 생성됩니다

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() 

답변

2

이유는 레이아웃입니다. 귀하의 레이아웃이

enter image description here

처럼 보인다하지만 당신은 heights 인수를 사용하여 각 행, 예를 들어, 서로 다른 높이를 원하는 당신에게 줄 것이다

layout(matrix(c(1,2,3,4,5,5),nrow=3,byrow = TRUE), heights=c(2,2,1)) 

enter image description here

지금, 당신의 전설 더 이상 그 많은 공간을 차지하지 않습니다. 범례의 플롯 창을 설정하기 전에 여백을 약간 조정하여 par(mar=c(0,1,2,0))을 추가해야 할 수도 있습니다. 그럼 당신은

enter image description here

전체 코드를 얻을 수

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()