简体   繁体   English

R Markdown中ggraph节点中的希腊字母

[英]Greek letters in ggraph nodes in R Markdown

When I run the R script below in RStudio, the theta is shown correctly.当我在 RStudio 中运行下面的 R 脚本时,theta 显示正确。

---
title: "test"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidygraph)
library(ggraph)
```

# Greek letters
```{r ggraph}
nodes<-data.frame(label=c("θ","S"))
edges<-data.frame(from=c(2),to=c(1))
graph<-tbl_graph(nodes=nodes,edges=edges)
ggraph::ggraph(graph,layout="fr")+
    ggraph::geom_edge_link()+
    ggraph::geom_node_label(mapping = aes(label=label))     
```

But when I knit it, I get this error message (partly in Danish):但是当我编织它时,我收到了这个错误信息(部分是丹麦语):

! Package inputenc Error: Unicode character θ (U+03B8) (inputenc) not set up for use with LaTeX.包 inputenc 错误:Unicode 字符 θ (U+03B8) (inputenc) 未设置为与 LaTeX 一起使用。

Try other LaTeX engines instead (eg, xelatex) if you are using pdflatex.如果您使用 pdflatex,请尝试其他 LaTeX 引擎(例如 xelatex)。 For R Markdown users, see https://bookdown.org/yihui/rmarkdown/pdf-document.html Fejl: LaTeX failed to compile test.tex.对于 R Markdown 用户,请参阅https://bookdown.org/yihui/rmarkdown/pdf-document.html Fejl:LaTeX 无法编译 test.tex。 See https://yihui.org/tinytex/r/#debugging for debugging tips.有关调试提示,请参阅https://yihui.org/tinytex/r/#debugging See test.log for more info.有关更多信息,请参阅 test.log。 In addition: Advarselsbeskeder: 1: I grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 2: I grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 3: I grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 4: I grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 5: I grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :另外: Advarselsbeskeder: 1: I grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 2: I grid. Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 3: I grid.Call(C_textBounds, as.graphicsAnnot(x$) label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 4: I grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y , : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 5: I grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 6: I grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 6: I grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 7: I grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 7: I grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 8: I grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for 8: I grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
konverteringsfejl på 'θ' i 'mbcsToSbcs': punktum erstattet for Kørsel stoppet konverteringsfejl på 'θ' i 'mbcsToSbcs':Kørsel stoppet 的 punktum erstattet

I have tried using expression(theta) instead of the character itself, but that doesn't work.我曾尝试使用 expression(theta) 而不是字符本身,但这不起作用。 Any ideas?有任何想法吗?

There might be a better way to solve this, but one way is to overcome your issue is to the following:可能有更好的方法来解决这个问题,但解决您的问题的一种方法是:

  1. Use xelatex and the fontenc package;使用xelatexfontenc包;
  2. force dev to "jpeg" , "png" or " cairo_pdf " to generate figures ( θ can cause issue with other devices).强制dev"jpeg""png"或 " cairo_pdf " 以生成数字( θ可能会导致其他设备出现问题)。

Below in the RMarkdown document I've used在我使用过的 RMarkdown 文档中

    ---
    title: "test"
    header_includes:
        - \usepackage[T1]{fontenc}
    output:
        pdf_document:
          latex_engine: xelatex
    ---


    ```{r setup, include=FALSE}
     knitr::opts_chunk$set(echo = TRUE, dev = "png", dpi = 300)
     library(tidygraph)
     library(ggraph)
     ```

    # Greek letters

    ```{r ggraph}
    library(ggraph)
    library(ggplot2)
    nodes<-data.frame(label=c("θ","S"))
    edges<-data.frame(from=c(2),to=c(1))
    graph<-tbl_graph(nodes=nodes,edges=edges)
    ggraph::ggraph(graph,layout="fr")+
        ggraph::geom_edge_link()+
        ggraph::geom_node_label(mapping = aes(label=label))
    ```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM