Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align title and subtitle in ggplot2 when generated via expression

I'm using the code below to generate a simple box plot in ggplot2:

# Libs data
data("mtcars"); require(ggplot2); require(ggthemes)
# Chart
ggplot(data = mtcars) +
  geom_boxplot(aes(y = wt, x = as.factor(am)),
               fill = "gray87") +
  xlab("AM") +
  ylab("WT") +
  theme_gdocs() +
  ggtitle("WT by AM") +
  theme(axis.title.y = element_text(angle = 90),
        axis.ticks = element_line(colour = "black", linetype = "solid",
                                  size = 0.5),
        panel.grid = element_line(colour = "gray"))

The generated chart is fairly straightforward: First chart

Task

I would like to add a subtitle to my chart and have some control over how it's rendered. I'm following this discussion and with use of the code:

# Chart
ggplot(data = mtcars) +
  geom_boxplot(aes(y = wt, x = as.factor(am)),
               fill = "gray87") +
  xlab("AM") +
  ylab("WT") +
  theme_gdocs() +
  ggtitle(expression(atop("WT by AM", 
                          atop(italic("Some crucial note that has to be here"), "")))) +
  theme(axis.title.y = element_text(angle = 90),
        axis.ticks = element_line(colour = "black", linetype = "solid",
                                  size = 0.5),
        panel.grid = element_line(colour = "gray"))

I get the following chart: second attempt


This looks really bad, and I would like to change a few things:

  1. Make both subtitle and the title left-justified
  2. Reduce the white space between the two lines
  3. Keep the font bold

Attempts

I tried different things, like for instance the code below:

ggplot(data = mtcars) +
  geom_boxplot(aes(y = wt, x = as.factor(am)),
               fill = "gray87") +
  xlab("AM") +
  ylab("WT") +
  theme_gdocs() +
  ggtitle(expression(atop("WT by AM", 
                          atop(italic("Stupid note"), "")))) +
  theme(axis.title.y = element_text(angle = 90),
        axis.ticks = element_line(colour = "black", linetype = "solid",
                                  size = 0.5),
        panel.grid = element_line(colour = "gray"),
        plot.title = element_text(size = 16, colour = "black", hjust = -1))

but it hides the title entirely:

missing title

like image 854
Konrad Avatar asked Dec 04 '15 12:12

Konrad


1 Answers

There is a new version of ggplot2 (2.1.0.9000+) currently on Github that solves your problem. For more information, see Bob Rudis' vignette.

devtools::install_github("hadley/ggplot2")  # Until the new version is available on CRAN
library(ggplot2)
library(ggthemes)  # Only necessary because the OP used theme_gdocs()

ggplot(data = mtcars) +
  geom_boxplot(aes(y = wt, x = as.factor(am)), fill = "gray87") +
  xlab("AM") +
  ylab("WT") +
  theme_gdocs() +
  ggtitle("WT by AM", subtitle = "pearl of wisdom") +  # subtitle is a new argument. Both are now left-justified by default
  theme(plot.title=element_text(margin=margin(b=0), size = 20), plot.subtitle=element_text(margin=margin(t=5, b = 10)))  # Changing the size and margins of the title and subtitles to give the OP some options.

Title and Subtitle Image

like image 138
Carolyn W Clayton Avatar answered Sep 23 '22 08:09

Carolyn W Clayton