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:
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"))
This looks really bad, and I would like to change a few things:
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:
From the "it's stupid but it works" file, you can add spaces to the right of center to force left alignment. The right number of spaces could be determined using math, but I couldn't see how to pass a string variable back into atop
.
# 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"))