ggplot theme remove y-axis on two plots in gtable, keep on third plot

9k views Asked by At

I am trying to remove the y-axis on two ggplots that are in a gtable with a third ggplot. I would like to show the y-axis for the leftmost graph in the gtable and remove the y-axis completely from the subsequent graphs; however, I would like the x-axis to remain on all plots.

My graph looks like this: ![nucleotide diversity][1]

[1]: image produced by code

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

There are 2 answers

1
americo On BEST ANSWER

Your linked image is not showing, but here is my shot in the dark:

Change from this:

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

to this:

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

if you want to change the label do this:

plot1 <- ... + ylab("y-axis label")
0
harris11 On

You need to do two things:

One is set axis.title.x and axis.title.y as blank under options (opts). The other one is set xlab("") and ylab("")

I have included a code snippet in case it helps:

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