R语言绘图基础之图形保存
在用ggplot2绘图时,经常发现图片中的中文无法显示或者中文字体格式不对,这种情况下,展现的图片是残缺不全的,为了解决R语言图片保存时,无法显示中文的问题,现对showtext()和Cairo包进行简单的运用。
01
常见方法
代码语言:javascript复制> library(showtext)
> font_files()#显示中文字体有哪些?
path file family
1 /Library/Fonts Al Nile.ttc Al Nile
2 /Library/Fonts Al Tarikh.ttc Al Tarikh
3 /Library/Fonts AlBayan.ttc Al Bayan
4 /Library/Fonts AmericanTypewriter.ttc American Typewriter
5 /Library/Fonts Andale Mono.ttf Andale Mono
6 /Library/Fonts Apple Chancery.ttf Apple Chancery
7 /Library/Fonts AppleGothic.ttf AppleGothic
8 /Library/Fonts AppleMyungjo.ttf AppleMyungjo
9 /Library/Fonts Arial Black.ttf Arial Black
10 /Library/Fonts Arial Bold Italic.ttf Arial
11 /Library/Fonts Arial Bold.ttf Arial
12 /Library/Fonts Arial Italic.ttf Arial
13 /Library/Fonts Arial Narrow Bold Italic.ttf Arial Narrow
14 /Library/Fonts Arial Narrow Bold.ttf Arial Narrow
15 /Library/Fonts Arial Narrow Italic.ttf Arial Narrow
16 /Library/Fonts Arial Narrow.ttf Arial Narrow
17 /Library/Fonts Arial Rounded Bold.ttf Arial Rounded MT Bold
18 /Library/Fonts Arial Unicode.ttf Arial Unicode MS
19 /Library/Fonts Arial.ttf Arial
20 /Library/Fonts Athelas.ttc Athelas
21 /Library/Fonts Ayuthaya.ttf Ayuthaya
22 /Library/Fonts Baghdad.ttc Baghdad
23 /Library/Fonts Bangla MN.ttc Bangla MN
24 /Library/Fonts Bangla Sangam MN.ttc Bangla Sangam MN
25 /Library/Fonts Baskerville.ttc Baskerville
26 /Library/Fonts Beirut.ttc Beirut
27 /Library/Fonts BigCaslon.ttf Big Caslon
28 /Library/Fonts Bodoni 72 OS.ttc Bodoni 72 Oldstyle
29 /Library/Fonts Bodoni 72 Smallcaps Book.ttf Bodoni 72 Smallcaps
30 /Library/Fonts Bodoni 72.ttc Bodoni 72
31 /Library/Fonts Bodoni Ornaments.ttf Bodoni Ornaments
32 /Library/Fonts Bradley Hand Bold.ttf Bradley Hand
33 /Library/Fonts Brush Script.ttf Brush Script MT
34 /Library/Fonts Chalkboard.ttc Chalkboard
35 /Library/Fonts ChalkboardSE.ttc Chalkboard SE
36 /Library/Fonts Chalkduster.ttf Chalkduster
37 /Library/Fonts Charter.ttc Charter
38 /Library/Fonts Cochin.ttc Cochin
39 /Library/Fonts Comic Sans MS Bold.ttf Comic Sans MS
40 /Library/Fonts Comic Sans MS.ttf Comic Sans MS
41 /Library/Fonts Copperplate.ttc Copperplate
42 /Library/Fonts Corsiva.ttc Corsiva Hebrew
43 /Library/Fonts Courier New Bold Italic.ttf Courier New
44 /Library/Fonts Courier New Bold.ttf Courier New
45 /Library/Fonts Courier New Italic.ttf Courier New
46 /Library/Fonts Courier New.ttf Courier New
47 /Library/Fonts Damascus.ttc Damascus
48 /Library/Fonts DecoTypeNaskh.ttc DecoType Naskh
49 /Library/Fonts Devanagari Sangam MN.ttc Devanagari Sangam MN
50 /Library/Fonts DevanagariMT.ttc Devanagari MT
51 /Library/Fonts Didot.ttc Didot
52 /Library/Fonts DIN Alternate Bold.ttf DIN Alternate
53 /Library/Fonts DIN Condensed Bold.ttf DIN Condensed
54 /Library/Fonts Diwan Kufi.ttc Diwan Kufi
55 /Library/Fonts Diwan Thuluth.ttf Diwan Thuluth
56 /Library/Fonts EuphemiaCAS.ttc Euphemia UCAS
57 /Library/Fonts Farah.ttc Farah
58 /Library/Fonts Farisi.ttf Farisi
59 /Library/Fonts Futura.ttc Futura
60 /Library/Fonts Georgia Bold Italic.ttf Georgia
61 /Library/Fonts Georgia Bold.ttf Georgia
62 /Library/Fonts Georgia Italic.ttf Georgia
63 /Library/Fonts Georgia.ttf Georgia
64 /Library/Fonts GillSans.ttc Gill Sans
65 /Library/Fonts Gujarati Sangam MN.ttc Gujarati Sangam MN
66 /Library/Fonts GujaratiMT.ttc Gujarati MT
67 /Library/Fonts Gurmukhi MN.ttc Gurmukhi MN
68 /Library/Fonts Gurmukhi Sangam MN.ttc Gurmukhi Sangam MN
69 /Library/Fonts Gurmukhi.ttf Gurmukhi MT
70 /Library/Fonts Herculanum.ttf Herculanum
71 /Library/Fonts Hoefler Text Ornaments.ttf Hoefler Text
72 /Library/Fonts Hoefler Text.ttc Hoefler Text
73 /Library/Fonts Impact.ttf Impact
74 /Library/Fonts InaiMathi-MN.ttc InaiMathi
75 /Library/Fonts Iowan Old Style.ttc Iowan Old Style
76 /Library/Fonts ITFDevanagari.ttc ITF Devanagari
77 /Library/Fonts Kailasa.ttc Kailasa
78 /Library/Fonts Kannada MN.ttc Kannada MN
79 /Library/Fonts Kannada Sangam MN.ttc Kannada Sangam MN
80 /Library/Fonts Kefa.ttc Kefa
81 /Library/Fonts Khmer MN.ttc Khmer MN
82 /Library/Fonts Khmer Sangam MN.ttf Khmer Sangam MN
83 /Library/Fonts Kokonor.ttf Kokonor
84 /Library/Fonts Krungthep.ttf Krungthep
85 /Library/Fonts KufiStandardGK.ttc KufiStandardGK
86 /Library/Fonts Lao MN.ttc Lao MN
87 /Library/Fonts Lao Sangam MN.ttf Lao Sangam MN
88 /Library/Fonts Luminari.ttf Luminari
89 /Library/Fonts Malayalam MN.ttc Malayalam MN
90 /Library/Fonts Malayalam Sangam MN.ttc Malayalam Sangam MN
91 /Library/Fonts Marion.ttc Marion
92 /Library/Fonts Microsoft Sans Serif.ttf Microsoft Sans Serif
93 /Library/Fonts Mishafi Gold.ttf Mishafi Gold
94 /Library/Fonts Mishafi.ttf Mishafi
95 /Library/Fonts Mshtakan.ttc Mshtakan
96 /Library/Fonts Muna.ttc Muna
97 /Library/Fonts Myanmar MN.ttc Myanmar MN
98 /Library/Fonts Myanmar Sangam MN.ttc Myanmar Sangam MN
99 /Library/Fonts Nadeem.ttc Nadeem
100 /Library/Fonts NewPeninimMT.ttc New Peninim MT
101 /Library/Fonts NISC18030.ttf GB18030 Bitmap
102 /Library/Fonts Oriya MN.ttc Oriya MN
103 /Library/Fonts Oriya Sangam MN.ttc Oriya Sangam MN
104 /Library/Fonts Papyrus.ttc Papyrus
105 /Library/Fonts Phosphate.ttc Phosphate
106 /Library/Fonts PlantagenetCherokee.ttf Plantagenet Cherokee
107 /Library/Fonts PTMono.ttc PT Mono
108 /Library/Fonts PTSans.ttc PT Sans
109 /Library/Fonts PTSerif.ttc PT Serif
110 /Library/Fonts PTSerifCaption.ttc PT Serif Caption
111 /Library/Fonts Raanana.ttc Raanana
112 /Library/Fonts Sana.ttc Sana
113 /Library/Fonts Sathu.ttf Sathu
114 /Library/Fonts Savoye LET.ttc Savoye LET
115 /Library/Fonts Seravek.ttc Seravek
116 /Library/Fonts Shree714.ttc Shree Devanagari 714
117 /Library/Fonts SignPainter.ttc SignPainter-HouseScript
118 /Library/Fonts Silom.ttf Silom
119 /Library/Fonts Sinhala MN.ttc Sinhala MN
120 /Library/Fonts Sinhala Sangam MN.ttc Sinhala Sangam MN
121 /Library/Fonts Skia.ttf Skia
122 /Library/Fonts SnellRoundhand.ttc Snell Roundhand
123 /Library/Fonts Songti.ttc Songti SC
124 /Library/Fonts STIXGeneral.otf STIXGeneral
125 /Library/Fonts STIXGeneralBol.otf STIXGeneral
126 /Library/Fonts STIXGeneralBolIta.otf STIXGeneral
127 /Library/Fonts STIXGeneralItalic.otf STIXGeneral
128 /Library/Fonts STIXIntDBol.otf STIXIntegralsD
129 /Library/Fonts STIXIntDReg.otf STIXIntegralsD
130 /Library/Fonts STIXIntSmBol.otf STIXIntegralsSm
131 /Library/Fonts STIXIntSmReg.otf STIXIntegralsSm
132 /Library/Fonts STIXIntUpBol.otf STIXIntegralsUp
133 /Library/Fonts STIXIntUpDBol.otf STIXIntegralsUpD
134 /Library/Fonts STIXIntUpDReg.otf STIXIntegralsUpD
135 /Library/Fonts STIXIntUpReg.otf STIXIntegralsUp
136 /Library/Fonts STIXIntUpSmBol.otf STIXIntegralsUpSm
137 /Library/Fonts STIXIntUpSmReg.otf STIXIntegralsUpSm
138 /Library/Fonts STIXNonUni.otf STIXNonUnicode
139 /Library/Fonts STIXNonUniBol.otf STIXNonUnicode
140 /Library/Fonts STIXNonUniBolIta.otf STIXNonUnicode
141 /Library/Fonts STIXNonUniIta.otf STIXNonUnicode
142 /Library/Fonts STIXSizFiveSymReg.otf STIXSizeFiveSym
143 /Library/Fonts STIXSizFourSymBol.otf STIXSizeFourSym
144 /Library/Fonts STIXSizFourSymReg.otf STIXSizeFourSym
145 /Library/Fonts STIXSizOneSymBol.otf STIXSizeOneSym
146 /Library/Fonts STIXSizOneSymReg.otf STIXSizeOneSym
147 /Library/Fonts STIXSizThreeSymBol.otf STIXSizeThreeSym
148 /Library/Fonts STIXSizThreeSymReg.otf STIXSizeThreeSym
149 /Library/Fonts STIXSizTwoSymBol.otf STIXSizeTwoSym
150 /Library/Fonts STIXSizTwoSymReg.otf STIXSizeTwoSym
151 /Library/Fonts STIXVar.otf STIXVariants
152 /Library/Fonts STIXVarBol.otf STIXVariants
153 /Library/Fonts SukhumvitSet.ttc Sukhumvit Set
154 /Library/Fonts SuperClarendon.ttc Superclarendon
155 /Library/Fonts Tahoma Bold.ttf Tahoma
156 /Library/Fonts Tahoma.ttf Tahoma
157 /Library/Fonts Tamil MN.ttc Tamil MN
158 /Library/Fonts Tamil Sangam MN.ttc Tamil Sangam MN
159 /Library/Fonts TeamViewer13.otf TeamViewer13
160 /Library/Fonts Telugu MN.ttc Telugu MN
161 /Library/Fonts Telugu Sangam MN.ttc Telugu Sangam MN
162 /Library/Fonts Times New Roman Bold Italic.ttf Times New Roman
163 /Library/Fonts Times New Roman Bold.ttf Times New Roman
164 /Library/Fonts Times New Roman Italic.ttf Times New Roman
165 /Library/Fonts Times New Roman.ttf Times New Roman
166 /Library/Fonts Trattatello.ttf Trattatello
face version ps_name
1 Regular 13.0d2e2 AlNile
2 Regular 13.0d2e1 AlTarikh
3 Plain 13.0d1e6 AlBayan
4 Regular 13.0d1e4 AmericanTypewriter
5 Regular Version 2.00x AndaleMono
6 Chancery 13.0d1e4 Apple-Chancery
7 Regular 13.0d1e3 AppleGothic
8 Regular 13.0d1e6 AppleMyungjo
9 Regular Version 5.00.1x Arial-Black
10 Bold Italic Version 5.00.2x Arial-BoldItalicMT
11 Bold Version 5.01.2x Arial-BoldMT
12 Italic Version 5.00.2x Arial-ItalicMT
13 Bold Italic Version 2.38.1x ArialNarrow-BoldItalic
14 Bold Version 2.38.1x ArialNarrow-Bold
15 Italic Version 2.38.1x ArialNarrow-Italic
16 Regular Version 2.38.1x ArialNarrow
17 Regular Version 1.51x ArialRoundedMTBold
18 Regular Version 1.01x ArialUnicodeMS
19 Regular Version 5.01.2x ArialMT
20 Regular 13.0d1e3 Athelas-Regular
21 Regular 13.0d1e7 Ayuthaya
22 Regular 13.0d1e5 Baghdad
23 Regular 13.0d1e5 BanglaMN
24 Regular 13.0d1e5 BanglaSangamMN
25 Regular 13.0d1e10 Baskerville
26 Regular 13.0d1e6 Beirut
27 Medium 13.0d1e11 BigCaslon-Medium
28 Book 13.0d2e1 BodoniSvtyTwoOSITCTT-Book
29 Book 13.0d2e1 BodoniSvtyTwoSCITCTT-Book
30 Book 13.0d2e1 BodoniSvtyTwoITCTT-Book
31 Regular 13.0d2e1 BodoniOrnamentsITCTT
32 Bold 13.0d1e2 BradleyHandITCTT-Bold
33 Italic Version 1.52x-1 BrushScriptMT
34 Regular 13.0d1e2 Chalkboard
35 Light 13.0d1e2 ChalkboardSE-Light
36 Regular 13.0d2e1 Chalkduster
37 Roman 13.0d1e3 Charter-Roman
38 Regular 13.0d2e1 Cochin
39 Bold Version 5.00x ComicSansMS-Bold
40 Regular Version 5.00x ComicSansMS
41 Regular 13.0d1e2 Copperplate
42 Regular 13.0d1e4 CorsivaHebrew
43 Bold Italic Version 5.00x CourierNewPS-BoldItalicMT
44 Bold Version 5.00.2x CourierNewPS-BoldMT
45 Italic Version 5.00.1x CourierNewPS-ItalicMT
46 Regular Version 5.00.2x CourierNewPSMT
47 Regular 13.0d3e9 Damascus
48 Regular 13.0d1e4 DecoTypeNaskh
49 Regular 13.0d1e2 DevanagariSangamMN
50 Regular 13.0d1e3 DevanagariMT
51 Regular 13.0d1e3 Didot
52 Bold 13.0d1e2 DINAlternate-Bold
53 Bold 13.2d1e1 DINCondensed-Bold
54 Regular 13.0d2e1 DiwanKufi
55 Regular 13.0d1e5 DiwanThuluth
56 Regular 13.0d1e2 EuphemiaUCAS
57 Regular 13.0d2e3 Farah
58 Regular 13.0d1e3 Farisi
59 Medium 13.0d1e3 Futura-Medium
60 Bold Italic Version 5.00x-4 Georgia-BoldItalic
61 Bold Version 5.00x-4 Georgia-Bold
62 Italic Version 5.00x-4 Georgia-Italic
63 Regular Version 5.00x-4 Georgia
64 Regular 13.0d1e4 GillSans
65 Regular 13.0d2e1 GujaratiSangamMN
66 Regular 13.0d1e2 GujaratiMT
67 Regular 13.0d2e1 GurmukhiMN
68 Regular 13.0d1e3 GurmukhiSangamMN
69 Regular 13.0d1e4 MonotypeGurmukhi
70 Regular 13.0d1e2 Herculanum
71 Ornaments 13.0d2e20 HoeflerText-Ornaments
72 Regular 13.0d2e20 HoeflerText-Regular
73 Regular Version 5.00x Impact
74 Regular 13.0d1e5 InaiMathi
75 Roman 13.0d1e4 IowanOldStyle-Roman
76 Book 13.0d3e1 ITFDevanagari-Book
77 Regular 13.0d1e3 Kailasa
78 Regular 13.0d2e1 KannadaMN
79 Regular 13.0d2e1 KannadaSangamMN
80 Regular 13.0d1e3 Kefa-Regular
81 Regular 13.0d1e3 KhmerMN
82 Regular 13.0d1e4 KhmerSangamMN
83 Regular 13.0d1e3 Kokonor
84 Regular 13.0d2e1 Krungthep
85 Regular 13.0d1e11 KufiStandardGK
86 Regular 13.0d1e5 LaoMN
87 Regular 13.0d1e3 LaoSangamMN
88 Regular 13.0d1e2 Luminari-Regular
89 Regular 13.0d4e1 MalayalamMN
90 Regular 13.0d4e1 MalayalamSangamMN
91 Regular 13.0d1e2 Marion-Regular
92 Regular Version 5.00.1x MicrosoftSansSerif
93 Regular 13.0d2e1 DiwanMishafiGold
94 Regular 13.0d2e1 DiwanMishafi
95 Regular 13.0d1e3 Mshtakan
96 Regular 13.0d1e4 Muna
97 Regular 13.0d1e2 MyanmarMN
98 Regular 13.0d1e2 MyanmarSangamMN
99 Regular 13.0d1e3 Nadeem
100 Regular 13.0d1e4 NewPeninimMT
101 Regular 13.0d1e1 GB18030Bitmap
102 Regular 13.0d3e1 OriyaMN
103 Regular 13.0d3e1 OriyaSangamMN
104 Condensed 13.0d1e2 Papyrus-Condensed
105 Inline 13.0d1e2 Phosphate-Inline
106 Regular 13.0d1e3 PlantagenetCherokee
107 Bold 13.0d2e4 PTMono-Bold
108 Regular 13.0d3e2 PTSans-Regular
109 Regular 13.0d2e1 PTSerif-Regular
110 Regular 13.0d2e1 PTSerif-Caption
111 Regular 13.0d1e3 Raanana
112 Regular 13.0d1e4 Sana
113 Regular 13.0d1e2 Sathu
114 Plain 13.0d2e4 SavoyeLetPlain
115 Regular 13.0d3e2 Seravek
116 Regular 13.0d2e1 ShreeDev0714
117 Regular 13.0d1e5 SignPainter-HouseScript
118 Regular 13.0d2e1 Silom
119 Regular 13.0d1e2 SinhalaMN
120 Regular 13.0d1e2 SinhalaSangamMN
121 Regular 13.0d1e54 Skia-Regular
122 Regular 13.0d1e1 SnellRoundhand
123 Black 13.0d3e2 STSongti-SC-Black
124 Regular Version 1.1.0 STIXGeneral-Regular
125 Bold Version 1.1.0 STIXGeneral-Bold
126 Bold Italic Version 1.1.0 STIXGeneral-BoldItalic
127 Italic Version 1.1.0 STIXGeneral-Italic
128 Bold Version 1.1.0 STIXIntegralsD-Bold
129 Regular Version 1.1.0 STIXIntegralsD-Regular
130 Bold Version 1.1.0 STIXIntegralsSm-Bold
131 Regular Version 1.1.0 STIXIntegralsSm-Regular
132 Bold Version 1.1.0 STIXIntegralsUp-Bold
133 Bold Version 1.1.0 STIXIntegralsUpD-Bold
134 Regular Version 1.1.0 STIXIntegralsUpD-Regular
135 Regular Version 1.1.0 STIXIntegralsUp-Regular
136 Bold Version 1.1.0 STIXIntegralsUpSm-Bold
137 Regular Version 1.1.0 STIXIntegralsUpSm-Regular
138 Regular Version 1.1.0 STIXNonUnicode-Regular
139 Bold Version 1.1.0 STIXNonUnicode-Bold
140 Bold Italic Version 1.1.0 STIXNonUnicode-BoldItalic
141 Italic Version 1.1.0 STIXNonUnicode-Italic
142 Regular Version 1.1.0 STIXSizeFiveSym-Regular
143 Bold Version 1.1.0 STIXSizeFourSym-Bold
144 Regular Version 1.1.0 STIXSizeFourSym-Regular
145 Bold Version 1.1.0 STIXSizeOneSym-Bold
146 Regular Version 1.1.0 STIXSizeOneSym-Regular
147 Bold Version 1.1.0 STIXSizeThreeSym-Bold
148 Regular Version 1.1.0 STIXSizeThreeSym-Regular
149 Bold Version 1.1.0 STIXSizeTwoSym-Bold
150 Regular Version 1.1.0 STIXSizeTwoSym-Regular
151 Regular Version 1.1.0 STIXVariants-Regular
152 Bold Version 1.1.0 STIXVariants-Bold
153 Thin 13.0d1e6 SukhumvitSet-Thin
154 Regular 13.0d1e4 Superclarendon-Regular
155 Bold Version 5.01.1x Tahoma-Bold
156 Regular Version 5.01.2x Tahoma
157 Regular 13.0d2e1 TamilMN
158 Regular 13.0d2e14 TamilSangamMN
159 Medium Version 013.10 TeamViewer13
160 Regular 13.0d1e3 TeluguMN
161 Regular 13.0d1e5 TeluguSangamMN
162 Bold Italic Version 5.00.3x TimesNewRomanPS-BoldItalicMT
163 Bold Version 5.01.3x TimesNewRomanPS-BoldMT
164 Italic Version 5.00.3x TimesNewRomanPS-ItalicMT
165 Regular Version 5.01.3x TimesNewRomanPSMT
166 Regular 13.0d2e2 Trattatello
[ reached 'max' / getOption("max.print") -- omitted 14 rows ]
代码语言:javascript复制quartz(family = "STXihei")
#选择中文显示的字体格式STXihei
代码语言:javascript复制>ggplot(data=mydata1,aes(City,Age)) geom_bar(stat="identity",width=0.8,colour="black",size=0.25,fill="red",alpha=1)
#若想使得ggplot绘制的图片中的中文能够显示
一定要先运行quartz(),否则无法实现效果。
02
高级方法
代码语言:javascript复制install.packages("Cairo")
#安装Cairo,一定要在网络流畅的情况下安装,否则很容易失败,错误情况查看往期文章的文末“疫”外收获-nCov2019全球疫情之南丁格尔玫瑰图。
代码语言:javascript复制library(Cairo)#加载Cairo
代码语言:javascript复制CairoPDF("char_1.pdf")
#pdf图片保存的文件名称,除了pdf格式外,还可以 保存png,jpeg。
代码语言:javascript复制ggplot(data=mydata1,aes(City,Age)) geom_bar(stat="identity",width=0.8,colour="black",size=0.25,fill="pink",alpha=1)
#开始绘图之间一定要设置pdf保存文件,否则图片中仍然无法显示中文。
代码语言:javascript复制dev.off()
#这一步很重要,不可省略,否则char_1.pdf文件打开是空白的,文件大小为0。
小结
showtext:在许多情况下,特别是对于PDF设备,在R图中使用非标准字体不是一件容易的事情.
showtext使得在R图中使用各种类型的字体(TrueType、OpenType、Type 1、web字体等)变得更加容易。
quartz((): 对于Windows,在图形窗口中选择“文件”→“另存为”,然后在弹出的对话框中选择想要的格式和保存位置即可。在Mac上,当Quartz图形窗口处于高亮状态时,点选菜单栏中的“文件”→“另存为”即可。其提供的输出格式仅有PDF,若要转换为jpeg或者png,需要打开pdf后再另存时修改格式。在UNIX系统中,图形必须使用代码来保存,不是很推荐使用。
Cairo():可以生成高质量的PNG、JPEG、TIFF位图文件、高分辨率的PDF文件以及嵌入的字体、SVG图形和PostScript文件,提供了X11和Windows交互式图形设备。CairoX11、CairoPNG、CairoPDF、CairoPS和CairoSVG都是Cairo的便利包装器,推荐使用。