Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize background color of ggtitle

Tags:

r

ggplot2

I want to be able to change the background of the ggtitle of my ggplot to forestgreen while keeping the text white. The color shouldn't apply to the whole of the graph but only the title. This is what I have so far:

p <- ggplot(...)
p <- p + ggtitle("Market Updates") + labs(x = "Date", y = "High")
p <- p + theme(plot.title = element.text(hjust = 0.5, size = 20, 
               color = "#FFFFFF"))

I would like to make it look like this:

enter image description here

like image 610
wittywillis Avatar asked Feb 20 '17 17:02

wittywillis


People also ask

How do I change the background color of my plot in R?

Background color of the plot area You can customize all the background color of the entire plot area with the bg argument of the par function. This will override all the background color of your plots unless you set back to the original graphical parameters.

How do I change the background in R?

In the menu bar, open the “Tools” menu. From the drop down menu, choose “Global Options”. In the pane on the left hand side of the options window, click “Appearance”. To import a theme, click on the “Add…” button.

How do I change the color of my title in R?

To change the color of Title and Subtitle, We simply add a color parameter to element_text() function.

How do I get rid of the GREY background in R?

Using theme_bw() function with the plot removes the grayish background but doesn't affect the Grid. Program: R.


1 Answers

Update from comments.

There are a couple of approaches to do this; using facet_, as Axeman suggests, to create a strip above the plot (it is easier to change the the format of the strip than the title strip) , or you can create a title strip manually and then glue it to the plot.

Example

library(ggplot2)
library(gridExtra)
library(grid)

# Create dummy variable to facet on: this name will appear in the strip
mtcars$tempvar <- "Market Updates"

# Basic plot
# Manually added legend to match your expected result
p <- ggplot(mtcars, aes(mpg, wt)) + 
        geom_line(aes(colour="Com")) +
        scale_colour_manual(name="", values=c(Com = "#228b22") ) +
        labs(x = "Date", y = "High")

Using facet_: this only adds the colour bar across the plot panel, although the title is centered.

p + facet_grid(. ~ tempvar) +
        theme(strip.background = element_rect(fill="#228b22"),
              strip.text = element_text(size=15, colour="white"))

Which produces

enter image description here

Using grid functions: this adds the colour bar across the plot panel, but the title is centered on the graphics window. (you could get a bit more control with positioning by adding it to the plot gtable)

my_g <- grobTree(rectGrob(gp=gpar(fill="#228b22")),
                 textGrob("Market Updates", x=0.5, hjust=0.5,
                                            gp=gpar(col="white", cex=1.5)))

grid.arrange(my_g, p, heights=c(1,9))

Which produces

enter image description here

like image 167
user20650 Avatar answered Sep 20 '22 02:09

user20650