What is the default font for ggplot2
Solution 1
This is not a complete answer, but some of the pieces are as follows:
- Themes are complicated: http://ggplot2.tidyverse.org/reference/theme.html
-
theme_set()
andtheme_get()
can set and query your default theme for the session - A theme is a list, and the named
text
list and itsfamily
element will tell you want font name is being used. You can set that in the theme call in a ggplot to override the default for that theme. - In windows the
windowsFonts()
will tell you the mapping between font names and your installed system font names. I have no idea how to do this in the OS X or Linux platforms though. - The default ones seem to be
serif
,sans
, andmono
across all platforms.
My main open question is what does the blank family name "" map to, since that is usually what is in the themes. If nothing is specified the default somehow gets set to sans
, since that it what it seems to be.
I also couldn't figure out how to replace the default font family in theme_gray()
. Think it is time for a question...
> theme_get()$text
List of 10
$ family : chr ""
$ face : chr "plain"
$ colour : chr "black"
$ size : num 11
$ hjust : num 0.5
$ vjust : num 0.5
$ angle : num 0
$ lineheight: num 0.9
$ margin :Classes 'margin', 'unit' atomic [1:4] 0 0 0 0
.. ..- attr(*, "unit")= chr "pt"
.. ..- attr(*, "valid.unit")= int 8
$ debug : logi FALSE
- attr(*, "class")= chr [1:2] "element_text" "element"
And the system font mapping in windows:
> windowsFonts()
$serif
[1] "TT Times New Roman"
$sans
[1] "TT Arial"
$mono
[1] "TT Courier New"
>
An example:
library(ggplot2)
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
ggtitle("Fuel Efficiency of 32 Cars") +
xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
theme(text=element_text(size=16, family="serif"))
Solution 2
If you export a figure created using ggplot2 (using RStudio: Export -> Copy to Clipboard) and load it into a graphics editor you can select and edit each individual aspect of the figure, including text.
Using Inkscape, the default font for all my ggplot2 plots is Arial.
h.l.m
Updated on March 24, 2020Comments
-
h.l.m about 4 years
When using ggplot2 in R, it clearly has a
base_family
font. What is it? as the documentation just uses "" as can be seen in...?theme_light()
It looks like ArialMT but not sure...
-
Oliver Bock over 7 yearsI think you can change the default font like this:
theme_set(theme_get() + theme(text = element_text(family = 'Open Sans')))
-
Adam_G almost 7 yearsThe link at the beginning of the answer is broken
-
Mike Wise almost 7 yearsOh, thanks for that. Now if I could only remember what it was :)
-
Artem Sokolov over 6 yearsThe equivalent to
windowsFonts()
in Linux isX11Fonts()
. -
deeenes over 5 yearsI think it is actually "sans" which defaults to Arial on many systems, but this really depends on the operating system and settings, on Linux ggplot will retrieve the default sans from fontconfig I guess (which will return Bitstream Vera Sans or Deja Vu Sans on a typical Linux system).
-
Hobo almost 5 yearsAnother option is to
ggsave
assvg
, and load in a text editor. Search for a<text
element'sfont-family
value