2015-02-02 4 views
1

최종 저장된 이미지의 주요 그리드 선 사이에 거리가 일정한 플롯을 생성하고 싶습니다.ggplot 균일 한 이미지 크기?

내가 시도한 것은 : (a) 상단 및 하단 그림 마진을 각각 1cm로 설정 한 다음 (b) 주 눈금 선 사이의 각 "사각형"에 0.75cm를 더하십시오.

이 방법을 사용하면 내 플롯에 주요 눈금 선 사이의 거리가 일정하지 않습니다.

enter image description here

최소한의 예는 아래와 같다.

누군가가이 문제를 해결할 수 있기를 바랍니다.

library(ggplot2) 
library(gridExtra) #to set up plot grid 

num_points<-4 
dat_pop<-data.frame(
    est=runif(num_points,0,6), 
    ord=c(1,2) 
) 
dat_pop$varname<-c('Cobalt','Chromium') 

margin_height<-1 
border_total<-margin_height*2 
num_square<-num_points+1 
image_height<-0.75*num_square+border_total 

dat_pop$grpN<-seq(1,num_points,1) 

xstart=-1*(num_points+1) 
xend=0 

ThemeMain<-theme(legend.position = "none", 
       plot.margin = unit(c(margin_height,0.25,margin_height,0.5),"cm"), 
       panel.margin = unit(c(0,0,0,0),"cm"), 
       axis.ticks.y = element_blank(), 
       axis.text.y = element_blank(), 
       panel.grid.minor.y = element_blank() 
) 

####################################################################################################### 
#MAIN PLOT 
####################################################################################################### 
p<- 
    ggplot(dat_pop, aes(x=-grpN,y=est)) + 
    scale_y_continuous(name=NULL, limits=c(0, 6), expand=c(0,0)) + 
    geom_point(aes(shape="1")) + 
    coord_flip() + 
    scale_x_continuous(limits=c(xstart,xend), breaks=seq(xstart,xend),expand=c(0,0))+xlab(NULL)+ 
    ThemeMain 

ggsave(file="plot.pdf",p,width=21.59,height=image_height,units='cm') 
+1

당신은 운이 더 당신이 * 최소한의 * 재현 예를 제공하는 경우 답을 얻기가있을 수 있습니다. – shadow

+0

아마도 도움이 될 것입니다. http://stackoverflow.com/questions/24188986/r-ggplot2-make-two-geom-tile-plots-have-equal-height/24189239#24189239 – baptiste

답변

0

발견 적용 set_panel_size()가 작동하는 경우 : R, ggplot2, size of plot area

library(ggplot2) 
library(gridExtra) #to set up plot grid 
set_panel_size <- function(p=NULL, g=ggplotGrob(p), width=unit(3, "cm"), height=unit(3, "cm")){ 
    panel_index_w<- g$layout$l[g$layout$name=="panel"] 
    panel_index_h<- g$layout$t[g$layout$name=="panel"] 
    g$widths[[panel_index_w]] <- width 
    g$heights[[panel_index_h]] <- height 
    class(g) <- c("fixed", class(g), "ggplot") 
    g 
} 

num_points<-4 
dat_pop<-data.frame(
    est=runif(num_points,0,6), 
    ord=c(1,2) 
) 
dat_pop$varname<-c('Cobalt','Chromium') 

margin_height<-1 
border_total<-margin_height*2 
num_square<-num_points+1 
image_height<-0.75*num_square 

dat_pop$grpN<-seq(1,num_points,1) 

xstart=-1*(num_points+1) 
xend=0 

ThemeMain<-theme(legend.position = "none", 
       plot.margin = unit(c(margin_height,0.25,margin_height,0.5),"cm"), 
       panel.margin = unit(c(0,0,0,0),"cm"), 
       axis.ticks.y = element_blank(), 
       axis.text.y = element_blank(), 
       panel.grid.minor.y = element_blank() 
) 

####################################################################################################### 
#MAIN PLOT 
####################################################################################################### 
p<- 
    ggplot(dat_pop, aes(x=-grpN,y=est)) + 
    scale_y_continuous(name=NULL, limits=c(0, 6), expand=c(0,0)) + 
    geom_point(aes(shape="1")) + 
    coord_flip() + 
    scale_x_continuous(limits=c(xstart,xend), breaks=seq(xstart,xend),expand=c(0,0))+xlab(NULL)+ 
    ThemeMain 

p2<-set_panel_size(p, height=unit(image_height, "cm"),width=unit(21.6, "cm")) 

ggsave(file="plot.pdf",p2,width=21.6,units='cm')