I like very much the style of the titles of facet plots:
require(ggplot2)
df <- data.frame(x = rnorm(1E3), labels = as.factor(c(rep("plot1",500), rep("plot2",500))));
ggplot(df, aes(x=x) ) + geom_histogram() +facet_wrap(~labels)
And I would like to format other plots' titles in a similar fashion. When I just add ggtitle() to the plot, I get the following:
ggplot(df, aes(x=x) ) + geom_histogram() +ggtitle('plot1 and plot2')
How can I format the ggtitle title to look like the facet_wrap title?
Thank you!
If you want thing to look just like the strip labels, you could create a new column for your title that contains a single group and use that variable to facet with.
Essentially using facets with a fake variable to get the look you like.
df$overall = "plot1 and plot2"
ggplot(df, aes(x=x) ) +
geom_histogram() +
facet_wrap(~overall)
aosmith's example is good but sometimes you might need to plot multiple data sets at once. In cases like that, it's better to create a dummy data set containing only the title and use it in facet_wrap . Here is an example;
require(ggplot2)
df <- data.frame(x = rnorm(1E3),
labels = as.factor(c(rep("plot1",500),
rep("plot2",500))))
head(df)
x labels
1 -1.23282556 plot1
2 1.00436360 plot1
3 -0.35275785 plot1
4 0.78827732 plot1
5 0.05207618 plot1
6 -1.75324781 plot1
You can then plot the data:
ggplot(data = data.frame(ftitle = "Plot1 and Plot2")) +
facet_wrap(~ ftitle) +
geom_histogram(data = df,
aes(x = x))
I needed to do it this way today, I hope somebody else might find it useful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.