jimhester / knitrBootstrap

A framework to create bootstrap styled HTML reports from knitr Rmarkdown.
Other
274 stars 61 forks source link

knitrBootstrap inappropriately changes ggplot's legend label spacing #74

Closed paullemmens closed 9 years ago

paullemmens commented 9 years ago

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](https://cloud.githubusercontent.com/assets/5063702/5052555/afeeeec0-6c41-11e4-9bf0-40e1bc3df4a4.png)

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](https://cloud.githubusercontent.com/assets/5063702/5052574/e6c5e264-6c41-11e4-83b2-b0c5e9441219.png)

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 commented 9 years ago

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 commented 9 years ago

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 https://github.com/jimhester/knitrBootstrap/issues/74#issuecomment-63123321 .

paullemmens commented 9 years ago

Hi @jimhester ,

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

Thanks!

jimhester commented 9 years ago

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