Re: Multiline subtitle

Rui Barradas <[email protected]> Sat, 18 Jul 2026 17:30:16 +0100
Newsgroups gmane.comp.lang.r.general,gmane.comp.lang.r.r-metrics
Message-ID <[email protected]>
Às 16:05 de 18/07/2026, Andre Luiz Tietbohl Ramos escreveu:
> Hello,
>
> Suppose I want to have a multiline subtitle using ggplot() with aligned
> strings between first line and second line like a table.  I'm using the
> approach below to no avail,
>
> tickers.subtitle <- c("CPFE3.SA",  "CXSE3.SA", " ALUP11.SA",  "BBAS3.SA",  "
> CMIG4.SA",  "VALE3.SA", " LEVE3.SA", "VIVT3.SA", "TAEE4.SA", "RANI3.SA", "
> BRSR6.SA", "SAPR4.SA")
> ticker.subtitle.weights <- c("20.0%",   "15.0%",  "15.0%",  "5.0%", "5.0%",
> "5.7%", "5.7%", "5.7%", "5.7%", "5.7%", "5.7%", "5.7%")
>
> port_cumulative_ret %>% ggplot(aes(x = date, y = cum_ret)) +
> ...
> labs(
> ...
> subtitle = paste('Ativos: ', tickers.subtitle,
>                            '\nPesos: ', ticker.subtitle.weights)
> )
>
> Any suggestions?
>
> TIA,
>
> --
> André Luiz Tietbohl Ramos, PhD
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Hello,


Here is a solution from this StackOverflow post [1]


# sample data
port_cumulative_ret <- data.frame(
   date = Sys.Date() - (10:1),
   cum_ret = cumsum(rnorm(10))
)

tickers.subtitle <- c("CPFE3.SA",  "CXSE3.SA", "ALUP11.SA", "BBAS3.SA",
                       "CMIG4.SA",  "VALE3.SA", "LEVE3.SA", "VIVT3.SA",
                       "TAEE4.SA", "RANI3.SA", "BRSR6.SA", "SAPR4.SA")
ticker.subtitle.weights <- c("20.0%",   "15.0%",  "15.0%", "5.0%",
                              "5.0%", "5.7%", "5.7%", "5.7%", "5.7%",
                              "5.7%", "5.7%", "5.7%")

### Load the needed libraries
library(ggplot2)
library(scales)
library(grid)
library(gridExtra)

# prepare the tickers data
df1 <- data.frame(ticker.subtitle.weights)
df1 <- as.data.frame(t(df1))
names(df1) <- tickers.subtitle
row.names(df1) <- NULL

# the cumulative returns plot
plot1 <- port_cumulative_ret |>
   ggplot(aes(x = date, y = cum_ret)) +
   geom_line()

# tickers table
tbl <- tableGrob(df1, rows = NULL)

# put table below the plot
grid.arrange(plot1, tbl, nrow = 2, heights = c(8, 2))


[1] https://stackoverflow.com/q/41164675/8245406


Hope this helps,

Rui Barradas