我正在寻找一种方法来修改ggplot中的字体类型.目前我很乐意简单地将字体更改为'courier'字体系列,但最终我的目标是调用自定义字体模板 - 对后一点的任何输入都将非常感激.
我做了一些功课,看了下面的帖子和文章:
这可能是因为我仍然是ggplot2的无望业余爱好者,但我甚至无法将图表字体切换为快递.有帮助吗?我已经在下面列出了相关图表的数据以及代码,所以希望这很容易理解.
Ste*_*ell 30
我认为你的答案很好,但你可以更简单地做到:
install.packages("extrafont");library(extrafont)
font_import("Trebuchet MS")
library(ggplot2)
qplot(1:10)+theme(text=element_text(family="Trebuchet MS"))
Run Code Online (Sandbox Code Playgroud)
Aar*_*ron 26
用相当小的麻烦排序我的查询.这是一个两步解决方案,如果不遵循响应成员的建议,我就不会达成这个解决方案.
要更改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)
Run Code Online (Sandbox Code Playgroud)
尴尬的是,在创建绘图之前,没有办法(我发现)一般性地修改geom_text对象的字体设置.尽管如此,詹姆斯的解决方案完全适用于此.而不是使用标准字体,我设置fontfamily ="F"来引入我在theme_min()中选择的自定义字体,即:
grid.gedit("GRID.text",gp=gpar(fontfamily="F"))
Run Code Online (Sandbox Code Playgroud)
希望这对于希望在其图形上修改字体的任何其他用户都很有用.
欢呼所有帮助我解决问题的人!亚伦
Thi*_*rry 11
看看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"))
Run Code Online (Sandbox Code Playgroud)
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())
Run Code Online (Sandbox Code Playgroud)
现在,当我想使用该特定主题时:
last_plot() + theme_map
Run Code Online (Sandbox Code Playgroud)
YMMV。
顺便说一句,如果我有权力,我会否决首选答案:
> grid.gedit("GRID.text",gp=gpar(fontfamily="mono"))
Error in editDLfromGPath(gPath, specs, strict, grep, global, redraw) :
'gPath' (GRID.text) not found
Run Code Online (Sandbox Code Playgroud)
不知道这意味着什么。我也没有提供评论此答案的链接。网站上的内容可能已更改。