BB.squared
BB.squared

Reputation: 113

Caption with multiple text and color

I am adding a caption to my plot in ggplot, with no problem. I can change the positions and single color and text.

What I would like to do is add a caption that has multiple texts and each text(word) is a separate color.

library(ggplot2)

ggplot(xdata, aes(x = Days, y= maxGenOutletTemp)) + 
geom_point(shape = 18, color = "#8181F7")+
geom_hline(yintercept = `1yrWTGbase`$Gen.Cool.1YRBase, linetype="solid", color = "#04B431", size = 1)+
geom_smooth(method = lm, linetype = "dashed", color = "red", fullrange = TRUE)+
geom_smooth(method = "auto",se = F)+

scale_x_datetime(date_breaks = "1 week", date_minor_breaks = "1 day",
           date_labels = "%m/%d")+ 
scale_y_continuous(limits=c(min(xdata$maxGenOutletTemp), 85))+

theme_light()+

labs(title = "WTG Generator Coolant.Outlet last 90 days  vs  1yr Wtg.Baseline",
   subtitle = "Last 90 Days",
   caption = "Green.Line -> 1yr WTG.baseline  //  Red.Line -> Trend line  //  Blue.Line -> Daily Moving Trend")+

theme(
plot.title = element_text(hjust = 0.5, size = 14),    # Center title position and size
plot.subtitle = element_text(hjust = 0.5),            # Center subtitle
plot.caption = element_text(hjust = 0, face = "bold"), # move caption to the left
)

I would like to have a caption that reads like:

1yr WTG.baseline(green text), Trend line(red text), Daily Moving Trend(blue text)

enter image description here

Upvotes: 1

Views: 1700

Answers (1)

Stéphane Laurent
Stéphane Laurent

Reputation: 84529

It is possible with the ggtext package (which is currently available on Github only, I think). It allows to use HTML for the caption.

library(ggplot2)
library(ggtext)

mycaption <- '<span style="color:red;">RED</span> // <span style="color:blue;">BLUE</span>'

ggplot(iris) + 
  aes(Sepal.Length, Sepal.Width) + 
  labs(caption = mycaption) + 
  theme(plot.caption = element_markdown(hjust = 0, size = 18))

enter image description here

Upvotes: 2

Related Questions