vlambda博客
学习文章列表

基础知识 | R语言绘图保存的pdf图片无法显示中文怎么办?

R语言绘图基础之图形保存



    在用ggplot2绘图时,经常发现图片中的中文无法显示或者中文字体格式不对,这种情况下,展现的图片是残缺不全的,为了解决R语言图片保存时,无法显示中文的问题,现对showtext()和Cairo包进行简单的运用。


01

常见方法

> library(showtext)> font_files()#显示中文字体有哪些? path file family1 /Library/Fonts Al Nile.ttc Al Nile2 /Library/Fonts Al Tarikh.ttc Al Tarikh3 /Library/Fonts AlBayan.ttc Al Bayan4 /Library/Fonts AmericanTypewriter.ttc American Typewriter5 /Library/Fonts Andale Mono.ttf Andale Mono6 /Library/Fonts Apple Chancery.ttf Apple Chancery7 /Library/Fonts AppleGothic.ttf AppleGothic8 /Library/Fonts AppleMyungjo.ttf AppleMyungjo9 /Library/Fonts Arial Black.ttf Arial Black10 /Library/Fonts Arial Bold Italic.ttf Arial11 /Library/Fonts Arial Bold.ttf Arial12 /Library/Fonts Arial Italic.ttf Arial13 /Library/Fonts Arial Narrow Bold Italic.ttf Arial Narrow14 /Library/Fonts Arial Narrow Bold.ttf Arial Narrow15 /Library/Fonts Arial Narrow Italic.ttf Arial Narrow16 /Library/Fonts Arial Narrow.ttf Arial Narrow17 /Library/Fonts Arial Rounded Bold.ttf Arial Rounded MT Bold18 /Library/Fonts Arial Unicode.ttf Arial Unicode MS19 /Library/Fonts Arial.ttf Arial20 /Library/Fonts Athelas.ttc Athelas21 /Library/Fonts Ayuthaya.ttf Ayuthaya22 /Library/Fonts Baghdad.ttc Baghdad23 /Library/Fonts Bangla MN.ttc Bangla MN24 /Library/Fonts Bangla Sangam MN.ttc Bangla Sangam MN25 /Library/Fonts Baskerville.ttc Baskerville26 /Library/Fonts Beirut.ttc Beirut27 /Library/Fonts BigCaslon.ttf Big Caslon28 /Library/Fonts Bodoni 72 OS.ttc Bodoni 72 Oldstyle29 /Library/Fonts Bodoni 72 Smallcaps Book.ttf Bodoni 72 Smallcaps30 /Library/Fonts Bodoni 72.ttc Bodoni 7231 /Library/Fonts Bodoni Ornaments.ttf Bodoni Ornaments32 /Library/Fonts Bradley Hand Bold.ttf Bradley Hand33 /Library/Fonts Brush Script.ttf Brush Script MT34 /Library/Fonts Chalkboard.ttc Chalkboard35 /Library/Fonts ChalkboardSE.ttc Chalkboard SE36 /Library/Fonts Chalkduster.ttf Chalkduster37 /Library/Fonts Charter.ttc Charter38 /Library/Fonts Cochin.ttc Cochin39 /Library/Fonts Comic Sans MS Bold.ttf Comic Sans MS40 /Library/Fonts Comic Sans MS.ttf Comic Sans MS41 /Library/Fonts Copperplate.ttc Copperplate42 /Library/Fonts Corsiva.ttc Corsiva Hebrew43 /Library/Fonts Courier New Bold Italic.ttf Courier New44 /Library/Fonts Courier New Bold.ttf Courier New45 /Library/Fonts Courier New Italic.ttf Courier New46 /Library/Fonts Courier New.ttf Courier New47 /Library/Fonts Damascus.ttc Damascus48 /Library/Fonts DecoTypeNaskh.ttc DecoType Naskh49 /Library/Fonts Devanagari Sangam MN.ttc Devanagari Sangam MN50 /Library/Fonts DevanagariMT.ttc Devanagari MT51 /Library/Fonts Didot.ttc Didot52 /Library/Fonts DIN Alternate Bold.ttf DIN Alternate53 /Library/Fonts DIN Condensed Bold.ttf DIN Condensed54 /Library/Fonts Diwan Kufi.ttc Diwan Kufi55 /Library/Fonts Diwan Thuluth.ttf Diwan Thuluth56 /Library/Fonts EuphemiaCAS.ttc Euphemia UCAS57 /Library/Fonts Farah.ttc Farah58 /Library/Fonts Farisi.ttf Farisi59 /Library/Fonts Futura.ttc Futura60 /Library/Fonts Georgia Bold Italic.ttf Georgia61 /Library/Fonts Georgia Bold.ttf Georgia62 /Library/Fonts Georgia Italic.ttf Georgia63 /Library/Fonts Georgia.ttf Georgia64 /Library/Fonts GillSans.ttc Gill Sans65 /Library/Fonts Gujarati Sangam MN.ttc Gujarati Sangam MN66 /Library/Fonts GujaratiMT.ttc Gujarati MT67 /Library/Fonts Gurmukhi MN.ttc Gurmukhi MN68 /Library/Fonts Gurmukhi Sangam MN.ttc Gurmukhi Sangam MN69 /Library/Fonts Gurmukhi.ttf Gurmukhi MT70 /Library/Fonts Herculanum.ttf Herculanum71 /Library/Fonts Hoefler Text Ornaments.ttf Hoefler Text72 /Library/Fonts Hoefler Text.ttc Hoefler Text73 /Library/Fonts Impact.ttf Impact74 /Library/Fonts InaiMathi-MN.ttc InaiMathi75 /Library/Fonts Iowan Old Style.ttc Iowan Old Style76 /Library/Fonts ITFDevanagari.ttc ITF Devanagari77 /Library/Fonts Kailasa.ttc Kailasa78 /Library/Fonts Kannada MN.ttc Kannada MN79 /Library/Fonts Kannada Sangam MN.ttc Kannada Sangam MN80 /Library/Fonts Kefa.ttc Kefa81 /Library/Fonts Khmer MN.ttc Khmer MN82 /Library/Fonts Khmer Sangam MN.ttf Khmer Sangam MN83 /Library/Fonts Kokonor.ttf Kokonor84 /Library/Fonts Krungthep.ttf Krungthep85 /Library/Fonts KufiStandardGK.ttc KufiStandardGK86 /Library/Fonts Lao MN.ttc Lao MN87 /Library/Fonts Lao Sangam MN.ttf Lao Sangam MN88 /Library/Fonts Luminari.ttf Luminari89 /Library/Fonts Malayalam MN.ttc Malayalam MN90 /Library/Fonts Malayalam Sangam MN.ttc Malayalam Sangam MN91 /Library/Fonts Marion.ttc Marion92 /Library/Fonts Microsoft Sans Serif.ttf Microsoft Sans Serif93 /Library/Fonts Mishafi Gold.ttf Mishafi Gold94 /Library/Fonts Mishafi.ttf Mishafi95 /Library/Fonts Mshtakan.ttc Mshtakan96 /Library/Fonts Muna.ttc Muna97 /Library/Fonts Myanmar MN.ttc Myanmar MN98 /Library/Fonts Myanmar Sangam MN.ttc Myanmar Sangam MN99 /Library/Fonts Nadeem.ttc Nadeem100 /Library/Fonts NewPeninimMT.ttc New Peninim MT101 /Library/Fonts NISC18030.ttf GB18030 Bitmap102 /Library/Fonts Oriya MN.ttc Oriya MN103 /Library/Fonts Oriya Sangam MN.ttc Oriya Sangam MN104 /Library/Fonts Papyrus.ttc Papyrus105 /Library/Fonts Phosphate.ttc Phosphate106 /Library/Fonts PlantagenetCherokee.ttf Plantagenet Cherokee107 /Library/Fonts PTMono.ttc PT Mono108 /Library/Fonts PTSans.ttc PT Sans109 /Library/Fonts PTSerif.ttc PT Serif110 /Library/Fonts PTSerifCaption.ttc PT Serif Caption111 /Library/Fonts Raanana.ttc Raanana112 /Library/Fonts Sana.ttc Sana113 /Library/Fonts Sathu.ttf Sathu114 /Library/Fonts Savoye LET.ttc Savoye LET115 /Library/Fonts Seravek.ttc Seravek116 /Library/Fonts Shree714.ttc Shree Devanagari 714117 /Library/Fonts SignPainter.ttc SignPainter-HouseScript118 /Library/Fonts Silom.ttf Silom119 /Library/Fonts Sinhala MN.ttc Sinhala MN120 /Library/Fonts Sinhala Sangam MN.ttc Sinhala Sangam MN121 /Library/Fonts Skia.ttf Skia122 /Library/Fonts SnellRoundhand.ttc Snell Roundhand123 /Library/Fonts Songti.ttc Songti SC124 /Library/Fonts STIXGeneral.otf STIXGeneral125 /Library/Fonts STIXGeneralBol.otf STIXGeneral126 /Library/Fonts STIXGeneralBolIta.otf STIXGeneral127 /Library/Fonts STIXGeneralItalic.otf STIXGeneral128 /Library/Fonts STIXIntDBol.otf STIXIntegralsD129 /Library/Fonts STIXIntDReg.otf STIXIntegralsD130 /Library/Fonts STIXIntSmBol.otf STIXIntegralsSm131 /Library/Fonts STIXIntSmReg.otf STIXIntegralsSm132 /Library/Fonts STIXIntUpBol.otf STIXIntegralsUp133 /Library/Fonts STIXIntUpDBol.otf STIXIntegralsUpD134 /Library/Fonts STIXIntUpDReg.otf STIXIntegralsUpD135 /Library/Fonts STIXIntUpReg.otf STIXIntegralsUp136 /Library/Fonts STIXIntUpSmBol.otf STIXIntegralsUpSm137 /Library/Fonts STIXIntUpSmReg.otf STIXIntegralsUpSm138 /Library/Fonts STIXNonUni.otf STIXNonUnicode139 /Library/Fonts STIXNonUniBol.otf STIXNonUnicode140 /Library/Fonts STIXNonUniBolIta.otf STIXNonUnicode141 /Library/Fonts STIXNonUniIta.otf STIXNonUnicode142 /Library/Fonts STIXSizFiveSymReg.otf STIXSizeFiveSym143 /Library/Fonts STIXSizFourSymBol.otf STIXSizeFourSym144 /Library/Fonts STIXSizFourSymReg.otf STIXSizeFourSym145 /Library/Fonts STIXSizOneSymBol.otf STIXSizeOneSym146 /Library/Fonts STIXSizOneSymReg.otf STIXSizeOneSym147 /Library/Fonts STIXSizThreeSymBol.otf STIXSizeThreeSym148 /Library/Fonts STIXSizThreeSymReg.otf STIXSizeThreeSym149 /Library/Fonts STIXSizTwoSymBol.otf STIXSizeTwoSym150 /Library/Fonts STIXSizTwoSymReg.otf STIXSizeTwoSym151 /Library/Fonts STIXVar.otf STIXVariants152 /Library/Fonts STIXVarBol.otf STIXVariants153 /Library/Fonts SukhumvitSet.ttc Sukhumvit Set154 /Library/Fonts SuperClarendon.ttc Superclarendon155 /Library/Fonts Tahoma Bold.ttf Tahoma156 /Library/Fonts Tahoma.ttf Tahoma157 /Library/Fonts Tamil MN.ttc Tamil MN158 /Library/Fonts Tamil Sangam MN.ttc Tamil Sangam MN159 /Library/Fonts TeamViewer13.otf TeamViewer13160 /Library/Fonts Telugu MN.ttc Telugu MN161 /Library/Fonts Telugu Sangam MN.ttc Telugu Sangam MN162 /Library/Fonts Times New Roman Bold Italic.ttf Times New Roman163 /Library/Fonts Times New Roman Bold.ttf Times New Roman164 /Library/Fonts Times New Roman Italic.ttf Times New Roman165 /Library/Fonts Times New Roman.ttf Times New Roman166 /Library/Fonts Trattatello.ttf Trattatello face version ps_name1 Regular 13.0d2e2 AlNile2 Regular 13.0d2e1 AlTarikh3 Plain 13.0d1e6 AlBayan4 Regular 13.0d1e4 AmericanTypewriter5 Regular Version 2.00x AndaleMono6 Chancery 13.0d1e4 Apple-Chancery7 Regular 13.0d1e3 AppleGothic8 Regular 13.0d1e6 AppleMyungjo9 Regular Version 5.00.1x Arial-Black10 Bold Italic Version 5.00.2x Arial-BoldItalicMT11 Bold Version 5.01.2x Arial-BoldMT12 Italic Version 5.00.2x Arial-ItalicMT13 Bold Italic Version 2.38.1x ArialNarrow-BoldItalic14 Bold Version 2.38.1x ArialNarrow-Bold15 Italic Version 2.38.1x ArialNarrow-Italic16 Regular Version 2.38.1x ArialNarrow17 Regular Version 1.51x ArialRoundedMTBold18 Regular Version 1.01x ArialUnicodeMS19 Regular Version 5.01.2x ArialMT20 Regular 13.0d1e3 Athelas-Regular21 Regular 13.0d1e7 Ayuthaya22 Regular 13.0d1e5 Baghdad23 Regular 13.0d1e5 BanglaMN24 Regular 13.0d1e5 BanglaSangamMN25 Regular 13.0d1e10 Baskerville26 Regular 13.0d1e6 Beirut27 Medium 13.0d1e11 BigCaslon-Medium28 Book 13.0d2e1 BodoniSvtyTwoOSITCTT-Book29 Book 13.0d2e1 BodoniSvtyTwoSCITCTT-Book30 Book 13.0d2e1 BodoniSvtyTwoITCTT-Book31 Regular 13.0d2e1 BodoniOrnamentsITCTT32 Bold 13.0d1e2 BradleyHandITCTT-Bold33 Italic Version 1.52x-1 BrushScriptMT34 Regular 13.0d1e2 Chalkboard35 Light 13.0d1e2 ChalkboardSE-Light36 Regular 13.0d2e1 Chalkduster37 Roman 13.0d1e3 Charter-Roman38 Regular 13.0d2e1 Cochin39 Bold Version 5.00x ComicSansMS-Bold40 Regular Version 5.00x ComicSansMS41 Regular 13.0d1e2 Copperplate42 Regular 13.0d1e4 CorsivaHebrew43 Bold Italic Version 5.00x CourierNewPS-BoldItalicMT44 Bold Version 5.00.2x CourierNewPS-BoldMT45 Italic Version 5.00.1x CourierNewPS-ItalicMT46 Regular Version 5.00.2x CourierNewPSMT47 Regular 13.0d3e9 Damascus48 Regular 13.0d1e4 DecoTypeNaskh49 Regular 13.0d1e2 DevanagariSangamMN50 Regular 13.0d1e3 DevanagariMT51 Regular 13.0d1e3 Didot52 Bold 13.0d1e2 DINAlternate-Bold53 Bold 13.2d1e1 DINCondensed-Bold54 Regular 13.0d2e1 DiwanKufi55 Regular 13.0d1e5 DiwanThuluth56 Regular 13.0d1e2 EuphemiaUCAS57 Regular 13.0d2e3 Farah58 Regular 13.0d1e3 Farisi59 Medium 13.0d1e3 Futura-Medium60 Bold Italic Version 5.00x-4 Georgia-BoldItalic61 Bold Version 5.00x-4 Georgia-Bold62 Italic Version 5.00x-4 Georgia-Italic63 Regular Version 5.00x-4 Georgia64 Regular 13.0d1e4 GillSans65 Regular 13.0d2e1 GujaratiSangamMN66 Regular 13.0d1e2 GujaratiMT67 Regular 13.0d2e1 GurmukhiMN68 Regular 13.0d1e3 GurmukhiSangamMN69 Regular 13.0d1e4 MonotypeGurmukhi70 Regular 13.0d1e2 Herculanum71 Ornaments 13.0d2e20 HoeflerText-Ornaments72 Regular 13.0d2e20 HoeflerText-Regular73 Regular Version 5.00x Impact74 Regular 13.0d1e5 InaiMathi75 Roman 13.0d1e4 IowanOldStyle-Roman76 Book 13.0d3e1 ITFDevanagari-Book77 Regular 13.0d1e3 Kailasa78 Regular 13.0d2e1 KannadaMN79 Regular 13.0d2e1 KannadaSangamMN80 Regular 13.0d1e3 Kefa-Regular81 Regular 13.0d1e3 KhmerMN82 Regular 13.0d1e4 KhmerSangamMN83 Regular 13.0d1e3 Kokonor84 Regular 13.0d2e1 Krungthep85 Regular 13.0d1e11 KufiStandardGK86 Regular 13.0d1e5 LaoMN87 Regular 13.0d1e3 LaoSangamMN88 Regular 13.0d1e2 Luminari-Regular89 Regular 13.0d4e1 MalayalamMN90 Regular 13.0d4e1 MalayalamSangamMN91 Regular 13.0d1e2 Marion-Regular92 Regular Version 5.00.1x MicrosoftSansSerif93 Regular 13.0d2e1 DiwanMishafiGold94 Regular 13.0d2e1 DiwanMishafi95 Regular 13.0d1e3 Mshtakan96 Regular 13.0d1e4 Muna97 Regular 13.0d1e2 MyanmarMN98 Regular 13.0d1e2 MyanmarSangamMN99 Regular 13.0d1e3 Nadeem100 Regular 13.0d1e4 NewPeninimMT101 Regular 13.0d1e1 GB18030Bitmap102 Regular 13.0d3e1 OriyaMN103 Regular 13.0d3e1 OriyaSangamMN104 Condensed 13.0d1e2 Papyrus-Condensed105 Inline 13.0d1e2 Phosphate-Inline106 Regular 13.0d1e3 PlantagenetCherokee107 Bold 13.0d2e4 PTMono-Bold108 Regular 13.0d3e2 PTSans-Regular109 Regular 13.0d2e1 PTSerif-Regular110 Regular 13.0d2e1 PTSerif-Caption111 Regular 13.0d1e3 Raanana112 Regular 13.0d1e4 Sana113 Regular 13.0d1e2 Sathu114 Plain 13.0d2e4 SavoyeLetPlain115 Regular 13.0d3e2 Seravek116 Regular 13.0d2e1 ShreeDev0714117 Regular 13.0d1e5 SignPainter-HouseScript118 Regular 13.0d2e1 Silom119 Regular 13.0d1e2 SinhalaMN120 Regular 13.0d1e2 SinhalaSangamMN121 Regular 13.0d1e54 Skia-Regular122 Regular 13.0d1e1 SnellRoundhand123 Black 13.0d3e2 STSongti-SC-Black124 Regular Version 1.1.0 STIXGeneral-Regular125 Bold Version 1.1.0 STIXGeneral-Bold126 Bold Italic Version 1.1.0 STIXGeneral-BoldItalic127 Italic Version 1.1.0 STIXGeneral-Italic128 Bold Version 1.1.0 STIXIntegralsD-Bold129 Regular Version 1.1.0 STIXIntegralsD-Regular130 Bold Version 1.1.0 STIXIntegralsSm-Bold131 Regular Version 1.1.0 STIXIntegralsSm-Regular132 Bold Version 1.1.0 STIXIntegralsUp-Bold133 Bold Version 1.1.0 STIXIntegralsUpD-Bold134 Regular Version 1.1.0 STIXIntegralsUpD-Regular135 Regular Version 1.1.0 STIXIntegralsUp-Regular136 Bold Version 1.1.0 STIXIntegralsUpSm-Bold137 Regular Version 1.1.0 STIXIntegralsUpSm-Regular138 Regular Version 1.1.0 STIXNonUnicode-Regular139 Bold Version 1.1.0 STIXNonUnicode-Bold140 Bold Italic Version 1.1.0 STIXNonUnicode-BoldItalic141 Italic Version 1.1.0 STIXNonUnicode-Italic142 Regular Version 1.1.0 STIXSizeFiveSym-Regular143 Bold Version 1.1.0 STIXSizeFourSym-Bold144 Regular Version 1.1.0 STIXSizeFourSym-Regular145 Bold Version 1.1.0 STIXSizeOneSym-Bold146 Regular Version 1.1.0 STIXSizeOneSym-Regular147 Bold Version 1.1.0 STIXSizeThreeSym-Bold148 Regular Version 1.1.0 STIXSizeThreeSym-Regular149 Bold Version 1.1.0 STIXSizeTwoSym-Bold150 Regular Version 1.1.0 STIXSizeTwoSym-Regular151 Regular Version 1.1.0 STIXVariants-Regular152 Bold Version 1.1.0 STIXVariants-Bold153 Thin 13.0d1e6 SukhumvitSet-Thin154 Regular 13.0d1e4 Superclarendon-Regular155 Bold Version 5.01.1x Tahoma-Bold156 Regular Version 5.01.2x Tahoma157 Regular 13.0d2e1 TamilMN158 Regular 13.0d2e14 TamilSangamMN159 Medium Version 013.10 TeamViewer13160 Regular 13.0d1e3 TeluguMN161 Regular 13.0d1e5 TeluguSangamMN162 Bold Italic Version 5.00.3x TimesNewRomanPS-BoldItalicMT163 Bold Version 5.01.3x TimesNewRomanPS-BoldMT164 Italic Version 5.00.3x TimesNewRomanPS-ItalicMT165 Regular Version 5.01.3x TimesNewRomanPSMT166 Regular 13.0d2e2 Trattatello [ reached 'max' / getOption("max.print") -- omitted 14 rows ]



quartz(family = "STXihei")#选择中文显示的字体格式STXihei
>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

高级方法


install.packages("Cairo")
#安装Cairo,一定要在网络流畅的情况下安装,否则很容易失败,错误情况查看往期文章的文末 。
library(Cairo)#加载Cairo
CairoPDF("char_1.pdf")#pdf图片保存的文件名称,除了pdf格式外,还可以 保存png,jpeg。
ggplot(data=mydata1,aes(City,Age))+geom_bar(stat="identity",width=0.8,colour="black",size=0.25,fill="pink",alpha=1)#开始绘图之间一定要设置pdf保存文件,否则图片中仍然无法显示中文。
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的便利包装器,推荐使用。


♫. ♪ ~ ♬..♩~ ♫. ♪..♩~ ♫. ♪ ~ ♬..♩..♩~ ♫. ♪ ~ ♬..♩..♩~ ♫. ♪ ~ ♬..♩
♫. ♪ ~ ♬..♩~ ♫. ♪..♩~ ♫. ♪ ~ ♬..♩..♩~ ♫. ♪ ~ ♬..♩..♩~ ♫. ♪ ~ ♬..♩
♫. ♪ ~ ♬..♩~ ♫. ♪..♩~ ♫. ♪ ~ ♬..♩..♩~ ♫. ♪ ~ ♬..♩..♩~ ♫. ♪ ~ ♬..♩
♫. ♪ ~ ♬..♩~ ♫. ♪..♩~ ♫. ♪ ~ ♬..♩..♩~ ♫. ♪ ~ ♬..♩..♩~ ♫. ♪ ~ ♬..♩


我知道你  在看  哦