2013-08-08 2 views
4

세 번째 ggplot이있는 gtable에있는 두 개의 ggplots에서 y 축을 제거하려고합니다. gtable에서 가장 왼쪽에있는 그래프의 y 축을 표시하고 후속 그래프에서 y 축을 완전히 제거하고 싶습니다. 그러나, 나는 모든 플롯에 x 축을 유지하고 싶습니다.ggplot theme gtable에서 두 개의 플롯에서 y 축 제거, 세 번째 플롯에서 계속

내 그래프는 다음과 같습니다! [염기 다양성] [1]

[1] : 코드에 의해 생성 된 이미지가

연결된 이미지가 여기에 표시되지 않지만,
library("ggplot2") 
library("gridExtra") 
library("gtable") 

theme_set(theme_bw(base_size=16)) 


p1 <- ggplot(a.pi, aes(x=window, y=measure, fill=key, colour=key)) + 
    geom_line() + 
    scale_colour_manual(values=c("#000099", "#333333", "#FF0000")) + 
    ylab(expression(pi)) + 
    xlab("Position") + 
    scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+ 
    scale_y_continuous(limits=c(0.0,0.0004)) + 
    theme(#axis.text.y = element_blank(), 
     #axis.ticks.y = element_blank(), 
     #axis.title.y = element_blank(), 
     #axis.title.x = element_blank(), 
     plot.margin = unit(c(0,-3,0,0), "lines"), 
     plot.background = element_blank(), 
     panel.background = element_blank(), 
     panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank(), 
     panel.border = element_blank(), 
     legend.position="none", 
     axis.line = element_line() 
     ) 

p2 <- ggplot(b.pi, aes(x=window, y=measure, fill=key, colour=key)) + 
    geom_line() + 
    scale_colour_manual(values=c("#333333", "#FF0000")) + 
    #ylab(expression(pi)) + 
    xlab("Position") + 
    scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+ 
    scale_y_continuous(limits=c(0.0,0.0004)) + 
    theme(axis.text.y = element_blank(), 
     axis.ticks.y = element_blank(), 
     axis.title.y = element_blank(), 
     #axis.title.x = element_blank(), 
     plot.margin = unit(c(0,-3,0,0), "lines"), 
     plot.background = element_blank(), 
     panel.background = element_blank(), 
     panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank(), 
     panel.border = element_blank(), 
     legend.position="none", 
     axis.line = element_line() 
     ) 

p3 <- ggplot(c.pi, aes(x=window, y=measure, fill=key, colour=key)) + 
    geom_line() + 
    scale_colour_manual(values=c("#333333", "#FF0000")) + 
    #ylab(expression(pi)) + 
    xlab("Position") + 
    scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+ 
    scale_y_continuous(limits=c(0.0,0.0004)) + 
    theme(axis.text.y = element_blank(), 
     axis.ticks.y = element_blank(), 
     axis.title.y = element_blank(), 
     #axis.title.x = element_blank(), 
     plot.margin = unit(c(0,-3,0,0), "lines"), 
     plot.background = element_blank(), 
     panel.background = element_blank(), 
     panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank(), 
     panel.border = element_blank(), 
     legend.position="none", 
     axis.line = element_line() 
     ) 

grid.arrange(p1,p2,p3, nrow=1) 


gt1 <- ggplot_gtable(ggplot_build(p1)) 
gt2 <- ggplot_gtable(ggplot_build(p2)) 
gt3 <- ggplot_gtable(ggplot_build(p3)) 

newWidth = unit.pmax(gt1$widths[1:3], gt2$widths[1:3], gt3$widths[1:3]) 

gt1$widths[1:3] = as.list(newWidth) 
gt2$widths[1:3] = as.list(newWidth) 
gt3$widths[1:3] = as.list(newWidth) 

# New gtable with space for the three plots plus a right-hand margin 
gt = gtable(widths = unit(c(1, 1, 1, 0.3), "null"), height = unit(1, "null")) 


# Instert gt1, gt2 and gt2 into the new gtable 
gt <- gtable_add_grob(gt, gt1, 1, 1) 
gt <- gtable_add_grob(gt, gt2, 1, 2) 
gt <- gtable_add_grob(gt, gt3, 1, 3) 

grid.newpage() 
grid.draw(gt) 
+3

(1)에 코드를 단순화함으로써이 문제를 개선하십시오 다른 하나는 xlab ("")와 ylab 내가 도움이 경우에 코드를 포함했다

를 ("")로 설정 문제를 재현하는 가장 짧은 버전, 그리고 (2) 코드를 완전히 재현 가능하게 만드십시오. 예를 들어, 내 R 콘솔에 코드를 붙여 넣을 때 오류가 발생합니다.'object 'a.pi'not found' – bdemarest

+0

@ user2145578 감사합니다! 귀하의 질문에 귀하의 질문에 대답! =) – isomorphismes

답변

1

가 내 샷 이에서

변경 : 이것에

plot1 <- theme(#axis.text.y = element_blank(),... 

어둠 속에서 :

plot1 <- theme(axis.text.y = element_text(),... 

당신은 라벨이 작업을 수행 변경하려면 :

당신은 두 가지를 할 필요가
plot1 <- ... + ylab("y-axis label") 
+0

고마워요! 내 이미지가 표시되지 않는 이유는 내가 "평판 포인트"가 충분하지 않기 때문입니다. – ONeillMB1

0

:

하나는 axis.title.x 및 axis.title.y을 설정을 옵션 (선택 사항)에서 공백으로

ggplot(space[1:closetos[i],], aes(dim1, dim9, colour = name, 
shape=shape))+ opts(axis.line = theme_segment(colour = "black"), 
panel.grid.major = theme_blank(), 
panel.grid.minor = theme_blank(), 
panel.border = theme_blank(), 
panel.background = theme_blank(), 
axis.title.x = theme_blank(), 
axis.title.y = theme_blank())+ 
       xlab("") + 
       ylab("")+ 
       theme(text = element_text(size=15, colour = "black"), 
        axis.text.x = element_text(angle=0, vjust=1, colour = "black"), 
        axis.text.y = element_text(angle=0, vjust=1, colour = "black"), 
        axis.line = element_line(colour = 'black', size = 1), 
        axis.ticks = element_line(colour = 'black', size = 1), 
        axis.ticks.length = unit(0.3, "cm"), 
        axis.title.y=element_text(vjust=0.4), 
        legend.position = "none") + 
       geom_point(size=5)+ 
       scale_color_manual("Status", values = mycolours) + 
       xlim((space$dim1[closetos[i]]-0.01), (space$dim1[closetos[i]]+0.01)) + 
       ylim((space$dim9[closetos[i]]-0.01), (space$dim9[closetos[i]]+0.01))