首页 > 解决方案 > ggplot2 图例标题不尊重 Google 字体的粗体

问题描述

不知道为什么,但是当我使用 showtext::font_add_google 导入“Montserrat”字体时,图例标题不会是粗体:/

require(showtext);require(ggplot2)

font_add_google(name="Montserrat")
ggplot(mpg,aes(cty,hwy,fill=manufacturer))+geom_point()+
theme(text=element_text(family="Montserrat"),legend.title=element_text(face="bold"))

在此处输入图像描述

我认为这(部分)有效(尽管没有加粗),因为我在本地安装了蒙特塞拉特。如果我尝试使用尚未安装的字体,即:font_add_google(name="Yesteryear")

我得到这个错误并且没有情节:

Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
  polygon edge not found
In addition: Warning message:
In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
  no font could be found for family "Yesteryear"

标签: rggplot2fontsshowtext

解决方案


正如 Stefan 所指出的,showtext_auto()在绘制作品之前添加。它不能在 R Studio 中正确渲染(在 Mac OS Catalina 下),但它会输出粗体图例ggsave()在此处输入图像描述


推荐阅读