Discussion:
Adjusting title and caption to the left side of a whole picture
aleksander.zawalich
2016-10-19 15:55:59 UTC
Permalink
I have a question on the plot created with ggplot2 package. Let's say we
have such a plot:

df = data.frame(
numbers = seq(1,5),
labels = c(
"Brand",
"Good Brand",
"The Best Brand",
"The Best Brand Today",
"The Best Brand This Month"
)
)
df = df[order(-df$numbers),]
plot = ggplot(data = df, aes(x=labels, y=numbers))+
geom_bar(stat="identity", fill='orange')+
coord_flip()+
labs(
title = "Exemplary title",
caption = "Exemplary caption 1\nExemplary caption number 2"
)+
theme(
plot.title = element_text(hjust = -0.5),
plot.caption = element_text(hjust = -0.5),
panel.spacing = unit(c(0,0,0,20), "cm")
)
plot

Is there any better way to align title and caption to the left side of a
picture (not plot, but picture, so in this case there where "The best brand
in the world" starts), than making negative vjust values for title and
caption?

It is not really the best solution for two reasons:

- if caption has \n character, it is formatted very strangely and seems
to be dependent on caption length,
- the title length can change, and therefore the vjust values can change
too, so they cannot be constant
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Baptiste Auguie
2016-11-03 20:18:37 UTC
Permalink
try this,

gridExtra::grid.arrange(plot + labs(title="", caption=""), top =
grid::textGrob("title", hjust=0, x=0, gp=grid::gpar(font=2, fontsize=18)),
bottom = grid::textGrob("caption\ncaption", hjust=0, x=0,
gp=grid::gpar(font=3)))

HTH

b.
Post by aleksander.zawalich
I have a question on the plot created with ggplot2 package. Let's say we
df = data.frame(
numbers = seq(1,5),
labels = c(
"Brand",
"Good Brand",
"The Best Brand",
"The Best Brand Today",
"The Best Brand This Month"
)
)
df = df[order(-df$numbers),]
plot = ggplot(data = df, aes(x=labels, y=numbers))+
geom_bar(stat="identity", fill='orange')+
coord_flip()+
labs(
title = "Exemplary title",
caption = "Exemplary caption 1\nExemplary caption number 2"
)+
theme(
plot.title = element_text(hjust = -0.5),
plot.caption = element_text(hjust = -0.5),
panel.spacing = unit(c(0,0,0,20), "cm")
)
plot
Is there any better way to align title and caption to the left side of a
picture (not plot, but picture, so in this case there where "The best brand
in the world" starts), than making negative vjust values for title and
caption?
- if caption has \n character, it is formatted very strangely and
seems to be dependent on caption length,
- the title length can change, and therefore the vjust values can
change too, so they cannot be constant
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Robin Edwards
2018-08-07 00:35:02 UTC
Permalink
Hope no-one minds if I pick up on this old issue.

This is a handy workaround by Baptiste, but I do wonder if ggplot devs
might consider incorporating this as a theme option? My reason for this is
that

1. horizontal bar plots are popular choice when labels are long, and for
this very reason the title often ends up starting in the middle of the page
(see example below); and
2. this grid approach breaks the pipe and so seems inconsistent with the
Tidyverse philosophy.


data_frame(pangram = c('The quick brown fox jumps over the lazy dog',
'How vexingly quick daft zebras jump',
'Jackdaws love my big sphinx of quartz'), n =
c(80,12,8)) %>%
ggplot(aes(pangram, n)) + geom_bar(stat='identity') +
coord_flip() + labs(title = 'Incidence of English pangrams')

<Loading Image...>
Anyway just my view!

Robin
London
Post by Baptiste Auguie
try this,
gridExtra::grid.arrange(plot + labs(title="", caption=""), top =
grid::textGrob("title", hjust=0, x=0, gp=grid::gpar(font=2, fontsize=18)),
bottom = grid::textGrob("caption\ncaption", hjust=0, x=0,
gp=grid::gpar(font=3)))
HTH
b.
Post by aleksander.zawalich
I have a question on the plot created with ggplot2 package. Let's say we
df = data.frame(
numbers = seq(1,5),
labels = c(
"Brand",
"Good Brand",
"The Best Brand",
"The Best Brand Today",
"The Best Brand This Month"
)
)
df = df[order(-df$numbers),]
plot = ggplot(data = df, aes(x=labels, y=numbers))+
geom_bar(stat="identity", fill='orange')+
coord_flip()+
labs(
title = "Exemplary title",
caption = "Exemplary caption 1\nExemplary caption number 2"
)+
theme(
plot.title = element_text(hjust = -0.5),
plot.caption = element_text(hjust = -0.5),
panel.spacing = unit(c(0,0,0,20), "cm")
)
plot
Is there any better way to align title and caption to the left side of a
picture (not plot, but picture, so in this case there where "The best brand
in the world" starts), than making negative vjust values for title and
caption?
- if caption has \n character, it is formatted very strangely and
seems to be dependent on caption length,
- the title length can change, and therefore the vjust values can
change too, so they cannot be constant
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...