I am writing a function to plot n series of data. My idea was to loop through each series and adding a new gg_smooth for each loop. It works when I "loop" by hand but inserting it into an actual loop overwrites the colour aestetic.
Data I am working with, the idea is to be able to have n number of columns:
Using the following lines i get the desired result:
gene_list <- c("tetA", "tet.W.")
gg <- ggplot()
gg <- gg + geom_smooth(data=df_analysis_summed,
aes(x=as.Date(dato), y=!!sym(gene_list[1]), linetype = oua_2, colour = gene_list[1] ),
method="auto", se=F)
gg <- gg + geom_smooth(data=df_analysis_summed,
aes(x=as.Date(dato), y=!!sym(gene_list[2]), linetype = oua_2, colour = gene_list[2] ),
method="auto", se=F)
gg + labs(colour = "gene")

I then try to add the functionality to a loop:
plot_genes_scat_smooth <- function (df,gene_list) {
plot <- ggplot()
for (gene_index in 1:length(gene_list)) {
print(gene_index)
print(gene_list[gene_index])
plot <- plot +
geom_smooth(data=df, aes(x=as.Date(dato), y=!!sym(gene_list[gene_index]), linetype=oua_2, colour = gene_list[gene_index]), method="auto", se=F)#+
#geom_point(data=df,aes(x=as.Date(dato), y=!!sym(gene), colour = gene, shape = oua_2))
}
plot
}
genes = c("tetA", "tet.W.")
plot_genes_scat_smooth(df_analysis_summed,gene_list = genes)
Using the function I get the following result:

It would seem that the colour aes of the first line is overwritten by the second call when doing trying to implement it as a function. How can that be?
