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:
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.
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.
To change the color of Title and Subtitle, We simply add a color parameter to element_text() function.
Using theme_bw() function with the plot removes the grayish background but doesn't affect the Grid. Program: R.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With