修改 ggplot 轴标签以包含希腊字符和下标

问题描述 投票:0回答:1

我想修改 ggplot 的 y 轴标签以包含希腊字母和下标,但我的代码不起作用。

示例:

set.seed(42)
data.frame(x = rnorm(300, 50, 5),
           g = sample(c("B1", "B2", "B3"), 300, replace = TRUE)) %>% 
  ggplot(aes(x = x, y = g)) +
  geom_violin() +
  scale_y_discrete(labels = labeller(c(B1 = expression("\u0392"[1]),
                                       B2 = expression("\u0392"[2]),
                                       B3 = expression("\u0392"[3]))))
Error in `$<-.data.frame`(`*tmp*`, ".label", value = list()) : 
  replacement has 0 rows, data has 3
r ggplot2
1个回答
0
投票

我不确定您想要的输出是什么。我用 google 搜索了这个 unicode,只发现它是 B,所以我只是将 1-2-3 粘贴到每个 unicode 中。有帮助吗?

labs <- paste(rep(expression("\u0392"),3),1:3)
set.seed(42)
data.frame(x = rnorm(300, 50, 5),
           g = sample(c("B1", "B2", "B3"), 300, replace = TRUE)) %>% 
  ggplot(aes(x = x, y = g)) +
  geom_violin() +
  scale_y_discrete(labels = labs)

© www.soinside.com 2019 - 2024. All rights reserved.