我正在使用ggplot2.
图中获得的所有数字(包括 x 轴和 y 轴以及图中的数字)均为英文格式,如下图所示:
但我希望所有图中的数字都是波斯语(例如,???? 而不是 2015)。
我有很多不同数字的地块。谁能帮我将图中的英文数字转换为波斯语?
我发现你在另一个问题中写的这个函数:
\nconvert_english_to_farsi <- function(x) {\n persian <- "\\u0660\\u0661\\u0662\\u0663\\u0664\\u0665\\u0666\\u0667\\u0668\\u0669\\u06F0\\u06F1\\u06F2\\u06F3\\u06F4\\u06F5\\u06F6\\u06F7\\u06F8\\u06F9"\n english <- "\\U0030\\U0031\\U0032\\U0033\\U0034\\U0035\\U0036\\U0037\\U0038\\U0039\\U0030\\U0031\\U0032\\U0033\\U0034\\U0035\\U0036\\U0037\\U0038\\U0039"\n return(chartr(english, persian, x))\n}\nRun Code Online (Sandbox Code Playgroud)\n这实际上对我来说非常有效(我在这里检查过):
\nconvert_english_to_farsi(123456)\n#> [1] "\xdb\xb1\xdb\xb2\xdb\xb3\xdb\xb4\xdb\xb5\xdb\xb6"\nRun Code Online (Sandbox Code Playgroud)\n您可以将此函数用作labels中大多数scale_*函数的参数ggplot2。例如,如果我们想要更改某些图的 y 轴标签(economics包含在其中,ggplot2因此这是可重现的):
library(ggplot2)\nggplot(economics, aes(x = date, y = unemploy)) +\n geom_line() +\n scale_y_continuous(labels = convert_english_to_farsi)\nRun Code Online (Sandbox Code Playgroud)\n这里的最高数字应该是 12,000,转换为 \xdb\xb1\xdb\xb2\xdb\xb0\xdb\xb0\xdb\xb0,看起来是正确的。
\n
由reprex 包于 2021-02-07 创建(v1.0.0)
\n或者,您可以使用使用波斯数字的不同字体。我在这里找到了一个。在 Windows 中下载并安装它。然后使用包加载它extrafont:
convert_english_to_farsi(123456)\n#> [1] "\xdb\xb1\xdb\xb2\xdb\xb3\xdb\xb4\xdb\xb5\xdb\xb6"\nRun Code Online (Sandbox Code Playgroud)\n您可以通过以下方式检查可用字体:
\nlibrary(ggplot2)\nggplot(economics, aes(x = date, y = unemploy)) +\n geom_line() +\n scale_y_continuous(labels = convert_english_to_farsi)\nRun Code Online (Sandbox Code Playgroud)\n现在更改主题中的字体,ggplot2如下所示:
ggplot(economics, aes(x = date, y = unemploy)) +\n geom_line() +\n theme_minimal(base_family = "Persian Pager Number")\nRun Code Online (Sandbox Code Playgroud)\n