I am trying to make a loop in R with tickers I have created in Excel. I am trying to collect stock data from Yahoo finance, and the idea is that R is going to read the name of the stock from the Excel file, and then collect it from Yahoo Finance and create a graph.
In the data frame "Stocks" there are 10 different stocks listed in different columns, and I would like to run a loop so that I can get 10 different graphs. Here is the formula I have used to create a graph out of the first stock in the dataset.
`Stocks %>%
ggplot(aes(x=Date, y = NOVO.B.CO.Close)) +
geom_line(col = "darkgreen") +
geom_point(col = "darkgreen") +
theme_gray() +
ggtitle("Novo Nordiske B") `
Wrapping my comments into a proper answer. It is not clear whether you want to make this a single plot (using
facet_wrap) or multiple plots combined into a single window. When usingggplot2it is beneficial to have a singledata.framein long format, as one can letggplothandle all of the colouring and grouping based on a grouping column similar to the example belowFrom here the guide gives names for each colour, and
scale_*_(manual/discrete/continuous)can be used to change specific colour palettes (egscale_colour_discretecan be used to change the palette for factors).When it comes to combining ggplots the
patchworkpackage provides the simple interface. If we assume you have a vectortickers,titlesandcolorsrespectively, we can create a list of plots and combine them using simply addition (+).However for stocks the first option is likely going to give a better result.