Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a ggplot2 subtitle with different size and colour?

I'm using ggplot2 to improve precipitation barplots.

Here's a reproducible example of what I want to achieve:

library(ggplot2) library(gridExtra) secu <- seq(1, 16, by=2) melt.d <- data.frame(y=secu, x=LETTERS[1:8]) m <- ggplot(melt.d, aes(x=x, y=y)) +   geom_bar(fill="darkblue") +    labs(x="Weather    stations", y="Accumulated Rainfall [mm]") +   opts(axis.text.x=theme_text(angle=-45, hjust=0, vjust=1),        title=expression("Rainfall"), plot.margin = unit(c(1.5, 1, 1, 1), "cm"),        plot.title = theme_text(size = 25, face = "bold", colour = "black", vjust = 5)) z <- arrangeGrob(m, sub = textGrob("Location", x = 0, hjust = -3.5, vjust = -33, gp = gpar(fontsize = 18, col = "gray40"))) #Or guessing x and y with just option z 

I don't know how to avoid using guessing numbers on hjust and vjust on ggplot2? Is there a better way to put a subtitle (not just using \n, but a subtitle with different text color and size)?

I need to be able to use with ggsave to have a pdf file.

Here are two related questions:

Add a footnote citation outside of plot area in R?

How can I add a subtitle and change the font size of ggplot plots in R?

Thanks for any help.

like image 937
Migue Avatar asked Jul 30 '12 15:07

Migue


People also ask

How do you change the color of the title in ggplot2?

Because you are using the same color for all the titles, you can also use title = element_text(colour = "#7F3D17") which should set the color of the plot title, axis titles and legend title to the same color.

How do you change the color of subtitles in R?

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

How do I add a caption in ggplot2?

Title, subtitle, caption and tag in ggplot2. When using ggplot2 you can set a title, a subtitle, a caption and a tag. There are two ways to add titles: using ggtitle or labs function. The former is only for titles and subtitles and the latter also allows adding tags and captions.

How do I make subtitles in R?

You can use the title function to add a title, a subtitle and the axis labels, separately or at the same time. You can add a title to a base R plot using the title function. This is equivalent to set the argument main of the plot function. The title function also supports adding a subtitle with the sub argument.


2 Answers

The latest ggplot2 builds (i.e., 2.1.0.9000 or newer) have subtitles and below-plot captions as built-in functionality. That means you can do this:

library(ggplot2) # 2.1.0.9000+   secu <- seq(1, 16, by=2) melt.d <- data.frame(y=secu, x=LETTERS[1:8])  m <-  ggplot(melt.d, aes(x=x, y=y)) m <- m + geom_bar(fill="darkblue", stat="identity") m <- m + labs(x="Weather    stations",                y="Accumulated Rainfall [mm]",               title="Rainfall",               subtitle="Location") m <- m + theme(axis.text.x=element_text(angle=-45, hjust=0, vjust=1))  m <- m + theme(plot.title=element_text(size=25, hjust=0.5, face="bold", colour="maroon", vjust=-1)) m <- m + theme(plot.subtitle=element_text(size=18, hjust=0.5, face="italic", color="black")) m 
like image 150
hrbrmstr Avatar answered Oct 01 '22 12:10

hrbrmstr


Ignore this answer ggplot2 version 2.2.0 has title and subtitle functionality. See @hrbrmstr's answer below.


You could use nested atop functions inside an expression to get different sizes.

EDIT Updated code for ggplot2 0.9.3

m <-  ggplot(melt.d, aes(x=x, y=y)) +       geom_bar(fill="darkblue", stat = "identity") +       labs(x="Weather    stations", y="Accumulated Rainfall [mm]") +       ggtitle(expression(atop("Rainfall", atop(italic("Location"), "")))) +      theme(axis.text.x = element_text(angle=-45, hjust=0, vjust=1),       #plot.margin = unit(c(1.5, 1, 1, 1), "cm"),       plot.title = element_text(size = 25, face = "bold", colour = "black", vjust = -1)) 

enter image description here

like image 42
Sandy Muspratt Avatar answered Oct 01 '22 14:10

Sandy Muspratt