看看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"))
用相当小的麻烦排序我的查询。这是一个两步解决方案,如果不遵循响应成员的建议,我就不会达成这个解决方案。
要更改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"))
希望这对于希望在其图形上修改字体的任何其他用户都很有用。
欢呼所有帮助我解决问题的人! 亚伦
还可以查看Cairo软件包,该软件包支持完全切换出您选择的所有字体。 http://rforge.net/doc/packages/Cairo/00Index.html
对于我来说,这似乎是最简单的解决方案。
有些在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
灵感来自 一个帖子 上 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
因人而异。
顺便说一句,如果我有权力,我会投下首选答案:
> grid.gedit("GRID.text",gp=gpar(fontfamily="mono")) Error in editDLfromGPath(gPath, specs, strict, grep, global, redraw) : 'gPath' (GRID.text) not found
不确定这意味着什么。我也没有提供评论这个答案的链接;也许在网站上发生了一些变化。
我认为你的答案很好,但你可以更简单地做到:
install.packages("extrafont");library(extrafont) font_import("Trebuchet MS") library(ggplot2) qplot(1:10)+theme(text=element_text(family="Trebuchet MS"))
您可以设置生成的标签的字体 geom_text 同 grid.gedit :
geom_text
grid.gedit
grid.gedit("GRID.text",gp=gpar(fontfamily="mono"))
在制作原始图表后调用此方法。