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

loading {dlookr} in RMarkdown changes ggplot font size? #79

Open
SchmidtPaul opened this issue Oct 9, 2022 · 9 comments
Open

loading {dlookr} in RMarkdown changes ggplot font size? #79

SchmidtPaul opened this issue Oct 9, 2022 · 9 comments

Comments

@SchmidtPaul
Copy link

Hi, I stumbled upon an unexpected outcome while using {dlookr} in RMarkdown. Basically, this code:

---
title: "reprex"
output: html_document
---

```{r, message=FALSE}
library(dlookr) # with or without 
library(ggplot2)
ggplot(mtcars) +
  aes(y = mpg, x = cyl) +
  geom_point()
```

will create ggplots with different font sizes depending on whether library(dlookr) is included or not. Is it possible that loading {dlookr} changes the parameters for fig.width or similar?!

image

image

@choonghyunryu
Copy link
Owner

@SchmidtPaul

When loading the dlookr package, the fonts are also loaded, which seems to have caused a side effect.
I'll try to find a way to solve the problem.

Thanks for your report.

@SchmidtPaul
Copy link
Author

Thank you! This is just a wild guess, as I have not looked at your code, but if you are loading {showtext}, then this issue may be related to yixuan/showtext#51

@choonghyunryu
Copy link
Owner

choonghyunryu commented Oct 11, 2022

@SchmidtPaul
Thank you so much!

As your information, it seems to be an issue with the showtext package.
Thanks for providing a clue to solving the problem.

sorry my miss typo
Let's find a way to solve it inside dlookr.

I will find a way to solve the problem inside dlookr.

@SchmidtPaul
Copy link
Author

SchmidtPaul commented Oct 12, 2022

@choonghyunryu
Well it really seems to be the same issue, because adding showtext::showtext_opts(dpi = 300) (as suggested in the linked thread above) solved the problem.

---
title: "reprex"
output: html_document
---

```{r, message=FALSE}
library(dlookr) # with or without 
showtext::showtext_opts(dpi = 300)
library(ggplot2)
ggplot(mtcars) +
  aes(y = mpg, x = cyl) +
  geom_point()
```

image

@choonghyunryu
Copy link
Owner

@SchmidtPaul

sorry for my miss typo in the previous.
Let's find a way to solve it inside dlookr.

I will find a way to solve the problem inside dlookr.

But you kindly provided an answer.
Thanks for providing a clear solution.

I use a mac, and the problem has not occurred on mac.
However, I reproduced the problem occurring on Windows and Linux.

I used your solution as follows.
showtext::showtext_opts (dpi = 300)

image

The problem that the font was printed very small has been resolved.
However, the font feels a bit large.

In the visualization function provided by the dlookr package, the font becomes very large.

library(dlookr) # with or without
showtext::showtext_opts (dpi = 300)

plot_outlier(heartfailure)

image

dpi=150 seems reasonable, like this:

library(dlookr) # with or without
showtext::showtext_opts (dpi = 150)

plot_outlier(heartfailure)

image

By the way, this example feels a bit small.

library(dlookr) # with or without
showtext::showtext_opts (dpi = 150)
library(ggplot2)
ggplot(mtcars) +
  aes(y = mpg, x = cyl) +
  geom_point()

image

I will solve the problem by running the following script when loading dlookr in the next version.

showtext::showtext_opts (dpi = 150)

What do you think of using dpi=150?

@SchmidtPaul
Copy link
Author

SchmidtPaul commented Oct 13, 2022

Thank you for investigating!
You are right, your dpi=300 figures have too large font sizes. Mine do, too, as I now figured out. I've attached a zip-file containting the single html-file I created via RMarkdown with differen dpi = levels and at least for me it seems that dpi = 200 looks closest to the default plot that was created before loading {dlookr}.
20221013-dlookr.zip

I usually go with dpi = 300 because, ggplot2::ggsave() has a default setting of 300 dpi according to this comment:

On the other hand, if you use showtext in pure R code, always set dpi according to your device parameter. For example, ggsave has a default value of 300 for dpi.

In the same comment, however, is a link that I had not taken seriously before, but which deals with my problem right now:

When using showtext inside R markdown, do not explicitly call showtext_auto(). Instead, use the knitr option fig.showtext=TRUE, which will correctly set the dpi parameter. See the short example here.

So now I think I understand what to do if I want to have ggplots in my RMarkdown after loading {showtext}. The problem, however, is that I did not actually (want to) load {showtext}, I just loaded {dlookr}. So I currently do not know how this can be solved. Naturally, I should not have to set fig.showtext=TRUE for all chunks after loading {dlookr}.

Just to give you the full picture: Here is the actual document where I came across this issue. I briefly use describe() and later on I create ggplots. In this specific example, I've now actually solved the problem for me by secretly not loading {dlookr} and instead doing this:

```{r}
#| eval: false
library(dlookr)

PlantGrowth %>%
  group_by(group) %>%
  describe(weight)
```

```{r}
#| echo: false
PlantGrowth %>%
  group_by(group) %>%
  dlookr::describe(weight)
```

And this is a good enough solution for now, but obviously not optimal.

@choonghyunryu
Copy link
Owner

@SchmidtPaul,

Thank you for your kind explanation.
For now, your solution is the best.

The dlookr package uses several fonts to add more aesthetics to the plots provided by the dlookr package.

When the dlookr package is loaded, it loads some fonts together and activates the showtext package.

This is invisible, but happens when the dlookr package is loaded. The following code is executed when the dlookr package is loaded.:

.onAttach <- function(libname, pkgname) {
  tryCatch(
    expr = {
      # Online font
      # English font
      sysfonts::font_add_google(name = "Roboto Condensed", 
                                family = "Roboto Condensed") 
      
      # Korean font
      sysfonts::font_add_google(name = "Noto Sans KR", 
                                family = "Noto Sans Korean") 
      
      options(dlookr_offline = FALSE)
    },
    error = function(e){ 
      options(dlookr_offline = TRUE)
      
      packageStartupMessage("Because it is an offline environment, only offline fonts are imported.")
    },    
    finally = {
      # Offline font
      # English font
      font_path <- system.file("fonts", "LiberationSansNarrow", 
                               package = "dlookr")
      sysfonts::font_add(
        family = "Liberation Sans Narrow",
        regular = paste(font_path, "LiberationSansNarrow-Regular.ttf", 
                        sep = "/")
      )
      
      # Korean font
      font_path <- system.file("fonts", "NanumSquare", package = "dlookr")
      sysfonts::font_add(
        family = "NanumSquare",
        regular = paste(font_path, "NanumSquareOTF_acR.otf", sep = "/")
      )
    }
  )  
  
  showtext::showtext_auto()
}

I am considering modifying this logic for the following reasons:

  • As you tell me, as a matter of fact, side effects do occur.
  • It takes a long time to load the package.

However, this task needs to be modified with all the visualization functions provided by dlookr.
So I plan to implement it in the next major version upgrade.

I would like to thank your dlookr for a lot of interest in this Issue.
And I'm impressed with your fantastic "Data Science for Agriculture in R". And I am very honored to introduce the dlookr package to your website.

I'll try to make the dlookr package more useful and work without side effects.

@SchmidtPaul
Copy link
Author

Hi @choonghyunryu,
With this post, I simply want to keep you updated regarding this.
I still find that dlookr::describe() is the best function for getting descriptive statistics that fit my requirements. However, because of different cases where the issue mentioned in this thread kept on bothering me, I have now created a function in my own package that more or less mimics yours: https://schmidtpaul.github.io/BioMathR/reference/describe.html

I know that your dlookr::describe() is still better and has more features than my miniature copy BioMathR::describe(). However, in certain projects, I could not avoid loading dlookr and thus face the problems of this issue here.

Note, however, that when teaching workshops, I currently still promote your instead of my version.

Cheers,
Paul

@choonghyunryu
Copy link
Owner

@SchmidtPaul,

I will consider the ability to allow the user to adjust the font size in the dlookr package visualization functions.
Best wishes for the growth of your wonderful BioMathR package.

Cheers,
Ryu

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