Home > Enterprise >  How to center align a wrapped axis title in ggplot2?
How to center align a wrapped axis title in ggplot2?

Time:08-25

I have a really long y-axis title with scientific notation that I want to wrap and make center aligned. I can either wrap the title and have it left aligned, or center an incorrect format of the title. How can I wrap the axis title AND have it centered in ggplot2?

Example Data

set.seed(321)
df <- data.frame(matrix(ncol = 2, nrow = 3))
colnames(df)[1:2] <- c("date","value")
df$value <- round(rnorm(3, 100,30),0)
st <- as.Date("2020-01-01")
en <- as.Date("2020-03-31")
df$date <- seq.Date(st,en,by = '1 month')

This makes the ggplot2 figure with the unwrapped long y-axis title

library(ggplot2)

ggplot(df, aes(x = date, y = value))  
  geom_point()  
  labs(x = "",
       y = expression(paste("Here is a really really really looooonnnng title with notation (  ", m^-2,")")))  
  scale_x_date(breaks = seq(st, en, by = "1 month"),
               limits = c(st,en),
               date_labels = "%b %Y")  
  scale_y_continuous(limits = c(0,150),
                     breaks=seq(0,150,25))  
  theme_bw()  
  theme(panel.grid = element_blank(),
        text = element_text(size = 16),
        axis.text.x = element_text(size = 14, color = "black", angle = 90, vjust = 0.5, hjust = 1),
        axis.text.y = element_text(size = 14, color = "black"))

I have tried using \n which wraps the title but, the title is not centered, it is left aligned

ggplot(df, aes(x = date, y = value))  
  geom_point()  
  labs(x = "",
       y = expression(paste("Here is a really really really \n looooonnnng title with notation (  ", m^-2,")")))  
  scale_x_date(breaks = seq(st, en, by = "1 month"),
               limits = c(st,en),
               date_labels = "%b %Y")  
  scale_y_continuous(limits = c(0,150),
                     breaks=seq(0,150,25))  
  theme_bw()  
  theme(panel.grid = element_blank(),
        text = element_text(size = 16),
        axis.text.x = element_text(size = 14, color = "black", angle = 90, vjust = 0.5, hjust = 1),
        axis.text.y = element_text(size = 14, color = "black"))

Using str_wrap() centers the title but the title is no longer formatted correctly

library(stringr)

ggplot(df, aes(x = date, y = value))  
  geom_point()  
  labs(x = "",
       y = str_wrap(
         expression(paste("Here is a really really really \n looooonnnng title with notation (  ", m^-2,")")),50)
       ) 
  scale_x_date(breaks = seq(st, en, by = "1 month"),
               limits = c(st,en),
               date_labels = "%b %Y")  
  scale_y_continuous(limits = c(0,150),
                     breaks=seq(0,150,25))  
  theme_bw()  
  theme(panel.grid = element_blank(),
        text = element_text(size = 16),
        axis.text.x = element_text(size = 14, color = "black", angle = 90, vjust = 0.5, hjust = 1),
        axis.text.y = element_text(size = 14, color = "black"))

I tried creating a wrapper function based on this similar SO question but it also did not format the title correctly

wrapper <- function(x, ...) 
{
  paste(strwrap(x, ...), collapse = "\n")
}

my_title = expression(paste("Here is a really really really looooonnnng title with notation (  ", m^-2,")"))

ggplot(df, aes(x = date, y = value))  
  geom_point()  
  labs(x = "",
       y = wrapper(my_title, 50))  
  scale_x_date(breaks = seq(st, en, by = "1 month"),
               limits = c(st,en),
               date_labels = "%b %Y")  
  scale_y_continuous(limits = c(0,150),
                     breaks=seq(0,150,25))  
  theme_bw()  
  theme(panel.grid = element_blank(),
        text = element_text(size = 16),
        axis.text.x = element_text(size = 14, color = "black", angle = 90, vjust = 0.5, hjust = 1),
        axis.text.y = element_text(size = 14, color = "black"))

CodePudding user response:

You can try to use the package ggtext. If we want to use it, we need to change the normal expression style to HTML and CSS style (which is <sup></sup> for superscript).

For clearer code, I've put your y-axis title into a variable y_lab. The code that is doing the job is theme(axis.title.y = element_textbox_simple(orientation = "left-rotated", halign = 0.5)).

library(ggplot2)
library(ggtext)

y_lab <- "Here is a really really really looooonnnng<br>title with notation (m<sup> -2</sup>)"
ggplot(df, aes(x = date, y = value))  
  geom_point()  
  labs(x = "",
       y = y_lab) 
  scale_x_date(breaks = seq(st, en, by = "1 month"),
               limits = c(st,en),
               date_labels = "%b %Y")  
  scale_y_continuous(limits = c(0,150),
                     breaks=seq(0,150,25))  
  theme_bw()  
  theme(panel.grid = element_blank(),
        text = element_text(size = 16),
        axis.text.x = element_text(size = 14, color = "black", angle = 90, vjust = 0.5, hjust = 1),
        axis.text.y = element_text(size = 14, color = "black"),
        axis.title.y = element_textbox_simple(orientation = "left-rotated", halign = 0.5))

  • Related