转换英文数字为ggplot的波斯数字 [英] Convert English numbers to Persian numbers for ggplot
问题描述
我正在使用 ggplot2
进行数据可视化项目.
在图中获得的所有数字(包括x轴和y轴以及图中的数字)都是英语格式,如下图所示:
但是我希望所有图中的数字都是波斯语(例如,۲۰۱۵代替2015).
我有许多不同数字的地块.有人可以帮我将剧情中的英文数字转换为波斯语吗?
方法1-替换数字
我发现了您在另一个问题中编写的此函数:
convert_english_to_farsi<-function(x){波斯语--" \ u0660 \ u0661 \ u0662 \ u0663 \ u0664 \ u0665 \ u0666 \ u0667 \ u0668 \ u0669 \ u06F0 \ u06F1 \ u06F2 \ u06F3 \ u06F4 \ u06F5 \ u06F6 \ u06F7 \ u06F8 \英文<-" \ U0030 \ U0031 \ U0032 \ U0033 \ U0034 \ U0035 \ U0036 \ U0037 \ U0038 \ U0039 \ U0030 \ U0031 \ U0032 \ U0033 \ U0034 \ U0035 \ U0036 \ U0037 \ U0037 \ U0038 \ U0039return(chartr(英语,波斯语,x))}
这实际上对我来说效果很好(我在(v1.0.0)创建于2021-02-07 sup>
方法2-替换字体
或者,您可以使用其他使用波斯数字的字体.我在此处找到了一个.在Windows中下载并安装它.然后使用 extrafont
包加载它:
库(附加字体)#>用R注册字体font_import(path =〜/Fonts",#我仅将下载的ttf文件放在此处,因此仅导入了相关的字体递归=假)
您可以使用以下方法检查可用字体:
fonts()#>[1]波斯寻呼机号码";
现在像这样更改 ggplot2
主题中的字体:
ggplot(经济学,aes(x =日期,y =失业))+geom_line()+theme_minimal(base_family =波斯寻呼机号码")
I am working on a data visualization project using ggplot2
.
All numbers obtained in the plot (Includes axis-x and axis-y and numbers inside the plot) are in English format like the below plot:
but I want the numbers in all plots to be Persian (e.g., ۲۰۱۵ instead of 2015).
I have many plots with different numbers. Can anyone help me to convert English numbers in the plot to Persian?
Approach 1 - replacing numbers
I found this function you wrote in the other question:
convert_english_to_farsi <- function(x) {
persian <- "\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669\u06F0\u06F1\u06F2\u06F3\u06F4\u06F5\u06F6\u06F7\u06F8\u06F9"
english <- "\U0030\U0031\U0032\U0033\U0034\U0035\U0036\U0037\U0038\U0039\U0030\U0031\U0032\U0033\U0034\U0035\U0036\U0037\U0038\U0039"
return(chartr(english, persian, x))
}
This actually works quite well for me (I checked here):
convert_english_to_farsi(123456)
#> [1] "۱۲۳۴۵۶"
You can just use this function as the labels
argument in most scale_*
functions in ggplot2
. For example if we want to change the y-axis labels of some plot (economics
is included in ggplot2
so this is reproducible):
library(ggplot2)
ggplot(economics, aes(x = date, y = unemploy)) +
geom_line() +
scale_y_continuous(labels = convert_english_to_farsi)
The highest number here is supposed to be 12,000 which translates to ۱۲۰۰۰, which looks correct.
Created on 2021-02-07 by the reprex package (v1.0.0)
Approach 2 - replacing the font
Alternativly, you can use a different font that uses Persian numbers. I found one here. Download and install it in Windows. Then load it using the extrafont
package:
library(extrafont)
#> Registering fonts with R
font_import(
path = "~/Fonts", # I placed only the downloaded ttf file here so only the relevant font is imported
recursive = FALSE
)
You can check available fonts with:
fonts()
#> [1] "Persian Pager Number"
Now change the font in your ggplot2
theme like this:
ggplot(economics, aes(x = date, y = unemploy)) +
geom_line() +
theme_minimal(base_family = "Persian Pager Number")
这篇关于转换英文数字为ggplot的波斯数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!