Graphics in ggplot2, I want to add a loop to create graphics

Asked

Viewed 63 times

0

I have a question in ggplot, I want to compare two tables and create graphs for each Gene in R:

Table 1 Tabela 1

Table 2 below I used as reference in the index for the loop:

Tabela 2

"gene_data" is variable for Table 2, and "Output" is variable for Table 1, so I wanted to create a loop using Table 2 as an index, only the problem is that the loop is only returning the "ZCWPW1" gene chart, which in this case is line 21 of Table 2

for (i in nrow(gene_data)) {
  gene_saida = subset(saida, saida$gene_symbols == gene_data$GENES[i])
  
  print(ggplot(gene_saida, aes(x=most_severe_consequence)) +
    geom_bar(stat = "count")+
    labs(title = gene_data$GENES[i])+
    ylab(label = "Frequencia")+
    xlab(label = "Variantes"))
}

type I could manually do all Graphics without the loop only that had to keep changing the gene_data$GENES[ ] inside this index I was placing the row number of table 2 :

gene_saida = subset(saida, saida$gene_symbols == gene_data$GENES[1])

#FREQUENCIA DE VARIANTES LOF
ggplot(gene_saida, aes(x=most_severe_consequence)) +
  geom_bar(stat = "count")+
  labs(title = "Gene ZCWPW1")+
  ylab(label = "Frequencia")+
  xlab(label = "Variantes")
  • 1

    Welcome to Stackoverflow! Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please take a look at this link (mainly in the use of function dput) and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

  • Marlon, you’re not making the loop for (i in nrow(gene_data)) because there is no iteration modify to for (i in 1:nrow(gene_data))

  • @Daniel has already pointed out why his loop doesn’t work. But what do you want with it? As it is, it will display on the screen one graph after another.

  • Guys thanks for the help, manage to solve my doubt, the resolution is available.

1 answer

1

Guys can solve, I added a loop that will go through all the lines of "gene_data" (table 2), and for each row traveled it will make the chart in png format with the line name of "gene_data", for example, let’s assume that it will go through line 1 of "gene_data", line 1 is the ABCA7 gene (table 2), so it will rename the chart to ABCA7 which is name contained in line 1.

for (i in unique(gene_data$GENES)) {
      nome_grafico = paste(i,".png", sep = "")
      png(filename = nome_grafico, width = 15, height = 15, units = "cm", pointsize = 12, res = 150)

Well, the first part was already, that was to choose the format and rename the charts, now let’s add the loop, for this we have to make a subset comparing the columns of "output" (table 1) with the lines of "gene_data", for example, line 1 of "gene_data" is ABCA7 so subset will filter all rows where ABCA7 appears in "output", and eventually will generate the ABCA7 chart.

print(ggplot(subset(saida, gene_symbols == i), aes(x=most_severe_consequence)) +
        geom_bar(stat = "count")+
        labs(title = i)+
        ylab(label = "Frequencia")+
        xlab(label = "Variantes"))
      dev.off()
    }

Complete R loop code below:

library(ggplot2)

for (i in unique(gene_data$GENES)) {
  nome_grafico = paste(i,".png", sep = "")
  png(filename = nome_grafico, width = 15, height = 15, units = "cm", pointsize = 12, res = 150)
  print(ggplot(subset(saida, gene_symbols == i), aes(x=most_severe_consequence)) +
    geom_bar(stat = "count")+
    labs(title = i)+
    ylab(label = "Frequencia")+
    xlab(label = "Variantes"))
  dev.off()
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.