2

Let's say that i have the following data.

Data<-structure(list(Date = structure(c(14821, 14821, 14822, 14822, 
                                    14823, 14823, 14824, 14824, 14825, 14825, 14826, 14826, 14827, 
                                    14827, 14828, 14828, 14829, 14829, 14830, 14830), class = "Date"), 
                 name = c("TSIN", "TSIP", "TSIN", "TSIP", "TSIN", "TSIP", 
                          "TSIN", "TSIP", "TSIN", "TSIP", "TSIN", "TSIP", "TSIN", "TSIP", 
                          "TSIN", "TSIP", "TSIN", "TSIP", "TSIN", "TSIP"), value = c(50.4037680727913, 
                                                                                     51.3364809806877, 50.4340417810677, 51.3504773174762, 50.6603181623312, 
                                                                                     51.3329488285268, 50.5416074633734, 51.2091857253563, 50.5690544870539, 
                                                                                     51.2348346742767, 51.1758823740724, 51.9726318303194, 51.0492677724948, 
                                                                                     51.9345778234068, 50.9432096226738, 51.5709622418077, 51.1861995444741, 
                                                                                     51.052286506949, 50.8054997798819, 50.9526010710907)), row.names = c(NA, 
                                                                                                                                                          -20L), class = c("tbl_df", "tbl", "data.frame"))

I am using scale_color_manual and scale_linetype_manual with my final plot. I wanted to include text in LaTex format in my labels. So I used the Tex command that transform the labels in LaTex format. But I am having a problem because the use of Tex command generate duplicates labels.

enter image description here

I would like to have two time series and two labels. The code that I am using is the following:

library(latex2exp)
library(tidyverse)

ggplot(Data, aes(x = Date, y=value, colour = name, linetype = name)) +
  geom_line() + 
  theme_test() +
  scale_color_manual(values = c("burlywood", "blue3"), 
                     labels = unname(TeX(c("MSHAR-ReCov$^{+}$", 
                                           "MSHAR-ReCov$^{-}$")))
  ) +
  scale_linetype_manual(values = 1:2, 
                        labels = unname(TeX(c("MSHAR-ReCov$^{+}$", 
                                              "MSHAR-ReCov$^{-}$")))
  ) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1), 
        legend.position = "bottom",
        legend.title = element_blank())+
  labs(x = "", y="")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Eve Chanatasig
  • 397
  • 2
  • 10
  • 2
    You could create a vector of labels `labels <- unname(c(TeX("MSHAR-ReCov$^{+}$"), TeX("MSHAR-ReCov$^{-}$")))` and pass this vector to the `labels` argument of both scales. See https://stackoverflow.com/questions/62594966/replace-legend-labels-texts-with-greek-i-e-latex-symbols/62595497#62595497 helps to answer your questions. – stefan Nov 08 '21 at 18:47
  • 1
    Does this answer your question? [Replace legend labels/texts with Greek (i.e. LaTeX) symbols](https://stackoverflow.com/questions/62594966/replace-legend-labels-texts-with-greek-i-e-latex-symbols) – tjebo Nov 08 '21 at 23:32

0 Answers0