Hello,

I'm trying to generate a pdf file from a plot using the system above to no
avail for I receive the error: It may be damaged or use a file format that
Preview doesn’t recognise. when trying to open an example file such
as: “ITUB4_em_14_03_2026_14h29m.pdf”
with the message: could not be opened.

I don't know how to solve it for it works when using ggplot() but does not
when using chartSeries(), see below.  Data downloaded via getSymbols().

full.window.color.bg <-  'gray63

*Does not work:*

chartSeries("ITUB4.SA",
                type = "line",
                show.grid = TRUE,
                theme = chartTheme("white",
                                   up.col = 'forestgreen',
                                   dn.col = 'red',
                                   bg.col = "black",
                                   area = full.window.color.bg
                                   ),
                TA = NULL
                 )

grafico <- paste0(gsub("[[:punct:]]SA", "", "ITUB4.SA"),'_on_',
                  as.character(format(as.Date(end), format="%d_%m_%Y")),
"_",
                  as.character(hour(now())), "h",
as.character(minute(now())), "m",
                  ".pdf")
folder <- "/Users/andreltr/Documents/B3/"
arquivo.grafico <- as.character(paste0(folder, plt))

ggsave(filename = grafico,
             path = folder,
             device = "pdf",
             width = 25, height = 20, units = "cm")
dev.off()

Using ggplot() the pdf file is generated without problems, as the example
below shows. Data downloaded via tidyquant library functions.

cum.portfolio <-
    port_cumulative_ret %>% ggplot(aes(x = date, y = cum_ret)) +
    theme_gray() +
    theme(plot.background = element_rect(fill = "gray90")) +
    geom_line(color = "blue") +
    theme(axis.text.x = element_text(size = 5,
                                                         angle = 50,
                                                         hjust = 1)) +
    labs(x = 'Data',
         y = 'Cum Return',
         title = paste0('Portfolio Cumulative Return of ',
length(num.tickers),
                               ' stocks on ',
                               format(as.Date(end), format="%d/%m/%Y")),
         subtitle = paste0("Stocks: ", toString(tickers.names), "\nWeights:
",
                           toString(percent(wts, accuracy = 0.1)))
         ) +
         scale_x_date(date_breaks = '2 weeks',
                               date_labels = '%d %b %y') +
    scale_y_continuous(
        limits = c(0.8, 2),
        breaks = seq(0, 10, 0.05),
        labels = scales::percent_format(accuracy = 1)
    )

graph <- paste0('Cum_Returno_from_Portfolio_',
                  gsub(" ", "", toString(tickers.names)),'_on_',
                  format(as.Date(end), format= "%d_%m_%Y"),'_',
                  as.character(hour(now())), "h",
as.character(minute(now())), "m",
                  ".pdf")
folder <- "/Users/andreltr/Documents/B3/"
ggsave(filename = graph,
             path = diretorio,
             width = 25, height = 20, units = "cm")
dev.off()

Any help appreciated,

--
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.

Reply via email to