Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use showtext in R language

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use showtext in R language". In daily operation, I believe many people have doubts about how to use showtext in R language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use showtext in R language". Next, please follow the editor to study!

Use fonts

Pictures can not be displayed in Chinese, the reason is that many R graphics devices can only use some standard fonts, but they often do not contain Chinese characters. While there are Chinese fonts, such as Song typeface and boldface included in Windows, R does not know how to use them. So it's an endless cycle: we have Chinese fonts, R doesn't know how to use them, and we don't like fonts that R can use-so R and useR are not easy to serve.

But now the situation has improved to some extent. We have the sysfonts package, which is specially used to load font files in the system. One of the main functions is font.add ().

Font.add (family, regular,...)

Where family is the name you give to the font, which you will refer to in later drawing commands. Regular is the path to the font file, and if the font is in a standard location on the system (such as Windows's C:\ Windows\ Fonts) or the current working directory, you can enter the file name directly. For example, on a Windows system, the following command imports the italic file in the system and names it "kaishu":

Font.add ("kaishu", "simkai.ttf")

After adding fonts, you can use the font.families () function to view the currently available font names, which should now include four fonts: sans,serif,mono and kaishu. The first three of them are loaded automatically by the sysfonts package, while kaishu is the one we just added.

The loading process of fonts is complete, and the next step is how to use them. Of course, R itself doesn't recognize these fonts, and we need to use the showtext add-on package to really draw with these fonts.

The use of showtext is much simpler, with only two functions currently available: showtext.begin () and showtext.end (). All we need to do is to include the drawing command between these two statements, and then select the font in the appropriate place. Don't say much, just go to the code:

# showtext automatically loads sysfonts package library (showtext); # imports italic font.add ("kaishu", "simkai.ttf"); library (Cairo); # opens graphics device CairoPNG ("chinese-char.png", 600,600); # starts using showtextshowtext.begin (); # series of drawing commands set.seed (123); plot (1, xlim = c (- 3,3), ylim = c (- 3,3), type = "n") Text (runif (100,3,3), runif (100,3,3), intToUtf8 (round (runif (100,100,3)), multiple = TRUE), col = rgb (runif, cex = 2, family = "kaishu"); # specify kaishu font title ("random Chinese character", family = "wqy") # specify wqy font # stop using showtextshowtext.end (); # turn off graphics device dev.off ()

That is, for R to use the font we loaded earlier, simply include the drawing command between a pair of showtext.begin () and showtext.end (), and then select family = in the drawing command. That's it. The wqy in the code is the Wenquanyi micron black font that comes with the showtext package, which can display the vast majority of Chinese characters, so even if there is no Chinese font in your system, you can use it to draw graphics with Chinese.

The above Mini Program will randomly display some Chinese characters in the graph, as shown in the following picture:

Figure 1: random display of Chinese characters

I bet two sugars that you don't know more than half of the Chinese characters above. )

Funny fonts

With the showtext package described above, you can display text in almost any font. At this time, we can do some interesting things: some fonts do not contain letters and numbers, but symbols or icons. For example, in this WM People 1 font, where the letters p and u are the patterns of men and women, respectively, we can draw the following picture:

In fact, this picture is essentially a stacked bar chart, but when drawn in this way, it can directly reflect the number and sex ratio of each category, and the figure itself is already explained, so there is no need to add additional elements such as legends.

The code to draw this picture is:

Link = "http://img.dafont.com/dl/?f=wm_people_1";download.file(link," wmpeople1.zip ", mode =" wb "); unzip (" wmpeople1.zip "); library (showtext); font.add (" wmpeople1 "," wmpeople1.TTF "); library (ggplot2); library (plyr); library (Cairo) Dat = read.csv ('edu,educode,gender,population didn't go to school, 1 edu,educode,gender,population didn't go to school, 1 edu,educode,gender,population didn't go to school, 1 edu,educode,gender,population fmaire 41268 elementary school, 2 edu,educode,gender,population mmae 139378 primary school, 2 mmmmee 154854 early middle school, 3 edu,educode,gender,population mprit 205537 high school, 4 meme mpany 94528 high school, 4 parol fprit 70521 junior college and above, 5meme mprit 57013 junior college and above, 5pencefmprit 50334'); dat$int = round (dat$population / 10000) Gdat = ddply (dat, "educode", function (d) {male = d$int [d$gender = = "m"]; female = d$int [d$gender = = "f"]; data.frame (gender = c (rep ("m", male), rep ("f", female)), x = 1: (male + female);}); gdat$char = ifelse (gdat$gender = "m", "p", "u"); CairoPNG ("edu-stat.png", 600,300) Showtext.begin (); theme_set (theme_grey (base_size = 15)) Ggplot (gdat, aes (x = x, y = educode)) + geom_text (aes (label = char, colour = gender), family = "wmpeople1", size = 8) + scale_x_continuous ("tens of millions") + scale_y_discrete ("Education", labels = unique (dat$ edu [order (dat$educode)]) + scale_colour_hue (guide = FALSE) + ggtitle (2012 demographics) Showtext.end (); dev.off ()

In fact, each villain in the picture is a p or u character, just because they show a different pattern in this font.

Interesting graphics

Further, what if the axes are displayed in different fonts? The result is, of course, by! Play! Bad! Yes!

But it's really appropriate, isn't it?! All kinds of toy guns in primary school! When I was in junior high school, I kept laughing! What a decent teenager in high school! You look like a bear after you go to college, don't you?! (please ignore the author who has left at this moment.)

But this idea is really great, just imagine, if we turn some boring axis description text into a more vivid pattern, then the whole picture will be more expressive? Like this:

Attach the corresponding R code:

Link = "http://img.dafont.com/dl/?f=emoticons";download.file(link," emoticons.zip ", mode =" wb "); unzip (" emoticons.zip "); library (showtext); font.add (" emoticons "," emoticons.ttf "); library (ggplot2); library (Cairo); emotions = c (" W "," s "," C "," A "," p ") Score = c (0.5,0.9,5.5,18.4,74.7); x = factor (emotions, emotions); gdat2 = data.frame (x, score); CairoPNG ("douban.png", 600600); showtext.begin () Ggplot (gdat2, aes (x = x, y = score)) + geom_bar (stat = "identity") + scale_x_discrete (") + scale_y_continuous (" percentage ") + theme (axis.text.x=element_text (size=rel (4), family=" emoticons ") + ggtitle (" Sherlock season 3 "Douban score"); showtext.end (); dev.off () At this point, the study of "how to use showtext in R language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report