针织物切断coord_fixed ggplot [英] knitr cuts off coord_fixed ggplot

查看:70
本文介绍了针织物切断coord_fixed ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一些行为,当我使用coord_fixed(ratio = 1)时,knitr会切断ggplot2图像的一部分.

I'm experiencing some behavior where knitr cuts off part of a ggplot2 image when i used coord_fixed(ratio = 1).

---
title: "mwe"
output: pdf_document
---

```{r}
library(ggplot2)

data <- list(
  x1 = 0:6,
  y1 = 0,
  x2 = rep(0:6, 2),
  y2 = rep(c(0, 1), each = 7) * -1,
  labels = paste("Label", 1:7)
)

g <- ggplot() + geom_text(aes(0:6, 0.75), label = data$labels)

g_unfixed_1 <- g + geom_raster(aes(data$x1, data$y1))
g_fixed_1 <- g_unfixed_1 + coord_fixed(ratio = 1)
g_unfixed_2 <- g + geom_raster(aes(data$x2, data$y2))
g_fixed_2 <- g_unfixed_2 + coord_fixed(ratio = 1)

g_unfixed_1
g_fixed_1
g_unfixed_2
g_fixed_2
```

通过以下调用,我可以看到g_fixed_1的上边距与g_fixed_2的上边距相比略有减少:

Called with the the following, I can see the top margin for g_fixed_1 is slightly cut off compared to the top margin for g_fixed_2:

rmarkdown::render("mwe.Rmd", clean = F)

我看了knitr制作的PDF,图的顶部被g_fixed_1剪断了:

I looked at the PDFs produced by knitr and the top of the plot is a little cut off with g_fixed_1:

g_fixed_2(两行数据)相比:

推荐答案

我遇到了相同的问题,并使用以下方法解决了该问题:

I have the same problem and solved it using:

p + scale_y_continous(limits = c(0, max(value)*1.1))

在我的示例中,我在一个绘图文件上进行了循环,如下所示:

In my example I was doing a loop over a file of plots, so was something like this:

    for(n in names(tabla)) {
    plots_ms[[n]] +
      scale_y_continuous(limits = c(0, max(plots_ms[[n]]$data$ms)*1.1))
}

这篇关于针织物切断coord_fixed ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆