Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: Font Style in label expression

Tags:

r

ggplot2

Changing font faces and sizes works well without expression in the labels. Here is the code where I've problem to change the font faces in label expression.

p <- ggplot(data = mtcars, aes(x=wt, y=mpg)) + 
    geom_point() + 
    labs(x="Weight", y=expression(paste("mpg (  ", m^{-2}, ")"))) + 
    theme(axis.title.x = element_text(family="serif", face="bold", 
        size=12, angle=00, hjust=0.54, vjust=0)) + 
    theme(axis.title.y = element_text(family="serif", face="bold", 
        size=12, angle=90, vjust=0.25))

p

Output

Any help would be highly appreciated. Thanks

like image 483
MYaseen208 Avatar asked Nov 26 '11 21:11

MYaseen208


People also ask

What is the font used in ggplot2?

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.

What is the default font size in ggplot2?

The base font size is 11 pts by default. You can change it with the base_size argument in the theme you're using.

Can you change font in ggplot?

ggplot allows you to change the font of each part of the figure: you just need to know the correct option to modify in the theme. (For a full list of customizable components of the theme, see this documentation.)


1 Answers

I do not think it is the presence of expression, although you don't say exactly what it is that you are seeing so it's difficult to be sure. On a Mac you get the xlab in bold-serif font, but the ylab is not bold (although it is in serif). If I use the plotmath bold it succeeds from inside the expression function:

p <- ggplot(data = mtcars, aes(x=wt, y=mpg)) + 
geom_point() + 
    labs(x = "Weight", y = expression(bold(mpg (m^-2) ))) +
    theme(axis.title.y = element_text(family="serif", 
        size = 12, angle = 90, vjust = 0.25)) +
    theme(axis.title.x = element_text(family="serif",
        size = 12, angle = 0, hjust = 0.54, vjust = 0))
p

As always, graphical devices are often OS-variable and you have not offered any hint about yours, so further commentary is inhibited.

(EDIT: If you quote the exponent -2 you can get it bold()-ed as well.)

expression( bold(mpg (m^bold("-2")) ) ) )
like image 106
IRTFM Avatar answered Oct 14 '22 00:10

IRTFM