The ggplot theme deletes the y axis in two sections in the gtable, keep the third graph

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

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

[1]: image created 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) 
+4
source share
2 answers

Your linked image is not displayed, but here is my picture in the dark:

Change this:

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

:

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

if you want to change the shortcut do the following:

 plot1 <- ... + ylab("y-axis label") 
+1
source

You need to do two things:

One of them sets axis.title.x and axis.title.y as blank in the parameters (opts). The other is xlab (") and ylab (" ")

I 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)) 
0
source

Source: https://habr.com/ru/post/1496028/


All Articles