最近作图ggplot 中文显示成了下面这样:
此外,对ggplot 中的字体进行一些设置,但这个family 特有限的,比如Times, Arial 这些比较标准的字体:
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"))
比如:https://fonts.google.com/[3] 的全部字体。
打卡了一个新世界的钥匙:
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))
以及这张:
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 字体前,需要提前声明:
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"))
)
如果想要统一字体,可以参数设置:
theme(text = element_text(family = "gochi"))
我们可以直接下载:
> 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()
如果无法直接下载,可以先查看自己的字体的目录:
> 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]
接下来将这些字体文件添加进上面的字体目录,就可以调用了。
# 载入 showtext
library(showtext);
# 第一个参数设置字体名称,第二个参数为字体库路径,同目录下,我们写字体库名就可以了
font_add("SyHei", "SourceHanSansSC-Bold.otf")
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