YouTip LogoYouTip

R Charts Cn

Font library directory on different systems: * Linux is generally under **/usr/share/fonts**, we can use the fc-list command to view: # fc-list/usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf: DejaVu Serif:style=Bold/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf: DejaVu Sans Mono:style=Book/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf: DejaVu Sans:style=Book/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf: DejaVu Sans:style=Bold/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf: DejaVu Sans Mono:style=Bold/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf: DejaVu Serif:style=Book * Windows fonts are under **C:WindowsFonts**, you can directly open to see them. * mac OS fonts are in the **/System/Library/Fonts** and **/Library/Fonts** directories. The font library supported by the system can be viewed by installing showtext: > install.packages("showtext", repos = "https://mirrors.ustc.edu.cn/CRAN/") # Install showtext...> font_files() # View font path file family face version 1 /Library/Fonts Arial Unicode.ttf Arial Unicode MS Regular Version 1.01x ps_name 1 ArialUnicodeMS Seeing ArialUnicodeMS is available, we can use it: pie3D(info,labels = names,explode = 0.1, main = "3D Chart",family = "ArialUnicodeMS") ### Load Custom Fonts Sometimes the system's font library doesn't support well, the showtext() function can load our custom fonts, you can download the font package ttf, then use the font_add() function to add. Here we use Source Han Sans, Source Han Sans is an open-source font launched by Adobe and Google. Official website: [https://source.typekit.com/source-han-serif/cn/](https://source.typekit.com/source-han-serif/cn/) GitHub address: [https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese](https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese) After opening the link, just select one: !(#) You can also download from cloud disk: [https://pan.baidu.com/s/14cRhgYvvYotVIFkRVd71fQ](https://pan.baidu.com/s/14cRhgYvvYotVIFkRVd71fQ) . You can download an OTF font, such as SourceHanSansSC-Bold.otf, put the file in the currently executing code file: Bar chart using font library: ## Example # Load showtext library(showtext); # The first parameter sets the font name, the second parameter is the font library path, in the same directory, we just write the font library name font_add("SyHei", "SourceHanSansSC-Bold.otf"); # Set the file name, output as png png(file="tutorial-bar-cn.png") cvd19 =c(83534,2640626,585493) #Load font showtext_begin(); barplot(cvd19, main="COVID-19 Bar Chart", col=c("#ED1C24","#22B14C","#FFC90E"), names.arg=c("China","USA","India"), family='SyHei'# Set font library ) # Remove font showtext_end(); !(#) 3D pie chart using Chinese: ## Example library(plotrix) library(showtext); # The first parameter sets the font name, the second parameter is the font library path, in the same directory, we just write the font library name font_add("SyHei", "SourceHanSansSC-Bold.otf"); # Data preparation info =c(1, 2, 4, 8) # Naming names=c("Google", "Tutorial", "Taobao", "Weibo") # Coloring (optional) cols =c("#ED1C24","#22B14C","#FFC90E","#3f48CC") # Set the file name, output as png png(file="3d_pie_chart.png") #Load font showtext_begin(); # Draw 3D chart pie3D(info,labels=names,explode =0.1, main ="I test SyHei font",family="SyHei") # Remove font showtext_end(); # Close graphics device dev.off(); !(#)
← R Scatterplots ChartsR Pie Charts β†’