如何在 ggplot2 图例标签中添加乳胶代码? [英] How to add Latex code in ggplot2 legend labels?

查看:18
本文介绍了如何在 ggplot2 图例标签中添加乳胶代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下示例:

p <- ggplot(data = data.frame(A=c(1,2,3,4,5,6,7,8),B=c(4,1,2,1,3,2,4,1),C=c("A","B","A","B","A","B","A","B")))
p <- p + geom_line(aes(x = A, y = B,color = C))

我想将图例中的标签从A"和B"更改为 Latex 公式,分别说$A^h_{tk}$"和$B^h_{tk}$".

I would like to change the labels in the legend from "A" and "B" to Latex formulae, say "$A^h_{t-k}$" and "$B^h_{t-k}$", respectively.

显然,根据此处的答案,存在实现此目的的方法.但是,我真的很难让它发挥作用.谁能帮我分析一下?

Apparently, according to the answers here, ways to achieve this exist. However, I am really struggling to get it to work. Could somebody break it down for me?

推荐答案


library(ggplot2)
df <- data.frame(A = c(1,2,3,4,5,6,7,8),
                 B = c(4,1,2,1,3,2,4,1),
                 C = c("A","B","A","B","A","B","A","B")
                 )
ggplot(df) + 
    geom_line(aes(x = A, y = B,color = C)) +
    scale_color_discrete(labels = c(expression(A[t-k]^h), expression(B[t-k]^h)))

这篇关于如何在 ggplot2 图例标签中添加乳胶代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆