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

using showtext leads to smaller font size in ggplot2 #51

Closed
ThierryO opened this issue Jul 2, 2021 · 8 comments
Closed

using showtext leads to smaller font size in ggplot2 #51

ThierryO opened this issue Jul 2, 2021 · 8 comments

Comments

@ThierryO
Copy link

ThierryO commented Jul 2, 2021

Reproducible example.

library(ggplot2)
ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point() + ggtitle("standard")

image

library(ggplot2)
showtext::showtext_auto()
ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point() +
  ggtitle("showtext, no font")

image

sessioninfo::session_info()
─ Session info ────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 4.1.0 (2021-05-18)
 os       Ubuntu 18.04.5 LTS          
 system   x86_64, linux-gnu           
 ui       RStudio                     
 language nl_BE:nl                    
 collate  nl_BE.UTF-8                 
 ctype    nl_BE.UTF-8                 
 tz       Europe/Brussels             
 date     2021-07-02                  

─ Packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version date       lib source        
 assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.1.0)
 cli           2.5.0   2021-04-26 [1] CRAN (R 4.1.0)
 colorspace    2.0-1   2021-05-04 [1] CRAN (R 4.1.0)
 crayon        1.4.1   2021-02-08 [1] CRAN (R 4.1.0)
 DBI           1.1.1   2021-01-15 [1] CRAN (R 4.1.0)
 digest        0.6.27  2020-10-24 [1] CRAN (R 4.1.0)
 dplyr         1.0.6   2021-05-05 [1] CRAN (R 4.1.0)
 ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.1.0)
 fansi         0.5.0   2021-05-25 [1] CRAN (R 4.1.0)
 farver        2.1.0   2021-02-28 [1] CRAN (R 4.1.0)
 fortunes      1.5-4   2016-12-29 [1] CRAN (R 4.1.0)
 generics      0.1.0   2020-10-31 [1] CRAN (R 4.1.0)
 ggplot2     * 3.3.3   2020-12-30 [1] CRAN (R 4.1.0)
 glue          1.4.2   2020-08-27 [1] CRAN (R 4.1.0)
 gtable        0.3.0   2019-03-25 [1] CRAN (R 4.1.0)
 labeling      0.4.2   2020-10-20 [1] CRAN (R 4.1.0)
 lifecycle     1.0.0   2021-02-15 [1] CRAN (R 4.1.0)
 magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.1.0)
 munsell       0.5.0   2018-06-12 [1] CRAN (R 4.1.0)
 pillar        1.6.1   2021-05-16 [1] CRAN (R 4.1.0)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.1.0)
 purrr         0.3.4   2020-04-17 [1] CRAN (R 4.1.0)
 R6            2.5.0   2020-10-28 [1] CRAN (R 4.1.0)
 rlang         0.4.11  2021-04-30 [1] CRAN (R 4.1.0)
 rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.1.0)
 scales        1.1.1   2020-05-11 [1] CRAN (R 4.1.0)
 sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.1.0)
 showtext      0.9-2   2021-01-10 [1] CRAN (R 4.1.0)
 showtextdb    3.0     2020-06-04 [1] CRAN (R 4.1.0)
 sysfonts      0.8.3   2021-01-10 [1] CRAN (R 4.1.0)
 tibble        3.1.2   2021-05-16 [1] CRAN (R 4.1.0)
 tidyselect    1.1.1   2021-04-30 [1] CRAN (R 4.1.0)
 utf8          1.2.1   2021-03-12 [1] CRAN (R 4.1.0)
 vctrs         0.3.8   2021-04-29 [1] CRAN (R 4.1.0)
 withr         2.4.2   2021-04-18 [1] CRAN (R 4.1.0)
@yixuan
Copy link
Owner

yixuan commented Jul 2, 2021

You may need to set showtext_opts(dpi = ...) using the resolution of your device.

@ThierryO
Copy link
Author

ThierryO commented Jul 2, 2021

Passing the dpi of the current chunk works. Although not very user friendly as I'm using showtext in a custom ggplot2 theme package (https://github.com/inbo/INBOtheme/blob/2a41272624e9092d24f0e92772bfebeef12c695c/R/theme_inbo2015.R#L54).

showtext::showtext_opts(dpi = opts_chunk$get()$dpi)
ggplot(mtcars, aes(x = mpg, y = disp)) + geom_point() +
  ggtitle(paste("showtext, knitr dpi", opts_chunk$get()$dpi))

image

@tomicapretto
Copy link

I'm experiencing the same problem. I just entered to the repo to report the same issue.

My code

good

library(ggplot2)
library(ggstatsplot)
library(palmerpenguins)
penguins <- tidyr::drop_na(penguins)

plt <- ggbetweenstats(
  data = penguins,
  x = species,
  y = bill_length_mm,
  title = "Distribution of bill length across penguins species",
  xlab = "Penguins Species",
  ylab = "Bill Length"
)

ggsave("plot_good.png", plt, width = 8, height = 8)

plot_good

bad

library(ggplot2)
library(ggstatsplot)
library(palmerpenguins)
library(showtext)
showtext_auto()

penguins <- tidyr::drop_na(penguins)

plt <- ggbetweenstats(
  data = penguins,
  x = species,
  y = bill_length_mm,
  title = "Distribution of bill length across penguins species",
  xlab = "Penguins Species",
  ylab = "Bill Length"
)

ggsave("plot_bad.png", plt, width = 8, height = 8)

plot_bad

@yixuan
Copy link
Owner

yixuan commented Jul 3, 2021

@ThierryO @tomicapretto 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.

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.

@tomicapretto
Copy link

Thanks so much for the answer @yixuan!

@ThierryO
Copy link
Author

I don't know whether the end user will use my code (custom ggplot2 theme) in a plain R script or an Rmd. What would you recommend to keep it user friendly?

@yixuan
Copy link
Owner

yixuan commented Jul 14, 2021

@ThierryO I haven't tried this myself, but probably you can take a look at the thematic package, which also uses showtext and supports theming.

I guess you can always call showtext_auto() inside your code, but users who run Rmd need to add fig.showtext = TRUE in their own documents.

@SchmidtPaul
Copy link

SchmidtPaul commented Jul 29, 2022

Hi, I made a reprex just for completeness as this had been confusing me for quite some time. Thanks for your answer @yixuan!

library(cowplot)
library(ggplot2)
library(pdftools)
library(png)
library(showtext)

p <- ggplot(data = PlantGrowth,
             aes(y = weight, x = group)) +
  geom_point() +
  labs(title = "This is the title",
       subtitle = "Here is also a subtitle",
       caption = "And here is a caption")

# Standard - not involving {showtext} -------------------------------------
ggsave("p1.png", p)
#> Saving 7 x 5 in image
ggsave("p1.pdf", p)
#> Saving 7 x 5 in image

png1 <- readPNG("p1.png")
pdf1 <- pdf_render_page("p1.pdf", page = 1, dpi = 300)

plot_grid(ggdraw() + draw_image(png1) + draw_label("png"),
          ggdraw() + draw_image(pdf1) + draw_label("pdf"))

✔️ This works fine ✔️

# showtext::showtext_auto() -----------------------------------------------
showtext::showtext_auto()

ggsave("p2.png", p)
#> Saving 7 x 5 in image
ggsave("p2.pdf", p)
#> Saving 7 x 5 in image

png2 <- readPNG("p2.png")
pdf2 <- pdf_render_page("p2.pdf", page = 1, dpi = 300)

plot_grid(ggdraw() + draw_image(png2) + draw_label("png"),
          ggdraw() + draw_image(pdf2) + draw_label("pdf"))

❌ This does not work ❌

# showtext::showtext_auto() & showtext_opts(dpi = 300) --------------------
showtext::showtext_auto()
showtext_opts(dpi = 300)

ggsave("p3.png", p)
#> Saving 7 x 5 in image
ggsave("p3.pdf", p)
#> Saving 7 x 5 in image

png3 <- readPNG("p3.png")
pdf3 <- pdf_render_page("p3.pdf", page = 1, dpi = 300)

plot_grid(ggdraw() + draw_image(png3) + draw_label("png"),
          ggdraw() + draw_image(pdf3) + draw_label("pdf"))

✔️ This works fine ✔️

Created on 2022-07-29 by the reprex package (v2.0.1)

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

4 participants