2

I am fairly new to R/ggplot2 and still learning on the go. Hopefully I am not missing something obvious!

I am trying to create several different plots using ggplot2 that I am layouting using the function plot_grid from the cowplot package to make the plots visible side by side and add plot numeration and captions. The problem is that if the generated plots are displayed in a small window or I have many plots beside one another then the titles of the two plots sometimes overlap. To solve this problem I tried to automatically insert line breaks in my too long titles using code I found in another thread since I wanted the text size of the titles to stay constant.

Using the following code I can easily automatically insert the necessary line breaks to make my title a specific width, but the problem is that I always need to enter a numeric value for the width. Depending on the number of plots I am inserting this value would of course change. I could of course go through my code and manually set the width for each set of plots until it is the correct value, but I was hoping to automate this process so that the title width is adjusted automatically to match the width of the x-axis. Is there anyway to implement this in R?

    #automatically line break and add titles 
    myplot_theme1 = function (plot, x.title = NULL, y.title = NULL, plot.title = NULL) {
        plot +   
        labs(title = paste(strwrap(plot.title, width = 50), collapse = "\n"),
           x = x.title, 
           y = y.title)
    }


    # generate an example plot
    data_plot <- data.frame(x = rnorm(1000), y = rnorm (1000))
    plot1 <- ggplot(data_plot, aes(x = x, y = y)) + geom_point() 
    title <- "This is a title that is very long and does not display nicely"
    myplot_theme1(plot1, plot.title = title)

My test plot

I have tried searching but I haven't found any solutions that seem to address what I am looking for. The only solution I did find that looked promising was based on the package gridDebug. This packages doesn't seem to be supported by my operating system anymore though (macOS Sierra Version 10.12.6) since when I try to install it I get the following error message:

Warning in install.packages: dependencies ‘graph’, ‘Rgraphviz’ are not available

And on the CRAN package documentation it states that the package is not even available for macOS El Capitan which was my previous operating system. If someone knows what is causing this issue so that I could try the solution from the above thread that would of course be great as well.

Sophia
  • 33
  • 1
  • 8
  • Did you try to put the plot output inside a ```div```? – amrrs Sep 19 '17 at 09:44
  • No I haven't. I am not sure what you mean with a div exactly though? I've tried googling it but apparently am not searching in the correct context. A tip where I can find some more information? – Sophia Sep 19 '17 at 14:00

1 Answers1

0

One idea (but perhaps not an ideal solution) is to adjust the size of text based on the number of characters in the title. You can adjust ggplot properties using theme and in this case you want to adjust plot.title (the theme property, not your variable). plot.title has elements size and horizontal justification hjust, the latter is in range [0,1].

# generate an example plot
data_plot <- data.frame(x = rnorm(1000), y = rnorm (1000))
plot1 <- ggplot(data_plot, aes(x = x, y = y)) + geom_point() 
title1 <- "This is a title that is very long and does not display nicely"
title2 <- "I'm an even longer sentence just test me out and see if I display the way you want or you'll be sorry"

myplot_theme1 = function (plot, x.title = NULL, y.title = NULL, plot.title = NULL) {
        plot +   
        labs(title = plot.title,
           x = x.title, 
           y = y.title) +
        theme(plot.title = element_text(size=800/nchar(plot.title), hjust=0.5))  # 800 is arbitrarily chosen
    }

myplot_theme1(plot1, plot.title = title1)
myplot_theme1(plot1, plot.title = title2)
CPak
  • 13,260
  • 3
  • 30
  • 48
  • Sadly it won't solve my problem since I need the text size to stay constant (the plots are for a report and the figure titles all need to have a standardized text font). Thank you though! – Sophia Sep 19 '17 at 14:03
  • If your text size must stay the same, you could use a similar idea as above, that is, adjust the width of your plot by some value that depends on the number of characters in your title. Something like `strwrap(plot.title, width = nchar(plot.title)*X)` – CPak Sep 19 '17 at 14:20
  • If I don't find another solution I'll do that, its not quite ideal though since it will make some of my plots look stretched out. Maybe someone will have a suggestion how to get the x-axis width. – Sophia Sep 21 '17 at 07:18