修改 ggplot2 中的字体 [英] Modifying fonts in ggplot2

查看:83
本文介绍了修改 ggplot2 中的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在 ggplot 中修改字体类型的方法.目前,我很乐意简单地将字体更改为courier"字体系列,但最终我的目标是调用自定义字体模板——非常感谢对后一点的任何输入.

I am looking for a way to modify font types in ggplot. At the moment I would be happy enough to simply change fonts to the 'courier' font family, but ultimately my goal is to call a custom font template--any input on this latter point would be very much appreciated.

我做了一些功课,看了下面的帖子和文章:

I've done a bit of homework, looking at the following posts and articles:

  • ggplot2: How to change font of labels in geom_text
  • R News Volume 6/2, Non-Standard Fonts in PostScript and PDF Graphic, Murrell and Ripley.
  • ComputerModern font in ggplot2 graphics?

可能是因为我对ggplot2还是个无望的业余爱好者,但我什至无法将图表字体切换为courier.有什么帮助吗?我已经在下方提供了相关图表的数据以及代码,希望这些都足够容易理解.

It may be because I am still a hopeless amateur with ggplot2, but I haven't even been able to switch chart fonts to courier. Any help? I've included the data for the chart in question, below, along with the code, so hopefully this is all easy enough to follow.

推荐答案

用相当少的麻烦整理了我的查询.这是一个两步走的解决方案,如果不遵循做出回应的成员的建议,我就不会得出这个解决方案.

Sorted out my query with fairly minimal hassle. It was a two-step solution that I wouldn't have arrived at without following the advice of the members who responded.

为了更改 ggplot 文本默认值,我修改了 Brandon 提到的代码:

To change the ggplot text defaults, I adapted the code that Brandon referred me to at:

http://johndunavent.com/combined-line-and-bar-图表-ggplot2

John Dunavent 创建的函数,theme_min,可以编辑该函数以提供 ggplot 的默认选项,包括使用 windowsFonts 命令从 Windows 导入的字体.我对他的代码的改编如下:

Where John Dunavent creates a function, theme_min, that can be edited to provide the default options for a ggplot, including using fonts imported from Windows with the windowsFonts command. My adaptation of his code looks like this:

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 对象的字体设置.不过,James 上面的解决方案非常适用于此.我没有使用标准字体,而是设置 fontfamily="F" 以引入我在 theme_min() 中选择的自定义字体,即:

Awkwardly, there is no way (that I found) to generically modify the font settings for geom_text objects before a plot is created. James' solution above worked perfectly for this, though. Instead of using a standard font, I set fontfamily="F" to bring in the custom font that I selected in theme_min(), i.e.:

grid.gedit("GRID.text",gp=gpar(fontfamily="F"))

希望这对希望修改图表字体的任何其他用户有用.

Hopefully this is useful to any other users looking to modify fonts on their graphs.

向所有帮助我解决这个问题的人干杯!亚伦

Cheers to all who helped me sort this out! Aaron

这篇关于修改 ggplot2 中的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆