showtext:在R绘图过程中更轻松地使用字体
2020年8月14日
2020年10月24日更新
在许多情况下,R绘图中使用非标准字体并非易事,尤其是对于PDF设备而言。例如,创建带有汉字的PDF图形可能需要很多额外的工作。同样,R用户可能已在其系统中安装了各种字体,但是对于许多图形设备,没有直接且可移植的方式来使用这些字体。
Winston Chang开发的软件包是解决此问题的一个不错的解决方案,它主要致力于在PDF图形设备中使用TrueType字体(.ttf)。现在,新的解决方案showtext程序包能够支持更多的字体格式和更多的图形设备,并且避免使用外部软件。 showtext 使在R图形中使用各种类型的字体(TrueType,OpenType,Type 1,Web字体等)更加容易。
# Sat Oct 24 13:46:10 2020 -
# 字符编码:UTF-8
# R 版本:R x64 4.0.3 for window 10
# cgh163email@163.com
# 个人笔记不负责任
# —— 拎了个梨🍐
.rs.restartR()
library(showtext)
library(shadowtext)
library(ggplot2)
rm(list=ls());gc()
# Sat Oct 24 14:07:39 2020 ---grid.shadowtext函数-用于grid系统--------------------------
# 蓝色45度3粗:
grid.shadowtext("grid is awesome",
gp = gpar(cex = 3, col = "blue"),
rot = 45)
# Sat Oct 24 14:05:31 2020 --
grid.shadowtext(
label = "你好R4.0.3", # 文字
x = .5, # 位置
y = 0.4, # 位置
just = 'top', # 对齐方式
hjust = .2, # 左右偏移
vjust = .6, # 上下偏移
rot = 15, # 旋转角度
check.overlap = T, #检查重叠情况
gp = gpar(cex = 5, col = "#FFFFCC"), # 字大小和填充色
bg.colour = "green", # 阴影颜色
bg.r = 0.05 #阴影粗细
,draw = T # 是否显示在plot面板
)
dev.copy(png, "grid.shadowtext函数,grid系统.png");dev.off() # 保存当前显示的图像
# Sat Oct 24 14:05:22 2020 --end
#
# Sat Oct 24 14:09:15 2020 --geom_shadowtext函数,用于ggplot2系统----------------------------
#
library(ggplot2)
d
head(d)
# x y label
# 1 -0.7054128 0.7426358 hello
# 2 -0.9319757 -0.5621444 world
# 3 -1.5624522 -0.1594412 !!!
#
ggplot(d, aes(x, y)) +
geom_shadowtext(aes(label = label, color = label),
bg.colour = 'firebrick')
ggsave(filename = 'geom_shadowtext函数,用于ggplot2系统.jpg',dpi = 300)
# Sat Oct 24 14:16:04 2020 ------------------------------
library(showtext)
#
#https://gitee.com/youmigo/bate/tree/master/fir%EF%BC%8Cfont
showtext_auto() # 自动使用字体
# 先定义字体和昵称:
font_add(family = "f1",regular = 'esri_30s.ttf' ) # 昵称,字体地址
font_add(family = 'f2',regular = 'FiraMono-Bold.ttf')
font_add(family = 'f3',regular = 'simhei.ttf')
hist(rnorm(1000), breaks = 30, col = "steelblue", border = "white",
main = "", xlab = "", ylab = "")
# dev.off()
windows() # 新开一个设备保证起效。
title(main = "Histogram of 12354NormO(∩_∩)O哈哈~bers", family = "f1", cex.main = 2)
title(ylab = "Frequen8524cy 哈哈", family = "f2", cex.lab = 2)
text(2, 70, "N = 100红红火65482.火恍恍惚惚0", family = "f3", cex = 2.5)
# Sat Oct 24 16:50:11 2020 --end
#
geom_shadowtext函数,用于ggplot2系统
grid.shadowtext函数,grid系统
具体支持几个字看字体
在ggplot2中使用
# Fri Aug 14 02:07:13 2020 -在ggplot2中使用-----------------------------
library(showtext)
require(ggplot2)
showtext_auto() # 开启自动调用
dev.off()
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,
x = 1.15,
y = 1.1,
family = "f3",
size = 15,
label = "\u4F60\u597D\uFF0C\u4E16\u754C,R 4.0.3"
) +
annotate(
"text",
x = 1.12,
0.9,
label = 'Chinese112539 for "Hello, world!"',
family = "f1",
fontface = "italic",
size = 9
) + annotate(
"text",
x = 1.2,
1,
1.3,
label = enc2utf8('shadowtext&showtext'),
family = "f2",
fontface = "italic",
size = 16
)
## PNG device
ggsave("字体作用在ggplot2.jpg", width = 7, height = 4, dpi = 96)
## turn off if no longer needed
showtext_auto(FALSE)
# Sat Oct 24 17:16:47 2020 --end
字体作用在ggplot2