Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

r - ggplot2: Font Style in label expression

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")) ) ) )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...