以极少的麻烦整理出我的查询。这是一个两步解决方案,如果不听从响应成员的建议,我是不会得出的。
为了更改 ggplot 文本默认值,我修改了 Brandon 提到的代码:
http://johndunavent.com/combined-line-and-bar-chart-ggplot2
John Dunavent 创建了一个函数 theme_min,可以对其进行编辑以提供 ggplot 的默认选项,包括使用 windowsFonts 命令从 Windows 导入的字体。我对他的代码的改编是这样的:
theme_min = function (size=10, font=NA, face='plain',
panelColor=backgroundColor, axisColor='#999999',
gridColor=gridLinesColor, textColor='black')
{
theme_text = function(...)
ggplot2::theme_text(family=font, face=face, colour=textColor,
size=size, ...)
opts(
axis.text.x = theme_text(),
axis.text.y = theme_text(),
axis.line = theme_blank(),
axis.ticks = theme_segment(colour=axisColor, size=0.25),
panel.border = theme_rect(colour=backgroundColor),
legend.background = theme_blank(),
legend.key = theme_blank(),
legend.key.size = unit(1.5, 'lines'),
legend.text = theme_text(hjust=0),
legend.title = theme_text(hjust=0),
panel.background = theme_rect(fill=panelColor, colour=NA),
panel.grid.major = theme_line(colour=gridColor, size=0.33),
panel.grid.minor = theme_blank(),
strip.background = theme_rect(fill=NA, colour=NA),
strip.text.x = theme_text(hjust=0),
strip.text.y = theme_text(angle=-90),
plot.title = theme_text(hjust=0),
plot.margin = unit(c(0.1, 0.1, 0.1, 0.1), 'lines'))
}
##Create a custom font type. Could be 'F', 'TEST', whatever
windowsFonts(F = windowsFont('Wide Latin'))
##and insert this line of code into the original code I list above:
+ theme_min(font='F', size=10)
尴尬的是,在创建绘图之前,没有办法(我发现)一般地修改 geom_text 对象的字体设置。不过,詹姆斯的上述解决方案非常适合这一点。我没有使用标准字体,而是设置 fontfamily="F" 来引入我在 theme_min() 中选择的自定义字体,即:
grid.gedit("GRID.text",gp=gpar(fontfamily="F"))
希望这对希望修改图形字体的任何其他用户有用。
向所有帮助我解决这个问题的人干杯!
亚伦