繁体   English   中英

如何摆脱ggplot2图中的空格?

[英]How to get rid of whitespace in a ggplot2 plot?

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

在此处输入图片说明

完整代码:

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

你试过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")

使用theme()删除为x轴标题分配的空间。 当你设置xlab("") ,仍然有空间为这个标题。

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

试试这个功能:

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

您还可以在 labs() 中设置 x = NULL

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM