2017-06-01 109 views
0

請看下面的例子:如何在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」,以乳膠配方的傳說,說 「$ A^H_ {TK} $」 和「$ B^h_ {tk} $「。

顯然,根據回答here,實現這一點的方法存在。但是,我真的很努力去實現它的工作。有人可以爲我分解嗎?

回答

1


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)))