如何摆脱ggplot2情节中的空白?

时间:2014-02-05 12:03:41

标签: r ggplot2 whitespace

我正在为出版物准备一个人物。我通过设置xlab("")来省略x标签,但是ggplot2会生成一个空格而不是完全删除标签。如何摆脱空白(在下图中用红色矩形标记)?

enter image description here

完整代码:

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

3 个答案:

答案 0 :(得分:5)

您是否尝试过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")

答案 1 :(得分:5)

使用theme()删除为x轴标题分配的空间。当您设置xlab("")时,仍有空间用于此标题。

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

答案 2 :(得分:2)

尝试此功能:

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

您还可以在创建后在生成的pdf文件中裁剪空白区域。在Unix中,系统命令是:

pdfcrop filename.pdf filename.pdf

如果安装了标准的LaTeX发行版(Mactex或texlive),pdfcrop可以在Mac上运行。当然,这个命令可以在R中执行,如下所示:

system(paste("pdfcrop", filename, filename))