首页 > 解决方案 > 将宏添加到刻面标签中的字母

问题描述

正如标题所说,我想在分面标签中添加一个宏。一个例子:

library(tidyverse)

# subset data
df2 <- diamonds %>%
  sample_n(500)

# plot
ggplot(df2,aes(x = carat, y = price)) +
  geom_point() +
  facet_wrap(~cut)

在此处输入图像描述

现在我想在 Fair 的 a 上添加一个宏

# attempt to recode Fair to Fāir
df2 <- df2 %>%
  mutate(cut2 = fct_recode(cut, "F\u0101ir" = "Fair"))

# doesn't work - produces exactly the same plot as above.
ggplot(df2,aes(x = carat, y = price)) +
  geom_point() +
  facet_wrap(~cut2)

任何提示将非常感谢。

标签: rggplot2plottext

解决方案


看起来这是一个问题,fct_recode而不是ggplot2. 这似乎工作得很好

df2 <- diamonds %>%
  sample_n(500)
df2$cut2 <- df2$cut
levels(df2$cut2)[1] <- "F\u0101ir"

ggplot(df2,aes(x = carat, y = price)) +
  geom_point() +
  facet_wrap(~cut2)

带有刻面和长音的绘图

实际上我猜它与 R 中的所有参数名称有关。看起来你不能使用 unicode 名称(至少在我测试的 4.0.5 中不能)

foo <- function(...) {
  print(match.call())
}
foo("F\u0101ir" = 1)
# foo(Fair = 1)
foo(Fāir = 1)
# foo(Fair = 1)

似乎这些值只是转换为 ASCII


推荐阅读