2

I'd like to change the color of the text in my title for a grid.arrange plot.

Given data like this:

X<-c(1,2,3,4,5)
Y<-c(2,3,6,7,8)
Y2<-c(5,6,7,8,2)
DF<-data.frame(X,Y,Y2)

g1<-ggplot(data=DF, aes(x=X,y=Y)) + geom_line(color='black')
g2<-ggplot(data=DF,aes(x=X,y=Y2)) + geom_line(color="black")

grid.arrange(arrangeGrob(g1,g2,ncol=2,top="My Title"))

How can I change the text color of "My Title" to a color other than black?

eipi10
  • 91,525
  • 24
  • 209
  • 285
Vinterwoo
  • 3,843
  • 6
  • 36
  • 55

1 Answers1

2

To change the color you can use the textGrob function from the grid package and use the gpar argument to specify the color:

top=textGrob("My Title", gp=gpar(col="blue"))

See the gpar help for a list of other options such as font size, font face, etc. that can be adjusted in the same way.

I thought this question must be a duplicate, but I haven't found a question that addresses changing the color specifically, though there are some questions (here and here, for example), that use other gpar options. If anyone is aware of a duplicate, please mark it.

Community
  • 1
  • 1
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • My mistake with gpar was assuming the argument was "color=blue" as opposed to "col=blue". So thanks for the answer – Vinterwoo Jan 20 '17 at 14:57
  • How do you specify the text background color? – gd047 Apr 10 '22 at 06:45
  • I don't think a text grob has a "background". It would probably be something like: `grid.rect(gp=gpar(fill="red"))` then `grid.text("My Title", gp=gpar(col="blue"))`. – eipi10 Apr 10 '22 at 16:35