2014-02-05 72 views
6

我正在準備出版物的數字。我正在通過設置xlab("")來省略x標籤,但是ggplot2會生成一個空格而不是完全刪除標籤。我如何擺脫空白(在下面的圖中用紅色矩形標記)?如何擺脫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() 
+0

試試'xlab(NULL)'' – hadley

回答

5

使用theme()刪除分配給x軸標題的空間。當您設置xlab("")時,此標題仍有空間。

+ theme(axis.title.x=element_blank()) 
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") 
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 

pdfcrop不工作在Mac中提供的標準膠乳分佈(MacTEX都或的texlive)安裝。當然,這個命令可以在R中執行如下:

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