2012-11-08 3 views
10

HERE에 대해 격자 배열에 관한 질문을하고 대단한 반응을 얻었습니다. 이제 플롯 간의 공간을 줄이려고하지만 오류가 발생합니다. 먼저 작동하는 코드와 오류 코드 (내가 시도한 코드)를 제시합니다. 나는 실제로 grid.arrange을 찾을 수없고 항상 그것이 gridExtra에서 오는 것으로 가정했지만 올바르지 않을 수 있습니다.grid.arrange plot 사이의 공간을 줄입니다.

그래서이 부분 :

  1. 어떻게 준비 그리드와 그래프 사이의 공간을 줄일 수 있습니다 내가 설명서를 찾을 수 있습니다
  2. 에 대한 grid.arrange (티스트 난 당신이 gridExtra 지금의 나의 생각 또는 사용을 수정하시기 바랍니다 유지 알고 내가 의도 한 방법으로 그것을 사용하지 않는 경우 패키지.)

좋은 코드 나쁜 공간

require(ggplot2);require(gridExtra) 
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() + 
    coord_flip() + ylab("") 
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() 


gA <- ggplot_gtable(ggplot_build(A)) 
gB <- ggplot_gtable(ggplot_build(B)) 
maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3]) 
gA$widths[2:3] <- as.list(maxWidth) 
gB$widths[2:3] <- as.list(maxWidth) 
grid.arrange(gA, gB, ncol=1) 

나쁜 코드 (내 시도)

require(ggplot2);require(gridExtra) 
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() + 
    coord_flip() + ylab("") + theme(plot.margin= unit(1, "cm")) 
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() 


gA <- ggplot_gtable(ggplot_build(A)) 
gB <- ggplot_gtable(ggplot_build(B)) 
maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3]) 
gA$widths[2:3] <- as.list(maxWidth) 
gB$widths[2:3] <- as.list(maxWidth) 
grid.arrange(gA, gB, ncol=1) 

오류 :

Error in `[.unit`(theme$plot.margin, 2) : 
    Index out of bounds (unit subsetting) 

답변

11

내가 오해했다 ggplot :

require(ggplot2);require(gridExtra) 
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() + 
    coord_flip() + ylab("") + theme(plot.margin= unit(c(1, 1, -1, 1), "lines")) 
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() + 
    theme(plot.margin= unit(rep(.5, 4), "lines")) 


gA <- ggplot_gtable(ggplot_build(A)) 
gB <- ggplot_gtable(ggplot_build(B)) 
maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3]) 
gA$widths[2:3] <- as.list(maxWidth) 
gB$widths[2:3] <- as.list(maxWidth) 
grid.arrange(gA, gB, ncol=1) 
-2

예, doc는 말합니다 : plot.margin | 전체 플롯 주변의 여백 (위쪽, 오른쪽, 아래쪽 및 왼쪽 여백의 크기를 기준으로 한 단위)

관련 문제