How to get rid of gaps in the ggplot2 plot?

I am preparing a figure for publication. I omit the x label by setting xlab("") , however ggplot2 creates a space instead of completely removing the label. How can I get rid of spaces (marked with a red rectangle in the chart below)?

enter image description here

Full code:

 ggplot(data, aes(x=Celltype, y=Mean, fill=factor(Dose), label=p.stars)) + geom_bar(stat = "identity", position = position_dodge(width=0.9), aes(group=Dose)) + geom_errorbar(aes(ymin = Mean - SEM, ymax = Mean + SEM), stat = "identity", position = position_dodge(width=0.9), width=0.25) + geom_text(aes(y = Mean + SEM), size = 5, position = position_dodge(width=0.9), hjust = .5, vjust = -1) + xlab("") + ylab("Concentration") + scale_fill_grey(name = "Dose") + theme_bw() 
+6
source share
3 answers

Use theme() to remove the space allocated for the x-axis header. When you install xlab("") , there is still room for this header.

 + theme(axis.title.x=element_blank()) 
+5
source

Have you tried plot.margin ?

 library(grid) ggplot(data, aes(x=Celltype, y=Mean, fill=factor(Dose), label=p.stars)) + geom_bar(stat = "identity", position = position_dodge(width=0.9), aes(group=Dose)) + geom_errorbar(aes(ymin = Mean - SEM, ymax = Mean + SEM), stat = "identity", position = position_dodge(width=0.9), width=0.25) + geom_text(aes(y = Mean + SEM), size = 5, position = position_dodge(width=0.9), hjust = .5, vjust = -1) + xlab("") + ylab("Concentration") + scale_fill_grey(name = "Dose") + theme_bw() + theme(plot.margin = unit(c(1,1,0,1), "cm")) # ("left", "right", "bottom", "top") 
+5
source

Try this feature:

 savepdf <- function(file, width=16, height=10) { fname <- paste("figures/",file,".pdf",sep="") pdf(fname, width=width/2.54, height=height/2.54, pointsize=10) par(mgp=c(2.2,0.45,0), tcl=-0.4, mar=c(3.3,3.6,1.1,1.1)) } 

You can also trim the gap in the resulting PDF file after creating it. On Unix, the system command:

 pdfcrop filename.pdf filename.pdf 

pdfcrop works on Mac if the standard LaTeX distribution (Mactex or texlive) is installed. Of course, this command can be executed in R as follows:

 system(paste("pdfcrop", filename, filename)) 
+2
source

All Articles