简体   繁体   中英

transgendered / hermaphrodite symbol in base R plot (and other rare unicode glyphs - macOs font problem)

I want to use male/female/transgendered/hermaphrodite symbols in base R plot.

This works for me with male and female symbols (male example below).

a = '\u2642'   # unicode for male (female = '\u2640')
plot(1, 1, pch = a)

男性

With hermaphrodite/transgendered, I get a square.

a <- '\u26a5'  # unicode for hermaphrodite/transgendered 
plot(1, 1, pch = a)

雌雄同体失败

> a
[1] "⚥"

According to what I had read elsewhere, this seemed like it could be a macOs-specific problem. Many other unicodes also failed and gave me the same square.

This is an encoding issue that seems to happen on MacOS or Linux. The solution I find for plotting unicode characters is to use the cairo package.

cairo_pdf("~/Desktop/test2.pdf")
a <- '\u26a5'  # unicode for hermaphrodite/transgendered 
plot(1, 1, pch = a)
dev.off()

When I ran your code I actually received an error:

Warning messages:
1: In plot.xy(xy, type, ...) :
  conversion failure on '⚥' in 'mbcsToSbcs': dot substituted for <e2>

It's interesting you didn't report the same error.

The same issue is observed with ggplot, as it's device/os dependent.

This was resolved by installing fonts that support transgendered/hermaphrodite unicode. A list of fonts reportedly supporting U+26A5 could be found in the link below.

https://www.fileformat.info/info/unicode/char/26a5/fontsupport.htm

I tried using DejaVu Sans (worked), Symbola (worked), and Code2000 (the version I installed did not work - seemingly without a working hermaphrodite symbol). These were downloaded from various sites on the inte.net and installed with Font Book. Here's a code with a usable font.

a <- '\u26a5'  # unicode for hermaphrodite/transgendered 
plot(1, 1, pch = a, family = "DejaVu Sans")

在此处输入图像描述

Here, pch could be replaced with likes of main, xlab, ylab, text, etc. There is no need for Cairo - a code for base R shown below works fine.

jpeg("~/Desktop/test2a.jpeg")
a <- '\u26a5' 
plot(1, 1, main = a, family = "Symbola")
dev.off()

Hermaphrodite unicode is supported by few fonts. For most things, Arial Unicode MS works fine. List of characters and glyphs with Arial Unicode MS (38,917 characters and 38,917 glyphs).

http://zuga.net/articles/unicode-all-characters-supported-by-the-font-arial-unicode-ms/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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