A problem often encountered in the COS forum is how Chinese fonts are solved when drawing. Initially, a popular approach is to use
family = "GB1" , but generally this makes the diagram more ugly, and does not completely solve the problem. Later found the Cairo bag, Xi Big Ben, but later found that it was not very clear when choosing the font, that is, I do not know how to select the font I want. Then, Winston Chang wrote the Extrafont package, the function is very perfect, but need to install Ghostscript inside the system. I am lazy, do not want to spend time on the configuration Ghostscript, so wrote a called
showtext package. (as if it were wrong)
showtextDetailed instructions written in my English blog, four-level vocabulary, very understood. Here is a brief explanation of usage, because the usage is quite simple.
The above picture all the code below, of course, in advance to use the four font files in the working directory.
Library (Showtext) library (Cairo) Font.add ("Newrocker","Newrocker.ttf") Font.add ("aerofoil","Aerofoil.ttf") Font.add ("xkcd","Xkcd.ttf") Font.add ("Nextdoor","Thegirlnextdoor.woff") Cairopng ("Showtext.png", 640, 400) Showtext.begin () plot (1, type ="N", family ="xkcd", Cex.axis = 1.5, Cex.lab = 1.5) Text (1, 1.2,"Showtext 0.2", CeX = 5, family ="Nextdoor", Col="#F8766D") Text (1, 0.8,"Has been released!", CeX = 5, family ="Newrocker", Col="#00BFC4") title ("Free to use fonts", family ="aerofoil", Cex.main = 2) Showtext.end () Dev.off ()
In fact, there are two steps: First
font.add() , load the font, and then use
showtext.end() to include the drawing statements.
font.add()The first parameter is the series name (family) of the font at the time of drawing, which is how to tell R how to reference the font when drawing. The second parameter is the path to the font, and if the font is directly in the working directory, the font name is hit. Of course, the operating system typically has some default font installation locations, which,
showtext in fact, are the sysfonts packages it relies on to try to search for these paths. Under Windows, enter directly
You can load the song body.
After the fonts are loaded, R itself does not know how to use them, so the drawing statements need to be contained in a pair and in the
showtext.end() middle. You can use these fonts just by specifying the appropriate
showtextWith the Wen Quan Shu of the black font, if you just want to use Chinese in the diagram, only need to load
showtext , and then use
showtext only the currently open graphics device is valid, and if no graphics device is currently present or the device is turned off,
showtext it will not work.
showtextDevelopment page in Https://github.com/yixuan/showtext, welcome to there to shoot bricks catch worms. In this message can also, Xie.
The Showtext package was updated to version 0.4-1 the previous two days. Because this update is more content, so simply write a blog introduction.
The first major change is that it can now be used automatically in graphics devices
showtext , and is easier to use than ever before. Before this version, you need to turn on the graphics device every time, call
showtext.begin() and and
showtext.end() then turn off the graphics device. It is cumbersome to call functions repeatedly. In the new version,
showtext.auto() all graphics devices are automatically used for rendering as soon as they are called at the very beginning
showtext . This is especially important for window devices, as we usually use R to draw, rarely manually
x11() , but rather directly using
plot() functions to let r automatically open a window. After the call
showtext.auto() , the window device can also automatically use the loaded font, which is useful for previewing the graphics effect.
Another benefit of automatic invocation is that it can be
ggsave() used in
showtext . It
ggsave() automatically turns on and off the graphics device, so we can't insert it in the previous version
showtext.begin() . Now
ggsave() you can also use
showtext the font library after automation.
To turn off the auto-Invoke feature, simply execute
A second notable improvement is the better support for bitmap graphics. In previous versions, it was
showtext used primarily for vector diagrams, such as
svg() . If
png() you use it in or
showtext , you will find that the font you draw is very ugly. This is because those bitmap devices do not have good anti-aliasing support. Now,
showtext you can draw a smooth glyph for those devices, so the image quality will be improved as well. The only thing to note is to keep the DPI of the graphics device
showtext consistent, such as when using the
png() device, you should manually set the desired resolution (here DPI is 120)
Support for bitmap graphics also means that Windows devices, such as and, are supported
The last improvement is actually the previous
sysfonts package update content, is currently not directly connected to Google's font library in China, so
font.add.google() when used, you can use 360 of the agents provided. The use method is to add a parameter, such as
Library (Showtext) font.add.google ("GoChi Hand""GoChi" "useso")
Other examples and instructions can be found on Github.
Transferred from: http://yixuan.cos.name/cn/2015/04/showtext-new-version/
R Drawing Font solution (GO)