ggplot2: Font Style in label expression

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

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() p <- p + labs(x="Weight", y=expression(paste("mpg (  ", m^{-2}, ")"))) p <- p + opts(axis.title.x = theme_text(family="serif", face="bold", size=12, angle=00, hjust=0.54, vjust=0)) p <- p + opts(axis.title.y = theme_text(family="serif", face="bold", size=12, angle=90, vjust=0.25)) print(p)

Any help would be highly appreciated. Thanks

回答1:

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 <- p + labs(x="Weight", y=expression( bold(mpg (m^-2) ) ) ) p <-  p + opts(axis.title.y = theme_text(family="serif",                                      size=12, angle=90, vjust=0.25)) p <- p + opts(axis.title.x = theme_text(family="serif", face="bold",                                     size=12, angle=00, 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")) ) ) )


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!