官术网_书友最值得收藏!

  • R Graphs Cookbook Second Edition
  • Jaynal Abedin Hrishi V. Mittal
  • 401字
  • 2021-08-05 17:30:32

Setting fonts for annotations and titles

For most data analysis, we can just use the default fonts for titles. However, sometimes, we might want to choose different fonts for presentation and publication purposes. Selecting fonts can be tricky, as this depends on the operating system and the graphics device. We will see some simple ways to choose fonts in this recipe.

Getting ready

All you need to try out in this recipe is to run R and type the recipe in the command prompt. You can also choose to save the recipe as a script so that you can use it again later on.

How to do it...

The font family and face can be set with the par() command:

par(family="serif",font=2)

How it works...

A font is specified in two parts: a font family (such as Helvetica or Arial) and a font face within family (such as bold or italic).

The available font families vary by operating system and graphics devices. So, R provides some proxy values that are mapped on to the relevant available fonts irrespective of the system. Standard values for family are serif, sans, and mono.

The font argument takes numerical values: 1 corresponds to plain text (the default), 2 to bold face, 3 to italic, and 4 to bold italic.

For example, par(family="serif",font=2) sets the font to a bold Times New Roman font on Windows. You can check the other font mappings by running the windowsFonts() command at the R prompt.

The fonts for axis annotations, labels, and plot main title can be set separately using the font.axis, font.lab, and font.main arguments, respectively.

There's more...

The choice of fonts is very limited if we just use the proxy family names. However, we can use a wide range of fonts if we are exporting our graphs in the PostScript or PDF formats. The postscriptFonts() and pdfFonts() functions show us all the available fonts for these devices. To see the PDF fonts, run the following command:

names(pdfFonts())
 [1] "serif"              "sans"               "mono"                
 [4] "AvantGarde"        "Bookman"             "Courier"             
 [7] "Helvetica"         "Helvetica-Narrow"    "NewCenturySchoolbook"
[10] "Palatino"          "Times"               "URWGothic"           
[13] "URWBookman"        "NimbusMon"           "NimbusSan"           
[16] "URWHelvetica"      "NimbusSanCond"       "CenturySch"          
[19] "URWPalladio"       "NimbusRom"           "URWTimes"            
[22] "Japan1"            "Japan1HeiMin"        "Japan1GothicBBB"     
[25] "Japan1Ryumin"      "Korea1"              "Korea1deb"           
[28] "CNS1"              "GB1"                 

To use one of these font families in a PDF, we can pass the family argument to the pdf() function:

pdf(family="AvantGarde") pdf(paste(family="AvantGarde")

See also

In recipes on setting fonts for publications and presentations.

主站蜘蛛池模板: 崇州市| 荥阳市| 双牌县| 长宁区| 赤壁市| 安丘市| 图木舒克市| 龙海市| 五大连池市| 花莲县| 普兰县| 神农架林区| 海南省| 饶河县| 教育| 中宁县| 宁阳县| 繁昌县| 游戏| 施秉县| 浏阳市| 西吉县| 邳州市| 泸定县| 石首市| 博乐市| 克什克腾旗| 安平县| 彭州市| 区。| 古蔺县| 云林县| 依安县| 尉氏县| 从化市| 临沧市| 宜州市| 荣昌县| 夹江县| 阿荣旗| 马鞍山市|