很久以前,我用ggplot2
改变了我的windowsFonts(Times=windowsFont("TT Times New Roman"))
字体。现在,我无法摆脱这一切。
在尝试将family=""
设置为ggplot2
theme()
时,我似乎无法生成字体的更改,因为我用不同的字体系列编译了下面的MWE。
library(ggplot2)
library(extrafont)
loadfonts(device = "win")
a <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
ggtitle("Fuel Efficiency of 32 Cars") +
xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
theme(text=element_text(size=16,
# family="Comic Sans MS"))
# family="CM Roman"))
# family="TT Times New Roman"))
# family="Sans"))
family="Serif"))
print(a)
print("Graph should have refreshed")
R正在返回一个警告font family not found in Windows font database
,但是有一个教程告诉我(如果我能再次找到它,我会在这里更新链接),它说这是正常的,没有问题。而且,这在某种程度上起了作用,因为我的图表曾经使用过一些字母或helvitica字体。我认为,即使在移民初期,这也一直是一个警告。
更新
当我运行windowsFonts()
时,输出是
$serif 1 "TT时代新罗马“ $sans 1 "TT Arial“ $mono 1 "TT速递新“
但是,这是在我运行font_import()
之后,所以我只能得出结论,我的字体没有保存在正确的位置。运行font_import()
请求的代码实际上用以下内容加载库:
LocalLibraryLocation <- paste0("C:\\Users\\",Sys.getenv("USERNAME"),"\\Documents","\\R\\win-library\\3.2");
.libPaths(c(LocalLibraryLocation, .libPaths()))
发布于 2018-08-18 06:41:42
另一种选择是使用支持更多类型字体的showtext
包(TrueType、OpenType、Type 1、web字体等)。和更多的图形设备,并避免使用外部软件,如Ghostscript。
# install.packages('showtext', dependencies = TRUE)
library(showtext)
导入一些Google字体
# https://fonts.google.com/featured/Superfamilies
font_add_google("Montserrat", "Montserrat")
font_add_google("Roboto", "Roboto")
将字体从当前搜索路径加载到showtext
# Check the current search path for fonts
font_paths()
#> [1] "C:\\Windows\\Fonts"
# List available font files in the search path
font_files()
#> [1] "AcadEref.ttf"
#> [2] "AGENCYB.TTF"
#> [428] "pala.ttf"
#> [429] "palab.ttf"
#> [430] "palabi.ttf"
#> [431] "palai.ttf"
# syntax: font_add(family = "<family_name>", regular = "/path/to/font/file")
font_add("Palatino", "pala.ttf")
font_families()
#> [1] "sans" "serif" "mono" "wqy-microhei"
#> [5] "Montserrat" "Roboto" "Palatino"
## automatically use showtext for new devices
showtext_auto()
绘图:需要打开Windows图形设备,因为showtext
与RStudio内置图形设备不能很好地工作。
# https://github.com/yixuan/showtext/issues/7
# https://journal.r-project.org/archive/2015-1/qiu.pdf
# `x11()` on Linux, or `quartz()` on Mac OS
windows()
myFont1 <- "Montserrat"
myFont2 <- "Roboto"
myFont3 <- "Palatino"
library(ggplot2)
a <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
ggtitle("Fuel Efficiency of 32 Cars") +
xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
theme(text = element_text(size = 16, family = myFont1)) +
annotate("text", 4, 30, label = 'Palatino Linotype',
family = myFont3, size = 10) +
annotate("text", 1, 11, label = 'Roboto', hjust = 0,
family = myFont2, size = 10)
## On-screen device
print(a)
## Save to PNG
ggsave("plot_showtext.png", plot = a,
type = 'cairo',
width = 6, height = 6, dpi = 150)
## Save to PDF
ggsave("plot_showtext.pdf", plot = a,
device = cairo_pdf,
width = 6, height = 6, dpi = 150)
## turn showtext off if no longer needed
showtext_auto(FALSE)
编辑:在RStudio中使用showtext
的另一个解决方法。在R会话开始时运行以下代码(来源)
trace(grDevices::png, exit = quote({
showtext::showtext_begin()
}), print = FALSE)
https://stackoverflow.com/questions/34522732
复制相似问题