【问题标题】:How to get rid of whitespace in a ggplot2 plot?如何摆脱 ggplot2 图中的空白?
【发布时间】:2014-03-01 20:30:07
【问题描述】:

我正在为出版物准备一个人物。我通过设置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()

【问题讨论】:

    标签: r ggplot2 whitespace


    【解决方案1】:

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

    【讨论】:

    • 自@lukeA 的回答以来这可能已经改变,但似乎 plot.margins 调用的参数分别影响'top'、'right'、'bottom'、'left'。
    【解决方案2】:

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

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

    【讨论】:

    • 这实际上并没有删除空白,而plot.margin 解决方案可以(尽管以一种笨拙且特定于情节的方式)。
    • @JackWasey 此解决方案删除为标题分配的空间,并在轴标签和绘图底部之间留出一些空间。要删除所有空间,是的,您还必须使用plot.margin。但问题是必须删除轴标题的空格。
    • 对不起,你是对的。 xlab(NULL) 似乎与 Hadley 所写的结果相同。
    • 对于我正在处理的情节,添加title = element_blank() 似乎有助于顶部和底部的空白。我的代码:+ theme(axis.title.x = element_blank(), axis.title.y = element_blank(), title = element_blank())
    【解决方案3】:

    试试这个功能:

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

    【讨论】:

      【解决方案4】:

      你也可以在 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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-21
        • 2021-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多