Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of whitespace in a ggplot2 plot?

I'm preparing a figure for a publication. I'm omitting the x label by setting xlab(""), however ggplot2 produces a whitespace instead of completely removing the label. How can I get rid of the whitespace (marked by red rectangle in the plot below)?

enter image description here

The 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()
like image 930
Eekhoorn Avatar asked Feb 05 '14 12:02

Eekhoorn


Video Answer


4 Answers

You could also set x = NULL in labs()

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) +
  labs(x = NULL, y = "Concentration") +
  scale_fill_grey(name = "Dose") +
  theme_bw()
like image 156
Jojje Avatar answered Oct 21 '22 20:10

Jojje


Use theme() to remove space allocated for the x axis title. When you set xlab("") there is still space made for this title.

+ theme(axis.title.x=element_blank())
like image 9
Didzis Elferts Avatar answered Oct 23 '22 14:10

Didzis Elferts


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")
like image 5
lukeA Avatar answered Oct 23 '22 15:10

lukeA


Try this function:

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 crop the white space in the resulting pdf file once created. In Unix, the system command is:

pdfcrop filename.pdf filename.pdf

pdfcrop does work on Mac provided 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))
like image 2
Brad Horn Avatar answered Oct 23 '22 15:10

Brad Horn