考虑以下示例:
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))
我想更改传奇中的标签来自&#34; A&#34;和&#34; B&#34;乳胶配方,说&#34; $ A ^ h_ {t-k} $&#34;和&#34; $ B ^ h_ {t-k} $&#34;分别。
显然,根据答案here,存在实现这一目标的方法。但是,我真的很难让它发挥作用。有人可以为我分解吗?
答案 0 :(得分:6)
要使用真正的LaTeX语法,您可以使用latex2exp
包。请注意使用unname()
,这是必要的。
library(ggplot2)
library(latex2exp)
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 = unname(TeX(c("$A_{t-k}^h", "$B_{t-k}^h"))))
由reprex package(v0.2.0)创建于2018-05-29。
答案 1 :(得分:4)
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)))