修改ggplot2中的字体

发布于 2024-09-30 05:43:39 字数 772 浏览 0 评论 0原文

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

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

这可能是因为我对 ggplot2 仍然是一个无可救药的业余爱好者,但我什至无法将图表字体切换为 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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

你的呼吸 2024-10-07 05:43:40

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

有些在 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

图表采用最佳字体

This seems like the simplest solution, for my money.

Some play data in df, and made into a simple graph, "p", with nice long x and y labels, so we can see the font change:

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

And we view the default plot in whatever that font is:

p 

Now we switch to Optima, adding some nice title and subtitle to bask in the glory of Optima:

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

And after all that, we print in the new font

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

graph in optima font

毅然前行 2024-10-07 05:43:39

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

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

I think your answer is fine but you can do it more simply:

install.packages("extrafont");library(extrafont)
font_import("Trebuchet MS")
library(ggplot2)
qplot(1:10)+theme(text=element_text(family="Trebuchet MS"))
鸠书 2024-10-07 05:43:39

以相当小的麻烦解决了我的查询。这是一个分两步走的解决方案,如果不遵循回复成员的建议,我就不可能达成这一方案。

为了更改 ggplot 文本默认值,我修改了 Brandon 向我推荐的代码:

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

John Dunavent 在其中创建了一个函数 theme_min,可以对其进行编辑以提供 ggplot 的默认选项,包括使用从 Windows 导入的字体使用 windowsFonts 命令。我对他的代码的改编如下所示:

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

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

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

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

呆橘 2024-10-07 05:43:39

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

Have a look at the family argument of 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"))
触ぅ动初心 2024-10-07 05:43:39

灵感来自 kohske 博客上的一篇文章我想出了这个:

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

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

Inspired by a post on kohske's blog I came up with this:

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

Now when I want to use that particular theme:

last_plot() + theme_map

YMMV.

BTW, if I had the power I would vote down the preferred answer:

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

Not sure what this means. Nor was I offered a link to comment on that answer; maybe something has changed on the site.

热血少△年 2024-10-07 05:43:39

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

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

在生成原始绘图后调用此方法。

You can set the font of the labels produced by geom_text with grid.gedit:

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

Call this after you have produced your original plot.

独夜无伴 2024-10-07 05:43:39

另请查看 Cairo 软件包,它支持将所有字体完全切换为您选择的字体。 http://rforge.net/doc/packages/Cairo/00Index.html

Also check out the Cairo package, which has support for totally switching out all of the fonts with those of your choosing. http://rforge.net/doc/packages/Cairo/00Index.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文