修改ggplot2中的字体

我正在寻找一种方法来修改ggplot中的字体types。 此刻,我会很乐意简单地将字体更改为'courier'字体家族,但最终我的目标是调用自定义字体模板 – 任何关于后一点的input都将非常感激。

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

  • ggplot2:如何更改geom_text中的标签字体
  • R新闻卷6/2 ,PostScript和PDFgraphics中的非标准字体,Murrell和Ripley。
  • ComputerGodern字体在ggplot2graphics?

这可能是因为我仍然是一个ggplot2的绝望的业余爱好者,但我甚至无法将图表字体切换到快递。 任何帮助? 我已经将下面的图表的数据和代码一起包括在内,所以希望这是非常容易的。

我认为你的答案很好,但你可以更简单地做到这一点:

 install.packages("extrafont");library(extrafont) font_import("Trebuchet MS") library(ggplot2) qplot(1:10)+theme(text=element_text(family="Trebuchet MS")) 

整理我的查询相当简单的麻烦。 这是一个两步走的解决scheme,如果没有听到回应的成员的build议,我就不会到达这个解决scheme。

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

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

John Dunavent创build了一个函数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对象的字体设置之前创build一个情节。 上面的詹姆斯的解决scheme完美的工作,虽然。 而不是使用标准字体,我设置fontfamily =“F”来引入我在theme_min()中select的自定义字体,即:

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

希望这对其他想要在其图表上修改字体的用户很有用。

欢迎所有帮助我的人解决这个问题! 亚伦

看看theme_text()的家庭参数

 dummy <- data.frame(A = rnorm(10), B = rnorm(10)) ggplot(dummy, aes(x = A, y = B)) + geom_point() #helvetica = default ggplot(dummy, aes(x = A, y = B)) + geom_point() + opts(axis.title.x = theme_text(family = "sans", face = "bold")) #times ggplot(dummy, aes(x = A, y = B)) + geom_point() + opts(axis.title.x = theme_text(family = "serif", face = "bold")) #courier ggplot(dummy, aes(x = A, y = B)) + geom_point() + opts(axis.title.x = theme_text(family = "mono", face = "bold")) 

受到科斯克博客上的一篇文章的启发,我想到了这个:

 theme_set( theme_bw( base_family= "serif")) theme_update( panel.grid.minor= theme_blank(), panel.grid.major= theme_blank(), panel.background= theme_blank(), axis.title.x= theme_blank(), axis.text.x= theme_text( family= "serif", angle= 90, hjust= 1 ), axis.text.x= theme_text( family= "serif"), axis.title.y= theme_blank()) theme_map <- theme_get() theme_set( theme_bw()) 

现在,当我想要使用特定的主题:

 last_plot() + theme_map 

因人而异。

顺便说一句,如果我有权力,我会投票下来的首选答案:

 > grid.gedit("GRID.text",gp=gpar(fontfamily="mono")) Error in editDLfromGPath(gPath, specs, strict, grep, global, redraw) : 'gPath' (GRID.text) not found 

不知道这是什么意思。 我也没有提供一个链接来评论这个答案。 也许在网站上发生了一些变化。

您可以使用grid.gedit设置由geom_text生成的标签的字体:

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

在你制作完你的原始情节之后再调用它。

另外检查出开罗软件包,它支持完全切换所有你select的字体。 http://rforge.net/doc/packages/Cairo/00Index.html

这似乎是最简单的解决scheme,我的钱。

有些在df中播放数据,并将其制作成简单的graphics“p”,并带有很好的长x和y标签,所以我们可以看到字体的变化:

 df <- data.frame(A = rnorm(10), B = rnorm(10)) p = ggplot(data = df, aes(x = A, y = B)) + geom_point() p = p + xlab("A long x-string so we can see the effect of the font switch") p = p + ylab("Likewise up the ordinate") 

我们可以查看任何字体的默认情节:

 p 

现在我们切换到Optima,添加一些不错的标题和副标题,以获得Optima的荣耀:

 label = "Now we switch to Optima" subtitle = "Optima is a nice font: https://en.wikipedia.org/wiki/Optima#Usages" 

毕竟,我们用新的字体打印

 # the only line you need to read: p + theme(text = element_text(family = "Optima", , face = "bold")) p = p + ggtitle(label = label, subtitle = subtitle) p 

在最佳的字体的图表