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

Legend spacing is changed comparing Rmd -> HTML render with regular R console output #408

Closed
paullemmens opened this issue Mar 27, 2015 · 6 comments

Comments

@paullemmens
Copy link

The change is minimal and difficult to spot, but in the figures below, look at the spacing between the last characters of each legend string and the box with, for instance, the colored dot. Note that using knitrBootstrap exacerbates the situation but the principled problem lies with rmarkdown.

The example below is a bit difficult, but I have had real life examples where there's actually overlap of the legend names and legend boxes (see below).

Steps to reproduce (and sessionInfo() all the way at the bottom.

  1. Render the plot from the Rmd code below in the console (with default 7x7in dimension).
  2. Render the following Rmd file and open html file
---
output:
  html_document:
    title: "test"
    theme: default

---

```{r, fig.width = 7, fig.height = 7}
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')

When you compare figure 1 and 2, you'll see that the box of both plots has the same size. I've taken a screen shot with both plots partially overlapping and highlighted the problem points. Although there is a bit of a difference in scaling, the arrows show that the width of the full legend is different and the boxes highlight where the text and color box is much closer in the HTML file.

I can understand that there may be some overall scaling needed for the rmarkdown processing, but I don't think that should affect the spacing of the (full) legend box.

screenclip

The following screen grab is from a more realistic plot. It clearly shows that the d from planned hits the box from booked.

image

R version 3.1.2 (2014-10-31)
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                           LC_TIME=English_United States.1252    

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

other attached packages:
[1] knitrBootstrap_1.0.0 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.2       gtable_0.1.2     htmltools_0.2.6  knitr_1.7.8      labeling_0.3     markdown_0.7.4   MASS_7.3-35     
[12] mime_0.2         munsell_0.4.2    plyr_1.8.1       proto_0.3-10     Rcpp_0.11.3      reshape2_1.4     scales_0.2.4     stringr_0.6.2    tools_3.1.2      yaml_2.1.13     
@jjallaire
Copy link
Member

One thing that might be causing this are the transformations related to the
fig_retina option (which in theory should be entirely neutral but perhaps
there's some subtle change in behavior). What happens if you set fig_retina
to null?

On Fri, Mar 27, 2015 at 7:38 AM, paullemmens notifications@github.com
wrote:

The change is minimal and difficult to spot, but in the figures below,
look at the spacing between the last characters of each legend string and
the box with, for instance, the colored dot. Note that using
knitrBootstrap exacerbates the situation but the principled problem lies
with rmarkdown.

The example below is a bit difficult, but I have had real life examples
where there's actually overlap of the legend names and legend boxes (see
below).

Steps to reproduce (and sessionInfo() all the way at the bottom.

  1. Render the plot from the Rmd code below in the console (with
    default 7x7in dimension).
  2. Render the following Rmd file and open html file

---output:
html_document:
title: "test"
theme: default---

                  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')

When you compare figure 1 and 2, you'll see that the box of both plots has
the same size. I've taken a screen shot with both plots partially
overlapping and highlighted the problem points. Although there is a bit of
a difference in scaling, the arrows show that the width of the full legend
is different and the boxes highlight where the text and color box is much
closer in the HTML file.

I can understand that there may be some overall scaling needed for the
rmarkdown processing, but I don't think that should affect the spacing of
the (full) legend box.

[image: screenclip]
https://cloud.githubusercontent.com/assets/5063702/6869660/02bb6c16-d496-11e4-9d9a-ae832ab8bb5f.png

The following screen grab is from a more realistic plot. It clearly shows
that the d from planned hits the box from booked.

[image: image]
https://cloud.githubusercontent.com/assets/5063702/6869760/d7211be0-d496-11e4-93da-2f90c730e28a.png

R version 3.1.2 (2014-10-31)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                           LC_TIME=English_United States.1252
attached base packages:
[1] graphics  grDevices utils     datasets  stats     methods   base
other attached packages:
[1] knitrBootstrap_1.0.0 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.2       gtable_0.1.2     htmltools_0.2.6  knitr_1.7.8      labeling_0.3     markdown_0.7.4   MASS_7.3-35
[12] mime_0.2         munsell_0.4.2    plyr_1.8.1       proto_0.3-10     Rcpp_0.11.3      reshape2_1.4     scales_0.2.4     stringr_0.6.2    tools_3.1.2      yaml_2.1.13

—
Reply to this email directly or view it on GitHub
https://github.com/rstudio/rmarkdown/issues/408.

@paullemmens
Copy link
Author

I've checked your suggestion, but it does not make a difference. Note that I checked both fig.retina and fig_retina (which you suggested).

@jjallaire
Copy link
Member

Them I'm not sure what might be causing this. @yihui any ideas?

@yihui
Copy link
Member

yihui commented Mar 29, 2015

@paullemmens Per suggestion of knitr FAQ 1 (http://bit.ly/knitr-faq), could you update your packages first? I do not see any difference between the two plots:

The plot in the X11 window:

screenshot from 2015-03-28 20 01 08

The plot in the HTML output from R Markdown:

screenshot from 2015-03-28 20 01 27

I tried it with and without fig_retina: null.

---
output:
  html_document:
    fig_retina: null
    theme: default
---

@paullemmens
Copy link
Author

@yihui I ran the update and then I indeed get the same output in the console (graph) and in HTML.

So I started adding packages and options. While doing that, I realized that I have this typical knitr chunk that I just cut & paste across documents in which I set my default device to CairoSVG. When I switched on this option in my sample code, things went south again:

image

So Cairo appears to be the culprit! Thanks for thinking along!

@github-actions
Copy link

github-actions bot commented Nov 3, 2020

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants