I want to style ggplot2 axis labels, by making some text bold. Ideally I would like to control the font size too. Is it all possible? Here is the example of what I am trying to do:
qplot(x = x, y = y, data = data.frame(x = rnorm(10), y = rnorm(10))) +
labs(x = "14pt Bold text \n12pt normal text")
So instead of 14pt Bold text
I want 14pt bold font, and for 12pt normal text I want 12pt normal text.
I've googled for examples and all I found is the way to change the appearance of all the label, or using plotmath
expressions, which strangely had no effects on ggplot2, i.e doing labs(x=expression("bold(Bold text)"))
had no effect.
Update
As always I tried to ask a too general question. The full example I want to achieve also involves using custom fonts. The fonts I am using are AvenirNextLTPro, and the look I am going for is the following:
library(showtext)
library(ggplot2)
font.add("AvenirNextLTPro",
regular = "AvenirNextLTPro-Regular.otf",
bold="AvenirNextLTPro-Demi.otf",
italic = "AvenirNextLTPro-It.otf",
bolditalic = "AvenirNextLTPro-DemiIt.otf")
showtext.auto()
ggplot(data = data.frame(x = rnorm(10), y = rnorm(10)), aes(x = x, y = y)) +
geom_point() +
labs(x = "14pt Bold text \n12pt normal text",
y = "14 pt Bold text\n\n 12pt\nnormal\ntext") +
theme(axis.title.y = element_text(angle = 0, hjust = 1, family = "AvenirNextLTPro"), axis.title.x = element_text(family = "AvenirNextLTPro"))
Note that for this to work you need to install the fonts, i.e. they should be visible for font.files()
. For Mac OS X this can be achieved by opening the Font app and adding the downloaded fonts.