将轴文本方向更改为从右到左

Adi*_*rid 6 r arabic hebrew ggplot2 right-to-left

在从右到左的语言(例如阿拉伯语和希伯来语)中,如何调整ggplot2文本元素的文本方向?请注意,我不是在谈论对齐(由 控制hjust,而是direction: rtl;在呈现文本的实际方向(相当于 CSS )。因此,这不是这个问题的复制。

这是一个可重现的最小示例:

library(ggplot2)
library(tibble)

example1 <- tribble(
  ~item,
  "??? ???? ????"
)

# or as ordinary data frame, to avoid 'tibble' dependency
example1 <- data.frame(item = "??? ???? ????")

ggplot(example1, aes(item)) + 
  geom_bar() + 
  theme(axis.text.x = element_text(size = 25))
Run Code Online (Sandbox Code Playgroud)

我放大了轴文本 x 来说明我的意思。代码生成以下图表,注意问号在文本的右侧,我希望它出现在文本的左侧。在 tibble 中没example1问题(即使它看起来“相反”,但问号结束了句子。)

改变 element_text 的方向

Hen*_*rik 3

您可以使用“RIGHT-TO-LEFT EMBEDDING”(“将以下文本视为从右到左嵌入”)的 Unicode 控制字符:u202B。请参阅显式定向嵌入

example1$item <- paste("\u202B", example1$item)
ggplot(example1, aes(item)) + 
   geom_bar() +
   theme(axis.text.x = element_text(size = 25))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述