Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

knitrBootstrap inappropriately changes ggplot's legend label spacing #74

Closed
paullemmens opened this issue Nov 14, 2014 · 4 comments
Closed

Comments

@paullemmens
Copy link

For already quite a while I've noticed that in my (both) knitr and rmarkdown documents rendered with/using knitrBootstrap the labels of the legends of ggplot plots tend to overlap. The overlap is minimal, but visually present and is absent when making the same plot in the console. I must admit that the space seems to be shrunk when also (only) compiling using rmarkdown (or knitr), but the shrinking is much smaller.

The minimal Rmd is the following:


---
output:
  knitrBootstrap::bootstrap_document:
    title: "test"
    theme: default

---

```{r}
dat <- data.frame(x = 1:4, 
                  y = c(4, 3, 1, 2), 
                  fac = factor(rep(c('a', 'b'), 2), 
                               labels = c('longest label (a)', 'longest label (b)')))
p <- ggplot(data = dat, mapping = aes(x = x, y = y, colour = fac)) +
  geom_point() +
  scale_colour_discrete(name = 'long legend name', 
                         guide=guide_legend(ncol = 3, byrow=TRUE, 
                                           title.position='left', 
                                           title.hjust=0))
p + theme(text=element_text(size=18), 
          axis.title.x=element_text(vjust=-0.2), 
          axis.title.y=element_text(angle=90, vjust=1),
          panel.border=element_rect(colour='black', size=1.2),
          legend.position='bottom')
```

If you run this code chunk in the console, then following image is the result.

console

If you render the file using knitrBootstrap and rmarkdown (see below for versions), then the image becomes. Notice how the legend title overlaps with the first legend color box.

knitrbootstrap

It is related to changing the font size of the legend title as well as the legend labels. Especially when I enlarge the label font size, the labels also start overlapping with the next color box. However, it remains the case that, even with larger label sizes, the overlapping never happens when plotting in the console. It's only a small detail, but it bugs me visually as well as out of curiosity I want to if and how to fix it.

When I said, at the top, 'for already quite a while', I meant that this happened for runs with knitr only (I think, if possible, even) in R 2.x, R 3.x, and especially with knitrBootstrap both in the CRAN and Github version.

R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] Cairo_1.5-6      rmarkdown_0.3.10 ggplot2_1.0.0   

loaded via a namespace (and not attached):
 [1] colorspace_1.2-4     digest_0.6.4         evaluate_0.5.5       formatR_1.0          grid_3.1.1           gtable_0.1.2         htmltools_0.2.6      knitr_1.7.1         
 [9] knitrBootstrap_1.0.0 labeling_0.3         markdown_0.7.4       MASS_7.3-33          mime_0.2             munsell_0.4.2        plyr_1.8.1           proto_0.3-10        
[17] Rcpp_0.11.3          reshape2_1.4         scales_0.2.4         stringr_0.6.2        tools_3.1.1          yaml_2.1.13         
@jimhester
Copy link
Owner

Is this knitrBootstrap from CRAN or github?

I am guessing this is due to knitr resizing the overall image size, while the font size remains at the same pointsize, try playing with fig.width knitr option to see if it fixes the issue. Also you may be able to fix the problem by adjusting title.hjust=0 in guide_legend.

There really shouldn't be any reason that knitrBootstrap would exacerbate this problem, it doesn't do anything additional to the image output than what knitr would do.

@paullemmens
Copy link
Author

Hi Jim,

This is the github version. I totally agree and don't quite understand why
bootstrap would exacerbate, but it seems to do. Next week I'll try and
concoct an even clearer example.

Regarding the hjust, you're right: it makes the problem less, but I have
also seen it with the actual labels of the color scale. I'll try your other
suggestions next week.

Thnx!

On Fri, Nov 14, 2014, 21:20 Jim Hester notifications@github.com wrote:

Is this knitrBootstrap from CRAN or github?

I am guessing this is due to knitr resizing the overall image size, while
the font size remains at the same pointsize, try playing with fig.width
knitr option to see if it fixes the issue. Also you may be able to fix the
problem by adjusting title.hjust=0 in guide_legend.

There really shouldn't be any reason that knitrBootstrap would exacerbate
this problem, it doesn't do anything additional to the image output than
what knitr would do.


Reply to this email directly or view it on GitHub
#74 (comment)
.

@paullemmens
Copy link
Author

Hi @jimhester ,

It's been quite a while but I tracked it down to actually being a Cairo (package) issue. See rstudio/rmarkdown#408 for reference.

Thanks!

@jimhester
Copy link
Owner

Glad you figured it out! Thank you for coming back and closing this as well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants