Search code examples
rplotbar-chartcentering

How to center the barplot title horizontally in the whole image using vanilla R?


In R, using the function barplot gives me the title centered horizontally over the plot area, not over the whole image.

par(mar=c(3,13,4,2))
barplot(10:1,horiz=T,main='Centralized title',las=1,names.arg=rep(paste0(letters,collapse=''),10))

R's default barplot

How can I have the title centered on the whole image instead, like in the image below?

R's barplot with title centered in the image

I know I can fiddle with adj parameter, but it's based on trial and error, the correct value changes with font size, among other problems. Is there a simpler solution?


Solution

    1. Set an outer margin
    2. Use mtext
    par(mar=c(3,13,0,2), oma=c(0,0,3,0))
    barplot(10:1,horiz=T,las=1,names.arg=rep(paste0(letters,collapse=''),10))
    mtext('Centralized title', outer = TRUE)
    

    enter image description here