[英]convert ggplot object into svg which use Open Sans Semibold font
I would like to create a ggplot
plot which use Open Sans Semibold font and then I want to convert this plot into svg
. 我想创建一个使用Open Sans Semibold字体的
ggplot
图,然后我想将此图转换为svg
。 Finally include this in rmarkdown
doc. 最后在
rmarkdown
doc中包含这个。 the biggest problem is to use Open Sans Semibold on the converted ggplot
object on the browser. 最大的问题是在浏览器上转换的
ggplot
对象上使用Open Sans Semibold。
svglite
to convert ggplot
into svg
and it works well. svglite
将ggplot
转换为svg
,效果很好。 I created rmarkdown
doc. 我创建了
rmarkdown
doc。
--- title: "" output: html_document --- ```{r setup, include = FALSE} library(svglite) knitr::opts_chunk$set( dev = "svglite", fig.ext = ".svg" ) ``` ```{r, warning = F, message = F, echo = F} library(ggplot2) data(cars) ggplot(mtcars, aes(mpg, qsec, color = factor(cyl))) + geom_point() + theme(text = element_text(family = 'Open Sans'), axis.title = element_text(family = 'Open Sans Semibold')) ```
When I open this doc in the Chrome/Opera/Mozilla browser, the Open Sans Semibold does not show up. 当我在Chrome / Opera / Mozilla浏览器中打开此文档时,Open Sans Semibold不会显示。 Instead Arial replaces it.
相反,Arial取而代之。 However in Safari it works perfectly.
然而在Safari中它完美无缺。 It turned out (more info here ) that there is a typical problem on those browsers with Open Sans Semibold and Light.
事实证明( 这里有更多信息),那些使用Open Sans Semibold和Light的浏览器存在典型问题。 In order to solve it rather than using semibold version, I use css
font-weight
with basic 'Open Sans', sans-serif
. 为了解决它而不是使用semibold版本,我使用css
font-weight
和基本的'Open Sans', sans-serif
。 And it works perfectly when I create a <p>
. 当我创建
<p>
时,它完美地工作。
---
title: ""
output: html_document
---
<style>
@import url(https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700);
.text_basic {
font-family: 'Open Sans', sans-serif;
}
.text_semibold {
font-family: 'Open Sans', sans-serif;
font-weight: 700;
}
</style>
<p class = 'text_basic'>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellat, nisi quasi cupiditate, ratione, consequuntur adipisci reiciendis impedit, laborum tenetur qui neque nobis enim. Sunt
</p>
<p class = 'text_semibold'>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellat, nisi quasi cupiditate, ratione, consequuntur adipisci reiciendis impedit, laborum tenetur qui neque nobis enim. Sunt
</p>
```{r setup, include = FALSE}
library(svglite)
knitr::opts_chunk$set(
dev = "svglite",
fig.ext = ".svg"
)
```
```{r, warning = F, message = F, echo = F}
library(ggplot2)
data(cars)
ggplot(mtcars, aes(mpg, qsec, color = factor(cyl))) +
geom_point() +
theme(text = element_text(family = 'Open Sans'),
axis.title = element_text(family = 'Open Sans Semibold'))
```
However this solution does not work on ggplot
object, because it still use Open Sans Semibold font. 但是这个解决方案对
ggplot
对象ggplot
,因为它仍然使用Open Sans Semibold字体。 In ggplot
it is not possible to use font-weight
because there is no such thing. 在
ggplot
,不可能使用font-weight
因为没有这样的东西。 Instead you can only use face
argument but that option does not accept numeric
values. 相反,您只能使用
face
参数,但该选项不接受numeric
。
One workaround which I initially believe could work is to convert ggplot
object into html code and then use css
to manipulate the fonts on axis. 我最初认为可行的一种解决方法是将
ggplot
对象转换为html代码,然后使用css
来操作轴上的字体。
---
title: ""
output: html_document
---
```{r setup, include = FALSE}
library(svglite)
knitr::opts_chunk$set(
dev = "svglite",
fig.ext = ".svg"
)
```
```{r, warning = F, message = F, echo = F}
library(ggplot2)
data(cars)
s <- svgstring()
ggplot(mtcars, aes(mpg, qsec, color = factor(cyl))) +
geom_point() +
theme(text = element_text(family = 'Open Sans'),
axis.title = element_text(family = 'Open Sans Semibold'))
htmltools::HTML(s())
invisible(
dev.off()
)
```
In that case it is possible to detect text
tags and manipulate them as I want to, but generated code is flat ie it is not easy to differatiate text
tags related to the labels and to the titles. 在这种情况下,可以检测
text
标签并按照我的意愿操作它们,但生成的代码是平的,即不容易区分与标签和标题相关的text
标签。
So, anyone has idea to solve this problem? 那么,有谁有想法解决这个问题?
Thanks anyone who read this last sentence. 谢谢任何读过最后一句的人。 I tried to summarize my problem as short as possible.
我试图尽可能简短地总结我的问题。
Both your ideas for a workaround seem straight forward approaches, and to make them work with the plot, you can use gsub
to directly modify the HTML and set the font-weight
of the style attribute of the text: 您的解决方法的想法似乎都是直接的方法,并且为了使它们适用于绘图,您可以使用
gsub
直接修改HTML并设置文本的style属性的font-weight
:
s <- svgstring()
ggplot(mtcars, aes(mpg, qsec, color = factor(cyl))) +
geom_point() +
theme(text = element_text(family = 'Open Sans'),
axis.title = element_text(family = 'Open Sans Semibold'))
gsub("font-family: Open Sans Semibold;",
"font-family: Open Sans;font-weight: 700;",
htmltools::HTML(s()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.