Eng*_*IRD 56 fonts r ggplot2 showtext extrafont
曾几何时,我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,但有一个我正在关注的教程(如果我能再次找到它,我将在这里更新链接)说这是正常的而不是问题.此外,这在某种程度上起作用,因为我的图表曾经使用过一些arial或helvitica类型的字体.我认为即使在最初的迁移期间,这也始终是一个警告.
UPDATE
当我运行windowsFonts()我的输出是
$ serif [1]"TT Times New Roman"
$ sans [1]"TT Arial"
$ mono [1]"TT Courier New"
但是,这是在我跑完之后font_import()所以我只能断定我的字体没有保存在正确的位置.运行font_import()请求的代码实际上加载了库:
LocalLibraryLocation <- paste0("C:\\Users\\",Sys.getenv("USERNAME"),"\\Documents","\\R\\win-library\\3.2");
    .libPaths(c(LocalLibraryLocation, .libPaths()))
Mik*_*ise 86
你想错过了一个初始化步骤.
您可以使用该命令查看可用的字体windowsFonts().例如,当我开始研究这个时,我看起来像这样:
> windowsFonts()
$serif
[1] "TT Times New Roman"
$sans
[1] "TT Arial"
$mono
[1] "TT Courier New"
在运行包extraFont并运行之后font_import(花了5分钟):
library(extrafont)
font_import()
loadfonts(device = "win")
我有更多的可用 - 可论证的太多,当然在这里列出的太多了.
然后我尝试了你的代码:
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"))
print(a)
屈服于此:
您可以使用以下代码段找到family参数所需的字体名称element_text:
> names(wf[wf=="TT Times New Roman"])
[1] "serif"
然后:
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="serif"))
print(a)
Tun*_*ung 20
另一种选择是使用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
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) 
D A*_*lls 15
更改绘图中的所有字体您选择的字体在plot + theme(text=element_text(family="mono"))哪里mono。
默认字体选项列表:
R 没有很好的字体覆盖范围,正如Mike Wise指出的那样,R 对常见字体使用不同的名称。
此页面详细介绍了默认字体。
晚会晚了,但对于希望在 Shinyapps.io 上ggplots的shiny应用程序内部添加自定义字体的人来说,这可能会很有趣。
你可以:
这导致app.R文件内的以下上部:
dir.create('~/.fonts')
file.copy("www/IndieFlower.ttf", "~/.fonts")
system('fc-cache -f ~/.fonts')
可以在此处找到完整的示例应用程序。