1

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:

data

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")

Desired outcome

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:

Wrong result from loop/function

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?

1
  • Hey, i've got a similar question a while before. You might check here. Maybe one of those answers are helping you out. I guess the functionallity is the same Commented Mar 4, 2020 at 13:48

1 Answer 1

1

Instead of trying to add multiple lines, one-by-one onto the plot. Ggplot2 works best with the data in a long format. In this case the data is pivoted so that there one column for gene type and one column for the corresponding value.

#create some data
set.seed(1)
dato<-seq.Date(as.Date("2018-04-25"), length.out = 14, by="1 day")
oua_2<-rep(c(0, 1), 7)
tetA<-rnorm(14, 0.04, 0.02)
tet.W<-rnorm(14, 0.2, 0.02)

df_analysis_summed <- data.frame(dato, oua_2, tetA, tet.W)

#convert the data frame to long
library(tidyr)
df_analysis_long <- df_analysis_summed %>% pivot_longer(starts_with("tet"), names_to = "genes", values_to = "value")

#function to plot
plot_genes_scat_smooth_long <- function (df) {
  plot <- ggplot()
  plot <- plot +
    geom_smooth(data=df_analysis_long, aes(x=as.Date(dato), y=value, linetype=as.factor(oua_2), colour = genes), method="auto", se=F)

  plot
}

plot_genes_scat_smooth_long(df_analysis_long)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.