【问题标题】:convert ggplot object into svg which use Open Sans Semibold font将 ggplot 对象转换为使用 Open Sans Semibold 字体的 svg
【发布时间】:2016-09-29 18:31:19
【问题描述】:

我想创建一个使用 Open Sans Semibold 字体的 ggplot 绘图,然后我想将此绘图转换为 svg。最后将其包含在rmarkdown doc 中。最大的问题是在浏览器上转换的ggplot对象上使用Open Sans Semibold。

  1. 我使用svgliteggplot 转换为svg,效果很好。
  2. 我在轴的标题中加入了 Open Sans Semibold 字体(我在本地存储了所有 Open Sans 系列)。
  3. 我创建了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'))
    
    ```
    

当我在 Chrome/Opera/Mozilla 浏览器中打开此文档时,没有显示 Open Sans Semibold。相反,Arial 取代了它。但是在 Safari 中它可以完美运行。 事实证明(更多信息here)在那些使用 Open Sans Semibold 和 Light 的浏览器上存在一个典型问题。为了解决它而不是使用半粗体版本,我使用 css font-weight 和基本 'Open Sans', sans-serif。当我创建 <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'))

```

但是,此解决方案不适用于 ggplot 对象,因为它仍然使用 Open Sans Semibold 字体。在ggplot 中不能使用font-weight,因为没有这样的东西。相反,您只能使用 face 参数,但该选项不接受 numeric 值。

我最初认为可行的一种解决方法是将 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()
)

```

在这种情况下,可以检测text 标签并按照我的意愿对其进行操作,但生成的代码是扁平的,即不容易区分与标签和标题相关的text 标签。

那么,有人有办法解决这个问题吗?

感谢所有阅读最后一句话的人。我试图尽可能简短地总结我的问题。

【问题讨论】:

    标签: r svg ggplot2 r-markdown


    【解决方案1】:

    您对解决方法的两个想法似乎都是直截了当的方法,为了使它们与情节一起使用,您可以使用gsub 直接修改 HTML 并设置文本样式属性的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()))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-08
      • 2014-05-19
      • 2017-07-18
      • 1970-01-01
      • 2017-12-20
      • 2014-02-13
      • 2018-01-02
      • 2015-12-07
      相关资源
      最近更新 更多