代码之家  ›  专栏  ›  技术社区  ›  jay.sf

如何在pdf中使用UTF-8编码的字符矢量?

  •  1
  • jay.sf  · 技术社区  · 1 年前

    尝试使用将整数转换为UTF-8编码的字符向量 intToUtf8 设置 pch=

    (.pch <- c(intToUtf8(9675), intToUtf8(9679)))
    # [1] "○" "●"
    

    虽然它对 png ,

    png('foo.png', 400, 400)
    
    with(df, plot(x, y, pch=.pch[(z < 0) + 1], xlim=0:1, ylim=0:1))
    legend('topleft', pch=.pch, legend=letters[1:2])
    
    dev.off()
    

    enter image description here

    不会的 pdf ,

    pdf.options(encoding='ISOLatin2.enc')
    pdf('foo.pdf', 4, 4)
    
    with(df, plot(x, y, pch=.pch[(z < 0) + 1], xlim=0:1, ylim=0:1))
    legend('topleft', pch=.pch, legend=letters[1:2])
    
    dev.off()
    

    enter image description here

    但我想要 pdf

    也尝试过 'ISOLatin1.enc' 但无济于事。

    > sessionInfo()
    R version 4.3.1 (2023-06-16)
    Platform: x86_64-pc-linux-gnu (64-bit)
    Running under: Ubuntu 22.04.3 LTS
    
    Matrix products: default
    BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
    LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
    
    locale:
     [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
     [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
     [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
     [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
     [9] LC_ADDRESS=C               LC_TELEPHONE=C            
    [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
    
    time zone: Europe/Zurich
    tzcode source: system (glibc)
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    loaded via a namespace (and not attached):
    [1] compiler_4.3.1 tools_4.3.1   
    

    数据:

    df <- structure(list(x = c(0.2, 0.6, 0.7, 0.9), y = c(0.4, 0.7, 0.9, 
    0.5), z = c(-1, -1, 1, 1)), class = "data.frame", row.names = c(NA, 
    -4L))
    
    1 回复  |  直到 1 年前
        1
  •  1
  •   SamR    1 年前

    我在中找到了一个使用UTF-8字符的解决方法 pdf 使用R中的绘图 grDevices::cario_pdf() docs 状态 可以(在合适的平台上)包括更广泛的UTF-8字形,并嵌入所使用的字体)

    grDevices::cairo_pdf('foo.pdf', 4, 4)
    
    with(df, plot(x, y, pch=.pch[(z < 0) + 1], xlim=0:1, ylim=0:1))
    legend('topleft', pch=.pch, legend=letters[1:2])
    
    dev.off()
    

    enter image description here

    注释重新矢量与位图输出

    这个 文档 同时声明:

    注意,与postscript和pdf不同, cairo_pdf cairo_ps 有时记录位图而不是矢量图形。

    我不太清楚“有时”是什么意思。在这种情况下,我不会出现这种情况:结果是矢量图。但是,您可能希望检查您的环境中是否也存在这种情况。