我无法在geom_text中设置我的字体.这是我尝试过的:
labels_test<-data.frame(a=c("a","b","c"),b=c(1:3),c=c(3:1))
# works
ggplot () + geom_text(data=labels_test,aes(b,c,label=a),color="blue")
# does not work:
ggplot () + geom_text(data=labels_test,aes(b,c,label=a),color="blue",family="Times")
# error message: In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,:
# Font family not found in Windows font database
Run Code Online (Sandbox Code Playgroud)
我已经进口的所有字体指示在这里.什么问题还有什么问题?
use*_*1_G 29
我会尝试"
windowsFonts(Times=windowsFont("TT Times New Roman"))
Run Code Online (Sandbox Code Playgroud)
在这样做时,您明确指定Windows字体映射.
nat*_*ate 28
其他答案没有解决我的问题(Windows 10).
我的系统的关键是extrafont::loadfonts(device="win") 之前 打电话library(ggplot2).
extrafont::loadfonts(device="win")
#extrafont::fonttable()
#extrafont::font_import("C:/Windows/Fonts/", pattern = "RobotoCondensed")
library(ggplot2)
Run Code Online (Sandbox Code Playgroud)
字体位置的常见问题:
我以前用随机文件夹安装了字体extrafont::font_import().因此extrafont::fonttable()引用了我的C:\Windows\Fonts\文件夹中的文件.为了解决这个问题重置我extrafonts::fonttable()有install.packages("extrafontdb"),才能在不同的位置以清除字体的引用.
关于保存的编辑:
深入兔子洞.储蓄是一项额外的挑战.为了extrafont::loadfonts(device="pdf")我必须确保我的字体中extrafont::fonttable()没有相同的姓氏和粗体/斜体状态.我编辑extrafont:::fonttable_file()解决了我家中任何重复的粗体/斜体字体.使用Roboto Condensed我将light fonts的字体系列重命名为"Roboto Condensed Light".
保存ggsave(device="pdf")然后工作.在acrobat中打开文件时,字体无法正确显示.我尝试使用ghostscript嵌入字体以及使用cairo_pdf设备.最简单,最实用的解决方案是在Illustrator中打开.pdf文件(字体显示在那里很好)并立即将它们重新保存为.pdf.
编辑2关于保存:
保存为.eps是在illustrator和acrobat中保存文件的唯一方法.结果很完美.ggsave(g, file="Figure.eps", fonts=c("FONT FAMILIES USED", "Roboto Condensed", "Roboto Condensed Light"))
最终的绘图代码:
这是我在绘图之前使用的最后一组调用.注释是仅需要运行一次的设置命令.
# Plotting
extrafont::loadfonts(device="pdf")
extrafont::loadfonts(device="postscript")
# extrafont::font_import("C:/Windows/Fonts/", pattern = "RobotoCondensed", prompt = F)
# extrafont::fonttable()
# C:/Program Files/R/R-3.3.1/library/extrafontdb/fontmap/ - Change lights to "Roboto Condensed Light"
# After ggsave(device="pdf") or ggsave(device="eps") open and resave the file in Illustrator
library(hrbrthemes)
library(ggplot2)
Run Code Online (Sandbox Code Playgroud)
小智 17
您必须使用以下命令导入系统字体:
font_import(paths = NULL, recursive = TRUE, prompt = TRUE,pattern = NULL)
Run Code Online (Sandbox Code Playgroud)