如何在ggplot2图例标签中添加Latex代码? [英] How to add Latex code in ggplot2 legend labels?
本文介绍了如何在ggplot2图例标签中添加Latex代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请考虑以下示例:
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图例标签中添加Latex代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文