曾几何时,我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")
Run Code Online (Sandbox Code Playgroud)
R正在返回一个警告font family not found in Windows font database
,但有一个我正在关注的教程(如果我能再次找到它,我将在这里更新链接)说这是正常的而不是问题.此外,这在某种程度上起作用,因为我的图表曾经使用过一些arial或helvitica类型的字体.我认为即使在最初的迁移期间,这也始终是一个警告.
UPDATE
当我运行windowsFonts()
我的输出是
$ serif [1]"TT Times …
我正在 ggplot2 中制作图表,但ggsave()
没有达到我的预期。
require(ggplot2)
require(showtext)
showtext_auto()
hedFont <- "Pragati Narrow"
font_add_google(
name = hedFont,
family = hedFont,
regular.wt = 400,
bold.wt = 700
)
chart <- ggplot(
data = cars,
aes(
x = speed,
y = dist
)
) +
geom_point() +
labs(
title = "Here is a title",
subtitle = "Subtitle here"
) +
theme(
plot.title = element_text(
size = 20,
family = hedFont,
face = "bold"
),
axis.title = element_text(
face = "bold"
)
)
ggsave( …
Run Code Online (Sandbox Code Playgroud) 我的 Windows 桌面 ( C:\Users\me\Desktop\Fonts
)上有一个文件夹,其中包含我想使用extrafont
.
当我尝试使用导入字体时
library(extrafont)
font_import(paths = "C:/Users/me/Desktop/Fonts", prompt=FALSE)
Run Code Online (Sandbox Code Playgroud)
我收到错误消息
Scanning ttf files in C:/Users/me/Desktop/Fonts ...
Extracting .afm files from .ttf files...
C:\Users\me\Desktop\Fonts\arista-light.ttf : No FontName. Skipping.
C:\Users\me\Desktop\Fonts\facebook-letter-faces.ttf : No FontName. Skipping.
C:\Users\me\Desktop\Fonts\Guardian-EgypTT-Text-Regular.ttf : No FontName. Skipping.
C:\Users\me\Desktop\Fonts\pico-black.ttf : No FontName. Skipping.
C:\Users\me\Desktop\Fonts\product-sans.ttf : No FontName. Skipping.
Found FontName for 0 fonts.
Scanning afm files in C:/Users/me/Documents/R/R-3.6.3/library/extrafontdb/metrics
Warning messages:
1: In system2(ttf2pt1, c(args, shQuote(ttfiles[i]), shQuote(tmpfiles[i])), :
running command '"C:/Users/me/Documents/R/R-3.6.3/library/Rttf2pt1/exec/ttf2pt1.exe" -a -G fAe "C:\Users\me\Desktop\Fonts\arista-light.ttf" "C:\Users\me\AppData\Local\Temp\RtmpOgbdTh/fonts/arista-light"' had …
Run Code Online (Sandbox Code Playgroud) 我想在图表中使用免费字体Latoggplot2
,因为我的 R markdown 文档的其余部分就是用这种字体设置的。
该字体已安装在我的系统上,并且可以在字体簿中使用(仅一次)。
所有可用的字体都随extrafont
包一起加载并在extrafontdb
.
当我将 Markdown 文档编织为 PDF 时,所有文本都以Lato
. 但是,我的 ggPlots 的绘图标签未显示。
我还收到以下警告消息:
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font width unknown for character 0x20
嵌入文档中包含的字体后,extrafont::embed_fonts
所有使用Lato
字体的图形都会显示绘图标签,但是
下面提供了一个 MWE,包括带有和不带有 Lato 字体的 ggPlot 图形(Lato 可以在此处免费获得)要随后嵌入字体,需要运行embed_fonts("TestRmd.pdf", outfile="TestRmd_embedded.pdf")
任何帮助是极大的赞赏!
---
title: "Embedding Fonts in PDF"
output: pdf_document
urlcolor: blue
---
```{r echo=FALSE}
library(ggplot2)
```
### Plot with standard font {#standard} …
Run Code Online (Sandbox Code Playgroud) 我编写了一个在同事之间共享的绘图函数,我的组织更喜欢使用 Calibri,而不是 ggplot2 默认的 Arial 文本。如果我是唯一使用此函数的人,我会首先在脚本顶部执行此操作:
library(extrafont)
font_import()
loadfonts(device="win")
Run Code Online (Sandbox Code Playgroud)
然后制作我的 ggplot2 图表。我似乎只需要font_import()
在给定的机器上执行一次,但是loadfonts(device="win")
每次开始新会话时我都需要执行一次。我不太熟悉这些功能,但如果我不执行此loadfonts
步骤,我的图表就不会使用 Calibri。
我希望我编写的绘图函数能够为其他人使用,并且我想检查他们是否已经完成了这些步骤,如果还没有,给他们一个有用的消息。我认为我可以使用fonts()
然后检查 Calibri 是否在输出中列出,但我认为这只检查我font_import()
在使用这台机器的历史中曾经加载过的字体。我也想也许
systemfonts::match_font("Calibri")
Run Code Online (Sandbox Code Playgroud)
会检查,但无论我是否已经运行loadfonts(...
,我都会得到相同的结果,所以也不是这样。
如何检查字体是否可以在图表中使用?
我的情节是可行的,除了我无法更改字体系列。即使我可以更改其他内容(例如颜色,大小和对齐方式),它也始终保持默认值。
这是我的代码:
ggplot(data = SeattleJuly17Data,
aes(x = Price, y = SatisfactionScore, col = RoomType)) +
geom_point() +
xlim(0,500) +
geom_smooth() +
ggtitle("Satisfaction Trends by Price and Room Type") +
theme(plot.title = element_text(family = "Calibri",
size=15,
color="Red",
hjust = 0.5)) +
xlab("Price per Night") +
ylab("Guest Satisfaction Score")
Run Code Online (Sandbox Code Playgroud) 我可以自定义字体ggplot
:
library(extrafont)\n\nwindowsFonts()\nfont_import(pattern = "comic", prompt = FALSE)\nloadfonts(device = "win")\nwindowsFonts()\n\nggplot(mapping=aes(x=seq(1,10,.1), y=seq(1,10,.1))) + \n geom_line(position="jitter", color="red", size=2) + theme_bw() +\n theme(text=element_text(size=16, family="Comic Sans MS"))\n
Run Code Online (Sandbox Code Playgroud)\n\n这呈现为:
\n\n\n\n\n\n我还可以将该图和 extrafont 集成到一个闪亮的应用程序中,该应用程序在本地运行,如下所示:
\n\nlibrary(ggplot2)\nlibrary(extrafont)\nlibrary(shiny)\n\nfont_import(paths = "www", pattern = "comic", prompt = FALSE)\nloadfonts()\nprint(fonts())\n\nui <- fluidPage(plotOutput("plot"),textOutput("fonts"))\n\nserver <- function(input, output) {\n output$plot <- renderPlot({\n ggplot(mapping=aes(x=seq(1,10,.1), y=seq(1,10,.1))) + \n geom_line(position="jitter", color="red", size=2) + theme_bw() +\n theme(text=element_text(size=16, family="Comic Sans MS"))\n })\n output$fonts <- renderText(print(fonts()))\n}\n\nshinyApp(ui = ui, server = server)\n …
Run Code Online (Sandbox Code Playgroud) 根据这篇文章中的建议,我试图将衬线字体(或字体“系列”)安装到 R 中,以便我可以将 ggplots 保存为 .eps 文件。尽管提供的建议有效,但我想尝试解决该问题以供将来使用。
这是生成问题的代码。
library(bayesplot)
df <- data.frame(xVar = rnorm(1e4,0,1), yVar = rnorm(1e4,2,1), zVar = rnorm(1e4,4,1))
t <- bayesplot::mcmc_trace(df)
t
Run Code Online (Sandbox Code Playgroud)
现在当我去保存这个数字时......
ggplot2::ggsave(filename = "tPlot.eps",
plot = t,
device = "eps",
dpi = 1200,
width = 15,
height = 10,
units = "cm")
Run Code Online (Sandbox Code Playgroud)
...它抛出错误
Error in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)) :
family 'serif' not included in postscript() device
Run Code Online (Sandbox Code Playgroud)
在上一篇文章中,回答者建议我下载该extrafont
软件包。
我跑了
View(fonttable())
Run Code Online (Sandbox Code Playgroud)
但衬线字体似乎没有安装。
然后我尝试了
font_addpackage(pkg = "serif")
Run Code Online (Sandbox Code Playgroud)
但我得到了错误
Error in font_addpackage(pkg = "serif") :
Unknown font …
Run Code Online (Sandbox Code Playgroud) 我已经加载了我的字体
library(extrafont)
font_import()
Run Code Online (Sandbox Code Playgroud)
当我查看路径时
fonttable()
Run Code Online (Sandbox Code Playgroud)
有一些字体在该路径下不再存在。这给我以后带来了一些问题。如何从字体数据库中删除/取消注册它们?
只是重新运行 font_import 是行不通的。
我希望在我的 R 图中使用 LM Roman 10 中的标题和轴图例,以便更好地将它们集成到我的 LateX 文本中。
我已经尝试过这个解决方案,但在 R 4.1.2 中不起作用。
代码如下(引用上一个问题的答案)
# Run once
install.packages("extrafont")
library(extrafont)
# Install **TTF** Latin Modern Roman fonts from www.fontsquirrel.com/fonts/latin-modern-roman
# Import the newly installed LModern fonts, change the pattern according to the
# filename of the lmodern ttf files in your fonts folder
font_import(pattern = "lmroman*")
# Run each time
library(extrafont)
loadfonts(device = "win") # this line only in Windows
par(family = "LM Roman 10")
hist(c(1,2,3,4,5), xlab = "Some …
Run Code Online (Sandbox Code Playgroud)