【问题标题】:Margins for ggplot2 facet labels much too large when using high font size使用大字体时,ggplot2 构面标签的边距太大
【发布时间】:2017-08-28 22:15:25
【问题描述】:

当增加字体大小以补偿 rmarkdown 中的高 dpi 渲染时,facet wrap ggplot 图像的标签具有异常大的边距。在下面的示例中,“D”、“E”等上方的垂直空间是我想要降低的。我尝试更改 element_text 边距以及 panel.spacing 主题参数。将这些设置为零不会改变太多。

代码

---
title: "PNG facet wrap test"
output:
  word_document: default
  html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning=FALSE, message =FALSE,fig.height=4)
knitr::opts_chunk$set(fig.showtext=TRUE)
knitr::opts_chunk$set(fig.width=6.5, fig.height = 4, out.width = 6.5, out.height = 4)
knitr::opts_chunk$set(dev="png", dev.args=list(type="cairo", pointsize=36), dpi=300)

require(ggplot2)
require(dplyr)
```

## Example image

```{r highdpi}
dia = ggplot2::diamonds %>% filter(cut=="Ideal", clarity=="SI2", color!="J")
ggplot(dia, aes(x=carat, y=price)) + facet_wrap(~color) + geom_point() + theme_light(36)
```

输出

【问题讨论】:

  • 我认为 DPI 与您的问题无关,我认为区别在于 theme_light(36)theme_light(16)。您的输出是图形在具有这些主题选项的交互式会话期间显示在图形窗口中的方式。
  • 图片取自渲染的 Word 文档,而非交互式会话。
  • 是的,如果您只是在不使用块选项和 dpi 的交互式会话中运行绘图代码,您将获得类似的图像。因此dpi不是问题。问题是在第一个中您使用theme_light(36),而在第二个中您将其更改为theme_light(16)
  • 很公平,但我的问题是如何增加字体大小而不会在分面环绕标签周围留出巨大的边距?我会改写我的问题。
  • theme(strip.text.x = element_text(margin = margin(b = 0, t = 0) ) ) 做你想做的事吗?

标签: r ggplot2 knitr r-markdown


【解决方案1】:

ggplot2_2.2.1 中,您可以使用 strip.text.x 来更改带 margin 的边距。 (在开发版本中你可能可以做到这一点directly with strip.text)。

这里我将上下边距设为 0:

ggplot(dia, aes(x=carat, y=price)) + 
     facet_wrap(~color) + 
     geom_point() + 
     theme_light(36) +
     theme(strip.text.x = element_text( margin = margin( b = 0, t = 0) ) )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    相关资源
    最近更新 更多