kel
kel

Reputation: 1

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)

Upvotes: 0

Views: 76

Answers (1)

kel
kel

Reputation: 1

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()

Upvotes: 0

Related Questions