【问题标题】:ggplot: legend label with LaTex expression + variable value + normal textggplot:带有 LaTex 表达式 + 变量值 + 普通文本的图例标签
【发布时间】:2020-06-08 14:01:33
【问题描述】:

我想用 LaTex 表达式、变量值和普通文本的组合来标记 ggplot,例如

l_decile_names[vec_deciles[1]], " decile (", vec_items[item_current], ")"

在哪里

  • l_decile_names 包含乳胶:"1^{st}", ... "10^{th}"
  • vec_items 包含字符串 "item A", "item B", "item C", ...

我可以让它适用于情节标题,但不适用于标签。 目的是绘制图下方标签的图例,其内容左对齐。

我用bquote()expr()TeX()尝试了各种方法,例如

labels = c("all subjects", 
         TeX(paste0(l_decile_names[vec_deciles[1]], " decile (", vec_items[item_current], ")")),
         TeX(paste0(l_decile_names[vec_deciles[2]], " decile (", vec_items[item_current], ")")))

...但还没有成功。 MWE 在下面提供,非常感谢您的帮助!

library(ggplot2)
library(latex2exp)

vec_deciles <- c(
  1,
  2
  # ...
)
l_decile_names <- list(
  '1stDecile' = '1^{st}',
  # ...
  '10thDecile' = '10^{th}'
)

# survey_items
vec_items <- c(
  "item A",
  "item B",
  "item C"
  # ...
)
item_current <- 3

vec_deciles_label <- paste(l_decile_names[vec_deciles[1]], "and", l_decile_names[vec_deciles[2]])

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_boxplot() +
  labs(
    title=TeX((paste0("Scores of ", vec_deciles_label, " decile in ", vec_items[item_current]))),
    x="",
    y="Score"
  ) +
  scale_color_discrete(name = "Scores for ",
    labels = c("all subjects", 
               "1^{st} decile (item C)",
               "4^{th} decile (item C)"

    )) +
  theme(
        legend.box="vertical",
        legend.position="bottom"
  ) +
  guides(colour=guide_legend(ncol=1, nrow=3, byrow=TRUE))

【问题讨论】:

  • 必须是 LaTex 还是您会接受任何可行的方法?
  • 好吧,LaTex 会很好,这样也可以涵盖比上面示例更复杂的情况。但是,如果您想到的解决方案在 HTML 和 PDF 输出中都能很好地工作,那显然是一个优势!
  • 你尝试过使用 scales::label_ordinal 吗?
  • scales::label_ordinal 可能允许将1^{st}", ... "10^{th} 添加到标签中,如本例所示。但是对于更复杂的 LaTex 表达式,您会推荐什么?
  • 试试labels = unname(c(...))。为我工作。见stackoverflow.com/questions/44310088/…的第二个回答

标签: r ggplot2


【解决方案1】:

答案基于@Stefan 的评论,他提到了this SO post
图例中的文本对齐可以按照@konvas 推荐的here 实现。

# ... see question above

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_boxplot() +
  labs(
    title=TeX((paste0("Scores of ", vec_deciles_label, " decile in ", vec_items[item_current]))),
    x="",
    y="Score"
  ) +
  scale_color_discrete(name = "Scores for ",
                       labels =  
                         unname(TeX( # <== to be able to have label with LaTex expression + variable value + normal text
                           c(
                             "all subjects", 
                             paste0(l_decile_names[vec_deciles[1]], " decile (", "$\\Rightarrow \\ldots$ ", vec_items[item_current], ")"),
                             paste0(l_decile_names[vec_deciles[2]], " decile (", "$\\Rightarrow \\ldots$ ", vec_items[item_current], ")")
                           )
                         ))
  ) +
  theme(
    legend.box="vertical",
    legend.position="bottom",
    legend.text.align = 0 # <== to left-align the legend text
  ) +
  guides(colour=guide_legend(ncol=1, nrow=3, byrow=TRUE))

此外,我遇到了以下限制:

  1. 并非所有的 LaTex 表达式都被接受, 例如"$\\Rightarrow$" 有效,而 "$\\Longrightarrow$" 无效;
  2. 一些 LaTex 表达式需要 $ 符号,其他不需要, 例如"$\\Rightarrow$" 会,而"1^{st}" 不会;
  3. 一些 LaTex 表达式可以在 R/Rmd 输出中工作,但不能在“更复杂的 Rmds”中工作,例如 thesisdown/huskydown

会话信息: MacOs 10.13.6、R 3.6.3、ggplot2_3.3.1、latex2exp_0.4.0

【讨论】:

    猜你喜欢
    • 2013-12-30
    • 2021-08-03
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多