Skip to content

Instantly share code, notes, and snippets.

@DavZim
Created September 4, 2017 21:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavZim/a85e71ef10cdec0b594527c936bad159 to your computer and use it in GitHub Desktop.
Save DavZim/a85e71ef10cdec0b594527c936bad159 to your computer and use it in GitHub Desktop.
``` r
library(extrafont)
#> Registering fonts with R
# font_install('fontcm') # try this if its not working
suppressMessages(loadfonts())
# check that the LM Roman fonts are installed with
"CM Roman" %in% fonts()
#> [1] TRUE
# main script ============
library(ggplot2)
theme_set(theme_light(base_family = "LM Roman 10"))
ggplot(mtcars, aes(x = wt, y = mpg, color = as.factor(gear))) +
geom_point() +
labs(x = "Weight",
y = "Miles per Gallon",
title = "Comparison of Cars",
color = "Number\nof Gears")
```
![](https://i.imgur.com/9YUtnWS.png)
``` r
ggsave(filename = "myplot.pdf", device = cairo_pdf)
#> Saving 7 x 5 in image
sessionInfo()
#> R version 3.4.1 (2017-06-30)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 16.04.3 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/libblas/libblas.so.3.6.0
#> LAPACK: /usr/lib/lapack/liblapack.so.3.6.0
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=de_DE.UTF-8 LC_COLLATE=en_US.UTF-8
#> [5] LC_MONETARY=de_DE.UTF-8 LC_MESSAGES=en_US.UTF-8
#> [7] LC_PAPER=de_DE.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] ggplot2_2.2.1 extrafont_0.17
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_0.12.12 Rttf2pt1_1.3.4 knitr_1.17 magrittr_1.5
#> [5] munsell_0.4.3 colorspace_1.3-2 rlang_0.1.2 stringr_1.2.0
#> [9] plyr_1.8.4 tools_3.4.1 grid_3.4.1 gtable_0.2.0
#> [13] extrafontdb_1.0 htmltools_0.3.6 yaml_2.1.14 lazyeval_0.2.0
#> [17] rprojroot_1.2 digest_0.6.12 tibble_1.3.4 evaluate_0.10.1
#> [21] rmarkdown_1.6 labeling_0.3 stringi_1.1.5 compiler_3.4.1
#> [25] scales_0.5.0 backports_1.1.0
```
@DavZim
Copy link
Author

DavZim commented Sep 4, 2017

I have no idea why I have to specify LM Roman 10 instead of CM Roman 10 (LM Roman is not in fonts(), but they seem to be related).

Also make sure that you use the cairo-device for saving the ggplots, otherwise you will get lots of errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment