问题
I'm experiencing some behavior where knitr cuts off part of a ggplot2 image when i used coord_fixed(ratio = 1)
.
mwe.Rmd
---
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
```
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)
I looked at the PDFs produced by knitr and the top of the plot is a little cut off with g_fixed_1
:
compared to g_fixed_2
(two rows of data):
回答1:
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))
}
来源:https://stackoverflow.com/questions/51226339/knitr-cuts-off-coord-fixed-ggplot