Skip to content

Custom x-axis label in quantmod chartSeries

7 messages · Ilya Kipnis, Andre Luiz Tietbohl Ramos

#
Hello,

Is it possible to customize the x-axis label? Using ggplot I have the
custom x-label described below,

stocks %>%
ggplot(aes(x = date, y = cum_ret)) +
    theme_gray() +
    theme(plot.background = element_rect(fill = "gray86")) +
    geom_line(color = "blue") +
    *theme(axis.text.x = element_text(angle = 50, hjust = 1))* +
    labs(x = 'Data',
         y = 'Cumulative Return',
         title = paste0('Portfolio Cumulative Return of ',
length(tickers.clean), ' stocks on ',
                        format(as.Date(end), format="%d/%m/%Y")),
         subtitle = paste0("Ativos: ", toString(tickers.clean), "\nWeights:
",
                           toString(percent(wts, accuracy = 0.1)))
         ) +




* scale_x_date(date_breaks = '2 weeks',
date_labels = '%d %b %y') +    scale_y_continuous(        breaks = seq(0,
10, 0.05),        labels = scales::percent_format(accuracy = 1)*
    )

TIA,

--
Andre Luiz Tietbohl Ramos, PhD
#
This isn't a reproducible example--we don't have the stocks data frame.
Also, it is bad practice to use dplyr in the R/Finance stack as it
overrides multiple functions, such as lag and filter.

On Mon, Feb 2, 2026 at 8:39?AM Andre Luiz Tietbohl Ramos <
andreltramos at gmail.com> wrote:

            

  
  
#
Dear Ilya,

Thanks for the prompt reply!  The code from my previous message works and I
uploaded as an example so you could see what my intent is.  Your suggestion
will be considered, thank you! Its output is attached (which is in
Portuguese).

Thanks,

--
Andr? Luiz Tietbohl Ramos, PhD
On Mon, Feb 2, 2026 at 12:56?PM Ilya Kipnis <ilya.kipnis at gmail.com> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20260202/7d3b1b8f/attachment.html>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Retorno_Acumulado_do_Portfolio_KNCA11,CRAA11,RURA11,XPCA11,HGAG11,IAGR11,RZAG11,LSAG11,VGIA11,MXRF11,VINO11_em_21-11-2025.pdf
Type: application/pdf
Size: 13548 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20260202/7d3b1b8f/attachment.pdf>
#
Andre,

I am saying that those of us that aren't *you* cannot run your code because
we don't know how you formulated the "stocks" object. Your code is not
reproducible to anybody else. Please include a minimum *reproducible*
 example.

-Ilya

On Mon, Feb 2, 2026 at 12:41?PM Andre Luiz Tietbohl Ramos <
andreltramos at gmail.com> wrote:

            

  
  
#
Dear Ilya,

Thanks for the prompt reply once more! The code I used is below and its
output is attached (which is in Portuguese).

tickers <- c('UNIP6.SA', 'KLBN4.SA', 'CSAN3.SA', 'BBAS3.SA', 'PETR4.SA', '
SBSP3.SA')
tickers.clean <- str_replace_all(tickers,'.SA$', '')
## weights
wts = c(0.4, 0.15, 0.15, 0.05, 0.1, 0.15)

wts_tbl <- tibble(symbol = tickers,
                  wts = wts)
wts_percent <- wts_tbl
wts_percent$wts <- sapply(wts_percent$wts, function(x) percent(x, accuracy
= 0.01))
print(wts_percent)

## YF download
price_data <- tq_get(tickers,
                     from = start,
                     to = end,
                     get = 'stock.prices')

## Daily return for each stock
ret_data <- price_data %>% group_by(symbol) %>%
    tq_transmute(select = adjusted,
                 mutate_fun = periodReturn,
                 period = "daily",
                 col_rename = "ret",
                 relationship = "many-to-many")

## weights, returns join on a daily basis
ret_data <- left_join(ret_data, wts_tbl, by = 'symbol', relationship =
"many-to-many")

## weighted average
ret_data <- ret_data %>%
    mutate(wt_return = wts * ret)

## removal of the .SA suffix from Brazilian stocks via custom function
del.SA downloaded through tidyquant
ret_data <- del.SA(ret_data, "tq")

## total return on a given day is the sum of the prices weight of the
portfolio
port_ret <- ret_data %>%
    group_by(date) %>%
    summarise(port_ret = sum(wt_return))

## Portfolio cumulative return via cumprod()
port_cumulative_ret  <- port_ret %>%
    mutate(cum_ret = cumprod(1 + port_ret))

## filter for period of interest
port_cumulative_ret <- port_cumulative_ret[port_cumulative_ret$date >=
as.Date("2024-06-01") &

 port_cumulative_ret$date <= today(),]

posrtfolio.return <-
    port_cumulative_ret %>% ggplot(aes(x = date, y = cum_ret)) +
    theme_gray() +
    theme(plot.background = element_rect(fill = "gray86")) +
    geom_line(color = "blue") +
    theme(axis.text.x = element_text(angle = 50, hjust = 1)) +
    labs(x = 'Data',
         y = 'Retorno Acumulado',
         title = paste0('Retorno Acumulado do Portfolio de
',length(tickers.clean), ' ativo(s) em ',
                        format(as.Date(end), format="%d/%m/%Y")),
         subtitle = paste0("Ativos: ", toString(tickers.clean), "\nPesos: ",
                           toString(percent(wts, accuracy = 0.1)))
         ) +
    scale_x_date(date_breaks = '2 weeks',
                 date_labels = '%d %b %y') +
    scale_y_continuous(
         breaks = seq(0, 10, 0.05),
        labels = scales::percent_format(accuracy = 1)
    )

Code for saving it to pdf seems quite straight forward and simple thus it
isn't shown.
I hope this work for you.  If it doesn't let me know for it does for me
(OSX v11.7, R v4.1).

Regards,

--
Andr? Luiz Tietbohl Ramos, PhD
On Mon, Feb 2, 2026 at 2:43?PM Ilya Kipnis <ilya.kipnis at gmail.com> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20260202/b557b56c/attachment.html>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Retorno_Acumulado_do_Portfolio_UNIP6,KLBN4,CSAN3,BBAS3,PETR4,SBSP3_em_02-02-2026.pdf
Type: application/pdf
Size: 9354 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20260202/b557b56c/attachment.pdf>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: port_cumulative_ret.Rdata
Type: application/octet-stream
Size: 7590 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20260202/b557b56c/attachment.obj>
#
Please include libraries used

On Mon, Feb 2, 2026 at 1:46?PM Andre Luiz Tietbohl Ramos <
andreltramos at gmail.com> wrote:

            

  
  
#
Hello,

The libraries loaded during startup in the .First section of my .Rprofile
file are in the attached file.

Regards,

--
Andr? Luiz Tietbohl Ramos, PhD
On Mon, Feb 2, 2026 at 4:02?PM Ilya Kipnis <ilya.kipnis at gmail.com> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20260202/4a147a25/attachment.html>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Rlibraries.r
Type: application/octet-stream
Size: 2853 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20260202/4a147a25/attachment.obj>