1

I am creating following graph on ggplot and need to annotate some info on each graph as subtitle, the graph looks like this: enter image description here For title and subtitle purposes, I wrote the following code:

plot.title <- "Link A" 
Common <- paste("Percentage:", "10%", sep=" ")
Average <- paste("Average:", "83", sep= " ")
plot.subtitle <- paste(Common, AverageSearchSpace, sep="\n")

and add this in ggplot as:

ggtitle(bquote(atop(.(plot.title), atop(.(plot.subtitle), "")))) 

Yet as it can be seen the titles are overlapping currently and i could not find a way to re-position them without overlapping.

I was wondering what the solution to separate the overlapping titles is. I tried to increase the plot margin in theme() with:

theme(plot.margin = unit(c(2, 2, 2, 2), "cm")

However, this did not help.

Also, I tried the following:

plot.title = element_text(size = 85,colour="black", vjust = -2)

This seems to adjust all of the title's position rather than subtitle and title separately.

Also, I could not find any command in theme() such as plot.subtitle to arrange its position. It seems it does not exist.

Any help code piece or related link is appreciated. Thanks.

kukushkin
  • 312
  • 4
  • 16
  • 1
    you don't provide a reproducible example so it's hard to help; this might help, also recently asked http://stackoverflow.com/questions/35367095/multi-line-ggplot-title-with-different-font-size-face-etc?noredirect=1#comment58440550_35367095 – MLavoie Feb 13 '16 at 10:10
  • Thank you very much. I search for it but never thought of writing multi line on search. I was looking for multiple lines subtitle title etc. could not find this. thanks for your time. @MLavoie – kukushkin Feb 15 '16 at 19:54

2 Answers2

2

The position of titles and subtitles is automatically adjusted, however, this positioning clearly fails if titles/subtitles have more than one line and if the size is relatively large, as in your case. So hence the overlap. The easiest way to tackle this, is simply to add an extra (blank) line to your title. Because the title then shifts up, you need to adjust the margins.

library(ggplot2)
library(grid)
#first some toy data, next time please provide some yourself!
data <- data.frame(x=5*rep(1:100,each=5),type=rep(c("BM1","BM2","BM3","NB1","NB2"),20),y=10*(2+rnorm(500)))

plot.title <- "Link A\n" # added an extra line here 
Common <- paste("Percentage:", "10%", sep=" ")
Average <- paste("Average:", "83", sep= " ")
plot.subtitle <- paste(Common, Average, sep="\n")
plot.tottitle <- paste(plot.title,Common, Average, sep="\n")

ggplot(data,aes(x=x,y=y,color=type))+
  geom_line() + ggtitle(bquote(atop(.(plot.title), atop(.(plot.subtitle), ""))))  + 
  theme(plot.title = element_text(size = 50,colour="black", vjust = 0)) +
  theme(plot.margin = unit(c(2, 0, 0, 0), "cm")) #margin adjusted because title moves off plot.

Resulting picture:enter image description here

There is another option, if you want more control: put only the title or subtitle above the plot with ggtitle and use annotate for the other. (please consider that as homework for next week ;-) )

RHA
  • 3,677
  • 4
  • 25
  • 48
  • thanks a lot for your answer. I used annotate in the beginning. However I clearly failed that one as well. I will try it again with annotate option to learn better. Wonderful answer, thanks again! @RHA – kukushkin Feb 15 '16 at 19:55
0

You could use this trick

enter image description here

require(ggplot2)
require(gridExtra) # tableGrob

element_grob.element_custom <- function(element, label="", ...)  {

  mytheme <- ttheme_minimal(colhead = list(fg_params = list(parse=TRUE, fontsize = 16)),
                            core = list(fg_params = list(parse=TRUE)))
  disect <- strsplit(label, "\\n")[[1]]
  m <- as.matrix(disect[-1])
  g1 <- tableGrob(m, cols = disect[1], theme=mytheme)
  # wrapping into a gTree only because grobHeight.gtable would be too tight
  # cf. absolute.units() squashing textGrobs
  gTree(children=gList(g1), height=sum(g1$heights), 
        cl = "element_custom")
}

# gTrees don't know their size 
grobHeight.element_custom = heightDetails.element_custom = function(x, ...)
  x$height
# silly wrapper to fool ggplot2's inheritance check...
element_custom <- function() {
  structure(list(), class = c("element_custom", "element_text"))
}

title <- c("First~line \n italic('wait, a second')\n integral(f(x)*dx, a, b)")


ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_line() + ggtitle(title) +
  (theme_grey() %+replace% theme(plot.title  = element_custom()))
baptiste
  • 75,767
  • 19
  • 198
  • 294