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

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

问题描述

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

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



这可能是因为我仍然是ggplot2的无望的业余爱好者,但是我甚至无法将图表字体切换为快递。任何帮助?我已经在下面的代码中包含了有关图表的数据,所以希望这很容易遵循。

解决方案

用相当小的麻烦分类我的查询。这是一个两步式的解决方案,如果没有按照回应会员的建议,我就不会到达。



要改变ggplot文本的默认值,我调整了Brandon提到的代码: b
$ b

http://johndunavent.com/combined-line-and-bar-chart-ggplot2 a>



其中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,color = textColor,
size = size,...)

opts(
axis.text.x = theme_text(),
axis.text.y = theme_text(),
axis.line = theme_blank(),
axis.ticks = theme_segment(color = axisColor,size = 0.25),
panel.border = theme_rect(color = 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 ,color = NA),
panel.grid.major = theme_line(color = gridColo
panel.grid.minor = theme_blank(),
strip.background = theme_rect(fill = NA,color = 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'))
}

##创建自定义字体类型。可能是'F','TEST',无论
windowsFonts(F = windowsFont('Wide Latin'))

##并将这行代码插入我上面列出的原始代码:
+ theme_min(font ='F',size = 10)

值得夸耀的是,是没有办法(我发现)一般修改geom_text对象的字体设置之前创建一个情节。尽管如此,詹姆斯的解决方案对此非常合适。

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

希望这对任何希望修改其图形上的字体的用户都很有用。

欢迎所有帮助我解决问题的人!
Aaron


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:

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.

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

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

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) 

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天全站免登陆