转换英文数字为ggplot的波斯语 [英] Convert English numbers to Persian for ggplot

查看:56
本文介绍了转换英文数字为ggplot的波斯语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ggplot 进行数据可视化项目.我有英文的原始数据:

I am working on a data visualization project using ggplot. I have my original data in English:

world_ecommerce <- data.frame(
    year = factor(c(2014, 2015, 2016, 2017, 2018)),
    score = c(1336, 1548, 1845, 2304, 2842)
)

我想将其可视化为条形图,并以波斯语显示所有数字.我原来的条形图是:

I want to visualize it as a bar chart and show all numbers in persian. My original Bar chart is:

ggplot(
    world_ecommerce, 
    aes(x = year, y = score, label = score), 
    fill = "#56c7da"
) +
    geom_bar(
        stat = "identity",
        fill = "#56c7da",
        position = position_dodge(),
        width = 0.5,
        size = 0
    ) +
    geom_text(
        aes(label = score),
        vjust = -1.5,
        color = "#555555",
        position = position_dodge(width = 0.5),
        size = 3.5
    ) +
    scale_y_continuous(
        expand = c(0,0),
        limits = c(0, 3150),
        breaks = c(
            500, 1000, 1500, 2000, 2500, 3000
        )
    ) +
    xlab("سال") +
    ylab("") +
    ggtitle("میلیارد دلار") +
    labs(
        subtitle = "اندازه بازار خرده فروشی آنلاین در دنیا",
        caption = "منبع: سایت تحلیلی-پژوهشی Statista"
    )

我需要geom_text标签和轴号使用波斯语(例如, ۲۰۱۵ ,而不是 2015 ).

I need the geom_text labels and axis numbers to be in Persian (e.g., ۲۰۱۵ instead of 2015).

推荐答案

首先,我们需要编写一个函数,以将英语数字中的字符翻译转换为波斯语:

First, we need to write a function to translate characters from English numbers to Persian:

to_fa_numbers <- 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))
}

然后,我们可以轻松地将其应用于ggplot几何图形和图层:

Then, we can easily apply it to our ggplot geoms and layers:

ggplot(
    world_ecommerce, 
    aes(x = year, y = score, label = score), 
    fill = "#56c7da"
    ) +
    geom_bar(
        stat = "identity",
        fill = "#56c7da",
        position = position_dodge(),
        width = 0.5,
        size = 0
    ) +
    geom_text(
        aes(label = to_fa_numbers(score)),
        vjust = -1.5,
        color = "#555555",
        position = position_dodge(width = 0.5),
        size = 3.5
    ) +
    scale_x_discrete(
        labels = to_fa_numbers
    ) +
    scale_y_continuous(
        expand = c(0,0),
        limits = c(0, 3150),
        breaks = c(
            500, 1000, 1500, 2000, 2500, 3000
        ),
        labels = to_fa_numbers
    ) +
    xlab("سال") +
    ylab("") +
    ggtitle("میلیارد دلار") +
    labs(
        subtitle = "اندازه بازار خرده فروشی آنلاین در دنیا",
        caption = "منبع: سایت تحلیلی-پژوهشی Statista"
    )

这篇关于转换英文数字为ggplot的波斯语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆