【问题标题】:Modifying fonts in ggplot2修改ggplot2中的字体
【发布时间】:2011-05-04 21:24:47
【问题描述】:

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

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

这可能是因为我仍然是一个无可救药的 ggplot2 业余爱好者,但我什至无法将图表字体切换为 courier。有什么帮助吗?我已经在下面包含了相关图表的数据以及代码,所以希望这一切都很容易理解。

【问题讨论】:

  • tikzDevice 和 XeTeX 怎么样?

标签: r ggplot2


【解决方案1】:

对于我来说,这似乎是最简单的解决方案。

播放df中的一些数据,做成一个简单的图形,“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

【讨论】:

  • 嘿@tim,ggplot2 带有内置字体选项。这个问题涉及您想要从系统导入 ggplot2 中不可用的字体的情况。
【解决方案2】:

我认为你的答案很好,但你可以更简单:

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

【讨论】:

  • 据我所知,对于annotate(),您需要提供字体信息,例如family = myfamily, fontface = myfontface 其中myfamily 和myfontface 应与根据您的方法传递给theme() 的内容一致。如果您有几个annotate() 电话,那就有点麻烦了。我还没有检查过,但我认为上面的答案也不涉及annotate()。
【解决方案3】:

受kohske 博客上的a post 的启发,我想出了这个:

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

YMMV.

顺便说一句,如果我有权力,我会否决首选答案:

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

不确定这是什么意思。我也没有提供评论该答案的链接;也许网站上发生了一些变化。

【讨论】:

  • 嗨 Neil,我仍然不是 ggplot2 方面的专家,但在我看来,您在此处提出的解决方案在使用 R 和 ggplot2 原生可用的字体/主题设置时效果很好。我需要能够从 windows 库中为我的绘图导入任何自定义字体,而 Brandon 和 James 帮助我想出的解决方案效果很好。下面是我使用 grid.gedit 的方法:windowsFonts(F = windowsFont(chartFont))grid.gedit("GRID.text",gp=gpar(fontfamily="F")) 你需要确保你想要的字体系列可用。
  • @NeilBest 该错误意味着您的绘图中没有任何文本层 - 即没有使用geom_text
  • 这似乎不再起作用(至少对我而言)。但是一个可行的解决方法是:theme_map$text$family = 'new font'
【解决方案4】:

以极少的麻烦整理出我的查询。这是一个两步解决方案,如果不听从响应成员的建议,我是不会得出的。

为了更改 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"))

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

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

【讨论】:

  • 并不是说 opts 命令在 ggplot2 中已经被弃用了一段时间。您应该(大部分?)能够通过将opts 替换为theme 来使用上面的代码。像theme_text 这样的一些元素规范可能需要更改为element_text
  • 大量使用'F' ;-)
【解决方案5】:

还可以查看 Cairo 包,它支持使用您选择的字体完全切换所有字体。 http://rforge.net/doc/packages/Cairo/00Index.html

【讨论】:

    【解决方案6】:

    你可以用grid.gedit设置geom_text产生的标签的字体:

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

    在制作完原始情节后调用它。

    【讨论】:

    • 伟大的 cmets,所有人,非常感谢。布兰登,你的建议是正确的,除了我在使用johndunavent.com/combined-line-and-bar-chart-ggplot2 找到的方法来修改 geom_text 字体标签时遇到问题。詹姆斯,不确定你是否熟悉那篇文章,但我将如何使用你的解决方案(效果很好)用我使用 windowsFonts() 命令引入 R 的字体修改 geom_text 标签字体?感谢你的帮助!杰夫,如果我继续在这里打砖块,我会去开罗看看……
    • windowsFonts(Verdana="TT Verdana") 然后你可以使用fontfamily="Verdana"。您可能还想在第一个参数中使用"axis.text" 和"axis.title" 设置轴标签和标题。顺便说一句,那个链接现在已经失效了,但你仍然可以通过 Google 缓存获取一些链接。
    【解决方案7】:

    看看theme_text()的family参数

    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"))
    

    【讨论】:

    • 太好了,谢谢蒂埃里——这确实是使用 R 标准字体系列库中包含的字体库修改字体类型的方法。不知道如何实现自定义字体。将查看其中一些其他帖子...
    猜你喜欢
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 2013-12-28
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2013-11-26
    相关资源
    最近更新 更多