Search code examples
rggplot2geom-text

In R geomtextpath, does it's not support chinese


Does ‘geom_textpath’ not support Chinese ? As below sample, when using geom_textpath add label to the plot, it's only show englise letters. Anyone can help? Thanks!

library(tidyverse)
library(geomtextpath)
plot_data_2 <- data.frame(category=c('品类A','品类B','品类C','品类D'),
                          amount=c(1,6,4,7))

plot_data_2 %>% ggplot(aes(x=1,y=amount,fill=category))+
  geom_col()+geom_textpath(position = position_stack(vjust=0.5),
                           aes(label=category))+
  coord_polar()

enter image description here


Solution

  • Disclaimer: I'm the other author of {geomtextpath}.

    This is a font fallback issue. Smart text renderers try to lookup a particular glyph from a font file, and if they don't find it, they substitute the font with another that does have the glyph. Because {geomtextpath} has to do a 'convert font index back to glyph' step, fallback doesn't work.

    In this case, the best course of action would be to explicitly use a font that supports Chinese characters. The freely available 'Noto Sans TC' or 'Noto Sans Simplified Chinese' fonts should work, though I don't know whether traditional or simplified is more appropriate here.

    library(tidyverse)
    library(geomtextpath)
    
    # Didn't reprex well because glyphs below get substituted by `??`.
    plot_data_2 <- data.frame(category=c('品类A','品类B','品类C','品类D'),
                              amount=c(1,6,4,7))
    
    plot_data_2 %>% ggplot(aes(x=1,y=amount,fill=category))+
      geom_col()+geom_textpath(position = position_stack(vjust=0.5),
                               aes(label=category),
                               family = "Noto Sans TC")+
      coord_polar() +
      theme(legend.text = element_text(family = "Noto Sans TC"))
    

    enter image description here