将英语数字转换为波斯语以用于 ggplot
Convert English numbers to Persian for ggplot
我正在使用 ggplot
进行数据可视化项目。我有英文的原始数据:
world_ecommerce <- data.frame(
year = factor(c(2014, 2015, 2016, 2017, 2018)),
score = c(1336, 1548, 1845, 2304, 2842)
)
我想将其可视化为条形图并以波斯语显示所有数字。我原来的条形图是:
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 而不是 2015)。
首先,我们需要编写一个函数来将个字符从英语数字翻译成波斯语:
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 geoms 和图层中:
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
进行数据可视化项目。我有英文的原始数据:
world_ecommerce <- data.frame(
year = factor(c(2014, 2015, 2016, 2017, 2018)),
score = c(1336, 1548, 1845, 2304, 2842)
)
我想将其可视化为条形图并以波斯语显示所有数字。我原来的条形图是:
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 而不是 2015)。
首先,我们需要编写一个函数来将个字符从英语数字翻译成波斯语:
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 geoms 和图层中:
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"
)