Problems with `coord_polar()` pdf alignment.

Problem with pdf plot alignment.

With coord_polar() and theme_void() there is strange alignmet, even when elements are present that are white. When adding a geom_hline() if colour = "white" then alignment fails, if colour = black then alignment is successful.

Is a problem when knitting to .pdf. I have knit to two columns to get it all onto one page for a screenshot, but the problem persists in single column. The same alignment problem happens when a coord_polar() plot has asymetric labels on one side, with fig.align="center" the alignment is off.

---
output: pdf_document
classoption:
- twocolumn
---

```{r, echo = FALSE}
knitr::opts_chunk$set(fig.height = 2)
```


```{r, message=FALSE}
library(ggplot2)

## Normal Plot
p1 <- ggplot(diamonds) + 
  aes(x = carat) + 
  geom_histogram() + 
  coord_polar()
p1

## `theme_void()` means strange alignment
p2 <- p1 + theme_void()
p2

## Adding a line fixes alignment
line <- 12000
p3 <- p2 + geom_hline(yintercept = line)
p3


## Adding a white line breaks alignment
p2 + geom_hline(yintercept = line, 
                colour = "white")

## Adding a white line over a black line fixes alignment
p3 + geom_hline(yintercept = line, 
                colour = "white", 
                size = 3)
```
> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS  10.16

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_4.0.2    htmltools_0.5.1.1 tools_4.0.2       yaml_2.2.1       
 [5] rmarkdown_2.8     knitr_1.33        xfun_0.23         digest_0.6.27    
 [9] rlang_0.4.11      evaluate_0.14    

pdf_document() will default to fig_crop = "auto" (in last dev version, TRUE in previous versions). This means figures will be cropped by pdfcrop tool. As you are using white background and not axis, I believe this is causing some issues. See Convert to a PDF/LaTeX document — pdf_document • rmarkdown

You need to set

output: 
  pdf_document:
    fig_crop: FALSE

So that cropping does not happen with PDF. I believe you could get better result.

In most case, cropping figures is useful and works as expected but in this case with no X / Y axis to help the detection, it seems this is not happening as expected.

Hope it helps

2 Likes

This does indeed fix the problem! Thank you so much for the answer :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.