Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

r - Rendering of Devanagari font while using ggplot2

here is the reproducible example

library(ggplot2)
library(showtext)

## Sahitya font for Devanagari characters

font_add_google("Sahitya")
showtext_auto()


p = ggplot(NULL, aes(x = 1, y = 1)) + ylim(0.8, 1.2) +
  theme(axis.title = element_blank(), axis.ticks = element_blank(),
        axis.text = element_blank()) +
  annotate("text", 1, 0.9, family = "Sahitya", size = 15,
           label = "????? ") 

print(p)

Actual rendering should be ????? (meaning is friend)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Twitter helped to get the answer. #Rstats community is the best. here are the details

Twitter Help by RStats Community

Additionally please refer ragg Package

Solution

library(ggplot2)
library(ragg)

file <- knitr::fig_path('.png')
agg_png(file)

ggplot(NULL, aes(x = 1, y = 1)) + ylim(0.8, 1.2) +
  theme(axis.title = element_blank(), axis.ticks = element_blank(),
        axis.text = element_blank()) +
  annotate("text", 1, 0.9, family = "Sahitya", size = 15,
           label = "????? ") 

invisible(dev.off())

However I observed you have to download and add the font first. Hence removed :

## Sahitya font for Devanagari characters

font_add_google("Sahitya")
showtext_auto()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...