Change Main Titles' Position

2,233 views
Skip to first unread message

KR

unread,
Jun 27, 2013, 9:53:06 AM6/27/13
to ggp...@googlegroups.com
Hi,

I have used the ggplot package to create a plot and I would like to put the main title of the plot at the bottom of the plot rather than the top. Even though it looks a simple question but I have not been able to figure out how to do it.

Here is my code:
ggplot(melt.combined.distribution.dataframe, aes(Race, Percentage, fill=Cohort)) + geom_bar(position="dodge") + coord_flip() + labs(title = "Combined Distribution") + theme(plot.title = element_text(size = rel(2), colour = "blue"))

I would appreciate any help in this regard.

KR

unread,
Jun 27, 2013, 2:56:14 PM6/27/13
to ggp...@googlegroups.com
Hi Jinyi,

Thank you for your response.
I tried your code. It works somehow OK on my side. When I ran your code along with my code (below), "your title" displayed in the middle of the plot. So, I tried to change the value of vjust to have the "your title" at the bottom of the plot which I was able to do so. However, it seems that the position also changes when I change the size of the plot by zooming in and out. In other words, it seems that the title is in a completely separate layer.

ggplot(melt.combined.distribution.dataframe, aes(Race, Percentage, fill=Cohort)) + geom_bar(position="dodge") + coord_flip() + ggtitle("your title") + theme(plot.title=element_text(vjust=-50))


-----------------

Hi KR,

For adjusting the title position, I tried the following and it met what I need, but I'm sure there is a better to do it - 
ggtitle("your title") + theme(plot.title=element_text(vjust=-50))

If you have the R Graphics cookbook, I modified it based on page 212-213.  

Hope it helps.
Jinyi

KR

unread,
Jun 28, 2013, 4:54:14 PM6/28/13
to ggp...@googlegroups.com
Hi Jinyi,

You are right. It works OK. Thank you for your help.
However, another problem pops up. Since x axis has its own label, the room for a main title for the plot is very small and the title at the bottom of the plot can easily goes off the border of the panel.
Do you have any advice on that?

KR

--------------------------- 


Hi KR,

Yeah, I zoomed in the plot and the title seemed relocated to the center of the plot; but when I exported the graph to images, the title was at the bottom of the plot.  I tried a couple of different values, -50 is the one that would show at the bottom position nicely.  

Jinyi
Reply all
Reply to author
Forward
0 new messages