前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >83-R可视化16-用showtext让R绘图认得中文及花里胡哨的字体

83-R可视化16-用showtext让R绘图认得中文及花里胡哨的字体

作者头像
北野茶缸子
发布2022-02-08 15:17:58
9570
发布2022-02-08 15:17:58
举报
  • Date : [[2021-12-19_Sun]]
  • Tags :  #R/index/02 #R/R可视化 #R/R数据科学 #R/R包
  • 参考:
    • R 绘图 – 中文支持 | 菜鸟教程 (runoob.com)[1]
    • yixuan/showtext: Using Fonts More Easily in R Graphs (github.com)[2]

起因

最近作图ggplot 中文显示成了下面这样:

此外,对ggplot 中的字体进行一些设置,但这个family 特有限的,比如Times, Arial 这些比较标准的字体:

代码语言:javascript
复制
ggplot(chic, aes(x = date, y = temp, color = season)) +
  geom_point() +
  labs(x = "Year", y = "Temperature (°F)") +
  theme(legend.title = element_text(family = "Playfair",
                                    color = "chocolate",
                                    size = 14, face = "bold"))

使用showtext 包

比如:https://fonts.google.com/[3] 的全部字体。

打卡了一个新世界的钥匙:

代码语言:javascript
复制
library(showtext)
font_add_google("Playfair Display", ## name of Google font
                "Playfair")  ## name that will be used in R
font_add_google("Bangers", "Bangers")

ggplot(chic, aes(x = date, y = temp)) +
  geom_point(color = "firebrick") +
  labs(x = "Year", y = "Temperature (°F)",
       title = "Temperatures in Chicago",
       subtitle = "Daily temperatures in °F from 1997 to 2001") +
  theme(plot.title = element_text(family = "Bangers", hjust = .5, size = 25),
        plot.subtitle = element_text(family = "Playfair", hjust = .5, size = 15))

以及这张:

代码语言:javascript
复制
pacman::p_load(ggplot2, showtext)

## Loading Google fonts (https://fonts.google.com/)
font_add_google("Gochi Hand", "gochi")
font_add_google("Schoolbell", "bell")
font_add_google("Covered By Your Grace", "grace")
font_add_google("Rock Salt", "rock")

## Automatically use showtext to render text for future devices
showtext_auto()

# load csv
chic = readr::read_csv("Desktop/chicago-nmmaps.csv")

ggplot(chic, aes(x = date, y = temp)) +
  geom_point(color = "firebrick") +
  labs(x = "Year", y = "Temperature (°F)",
       title = "Temperatures in Chicago",
       subtitle = "Seasonal pattern of daily temperatures from 1997 to 2001",
       caption = "Data: NMMAPS",
       tag = "Fig. 1") + theme_classic() +
  theme(axis.title = element_text(family = "gochi"),
          plot.title = element_text(family = "bell", hjust = .5, size = 25), 
        plot.subtitle = element_text(family = "bell", hjust = .5, size = 15),
        axis.text = element_text(family = "gochi"),
        plot.tag = element_text(family = "rock"),
        plot.caption = element_text(family = "rock"))

需要注意的是,我们在使用showtext 字体前,需要提前声明:

代码语言:javascript
复制
font_add("source-han-serif-cn")
showtext_auto()
(p <- ggplot(income.2019) + 
    geom_col(aes(x = names, y = value,
                 fill = type), position = position_dodge(width = 0.5)) + 
    theme(text = element_text(family = "wqy-microhei"))
  )

如果想要统一字体,可以参数设置:

代码语言:javascript
复制
theme(text = element_text(family = "gochi"))

使用汉字

我们可以直接下载:

代码语言:javascript
复制
> font_install(source_han_serif())
downloading the 'regular' font face...
 [100%] Downloaded 178 bytes...
 [0%] Downloaded 38518 bytes...

> font_families()
[1] "sans"                "serif"               "mono"               
[4] "wqy-microhei"        "source-han-serif-cn"

font_add("source-han-serif-cn")
showtext_auto()

如果无法直接下载,可以先查看自己的字体的目录:

代码语言:javascript
复制
> font_paths()
[1] "/Library/Fonts"                     "/System/Library/Fonts"             
[3] "/System/Library/Fonts/Supplemental" "/Users/appe/Library/Fonts" 

接着下载字体文件,这里使用思源黑体,思源黑体是 Adobe 与 Google 推出的一款开源字体。

下载:source-han-sans/OTF/SimplifiedChinese at release · adobe-fonts/source-han-sans (github.com)[4]

接下来将这些字体文件添加进上面的字体目录,就可以调用了。

代码语言:javascript
复制
# 载入 showtext  
library(showtext);  
# 第一个参数设置字体名称,第二个参数为字体库路径,同目录下,我们写字体库名就可以了  
font_add("SyHei", "SourceHanSansSC-Bold.otf")
代码语言:javascript
复制
font_add("source-han-serif-cn")
showtext_auto()
(p <- ggplot(income.2019) + 
    geom_col(aes(x = names, y = value,
                 fill = type), position = position_dodge(width = 0.5)) + 
    theme(text = element_text(family = "wqy-microhei"))
  )

参考资料

[1]R 绘图 – 中文支持 | 菜鸟教程 (runoob.com): https://www.runoob.com/r/r-charts-cn.html

[2]yixuan/showtext: Using Fonts More Easily in R Graphs (github.com): https://github.com/yixuan/showtext

[3]https://fonts.google.com/: https://fonts.google.com/

[4]source-han-sans/OTF/SimplifiedChinese at release · adobe-fonts/source-han-sans (github.com): https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-01-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 北野茶缸子 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 起因
  • 使用showtext 包
  • 使用汉字
    • 参考资料
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档