family用于控制文字的字体, 标准的取值范围为serif, sans, mono, 其中sans 为默认值
基本用法,代码示例:
par(mfrow = c(1,3))
par(cex = 1.5 , family = "serif")
plot(1:5, 1:5, main = "family serif", sub = "sub", xlab = "xlab", ylab = "ylab")
text(3, 3, "text")
par(cex = 1.5, family = "sans")
plot(1:5, 1:5, main = "family sans", sub = "sub", xlab = "xlab", ylab = "ylab")
text(3, 3, "text")
par(cex = 1.5, family = "mono")
plot(1:5, 1:5, main = "family mono", sub = "sub", xlab = "xlab", ylab = "ylab")
text(3, 3, "text")
效果图如下:
除了以上3种标准的字体外,还支持自定义字体,需要注意的是,为了使用自定义字体,必须确保字体已经安装,以windows 平台为例,在 C:WindowsFonts 目录下可以查看当前计算机上安装的字体;
直接将family 设置成自定义字体也是不行的,因为R语言不能直接访问到当前设备上的字体,必须通过对应的函数先添加字体才可以
在windows 中,首先通过windowsFont 函数设置一系列windows 下的字体,然后才可以使用对应的字体,代码示例:
windowsFonts(JP1 = windowsFont("MS Mincho"), JP2 = windowsFont("MS Gothic"), JP3 = windowsFont("Arial Unicode MS"), JP4 = windowsFont("宋体"), JP5 = windowsFont("微软雅黑")) par(mfrow = c(1,2)) par(cex = 1.5 , family = "JP4") plot(1:5, 1:5, main = "family custom", type = "n", sub = "sub", xlab = "xlab", ylab = "ylab") text(3, 3, " 星期五") par(cex = 1.5 , family = "JP5") plot(1:5, 1:5, main = "family custom", type = "n", sub = "sub", xlab = "xlab", ylab = "ylab") text(3, 3, " 星期五")
效果图如下:
linux 平台也是类似的,只不过图形设备不同,使用的添加字体的函数不一样,类似的函数有postscriptFonts, pdfFonts等