Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing the same Title on a Function

I have the following table:

gene.name = rep(c("MsLHCB1.1", "MsLHCA6", "MsPLGG1-A", "MsRPI3-B", 
                 "MsRPI1-B", "MsPGK1", "MsCFBP1-A", "MsPSBTN", "MsTKL-1", "MsRCA-A", 
                 "MsCRD1-B"),5)
tpi = c(rep(0,11), rep(1,11), rep(7, 11), rep(14, 11), rep(22, 11))
lfc = runif(55, -30, 25)
lfc.df = data.frame(gene.name, tpi, lfc)
names(lfc.df) = c("Gene.Name", "tpi", "LFC")

I want to plot these LFCs over the tpi and create a unique plot for each Gene Name factor. With the help of @ekoam, I was able to develop this funciton:

lfc.plots = function(data, title) {
  ggplot(data) +
  geom_bar(aes(x = tpi, y = LFC), stat = "identity") +
  geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
  labs(title = title) +  
  theme_prism() + 
  theme_bw() +
  theme(axis.ticks.y = element_line(color = "black")) + guides(y = guide_prism_minor()) +
  theme(plot.title = element_text(face = "bold.italic", size = 16, family = "sans", hjust = .5)) + 
  theme(axis.title.x = element_blank()) +
  theme(axis.text.x = element_blank()) +
  theme(axis.title.y = element_text(size = 14)) +
  theme(axis.text.y = element_text(size = 12)) + 
  theme(panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          strip.background = element_blank(),
          strip.text.x = element_text(size = 16, family = "sans", face = "italic"),
          panel.border = element_rect(colour = "black", fill = NA)) + 
  theme(plot.margin = unit(c(0.2,0.5,1.25,0.5), "cm")) +
  ggtitle(title)
}

I then run this code to create a tibble of the ggplots:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

result.lfc = lfc.df %>% 
  nest(data = -Gene.Name) %>%
  mutate(plots = lapply(data, lfc.plots, Gene.Name))

While I am able to get a ggplot for each gene name, the ggtitle is the same (the first value in the Gene.Name column) and I cannot seem to get this resolved. Would anybody have any suggestions?

>Solution :

The issue is that using lapply you always pass the vector Gene.Name to your function and only the first element of this vector is used for the title. Hence you end up with same title for all plots. Instead you could achieve your desired result using purrr::map2(data, Gene.Name, lfc.plots) to loop over both the data column and the Gene.Name column.

library(ggplot2)
library(purrr)
library(tidyr)
library(dplyr)

lfc.plots <- function(data, title) {
  ggplot(data) +
    geom_bar(aes(x = tpi, y = LFC), stat = "identity") +
    geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
    labs(title = title) +
    # theme_prism() +
    theme_bw() +
    theme(axis.ticks.y = element_line(color = "black")) +
    # guides(y = guide_prism_minor()) +
    theme(plot.title = element_text(face = "bold.italic", size = 16, family = "sans", hjust = .5)) +
    theme(axis.title.x = element_blank()) +
    theme(axis.text.x = element_blank()) +
    theme(axis.title.y = element_text(size = 14)) +
    theme(axis.text.y = element_text(size = 12)) +
    theme(
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      strip.background = element_blank(),
      strip.text.x = element_text(size = 16, family = "sans", face = "italic"),
      panel.border = element_rect(colour = "black", fill = NA)
    ) +
    theme(plot.margin = unit(c(0.2, 0.5, 1.25, 0.5), "cm")) +
    ggtitle(title)
}

result.lfc <- lfc.df %>%
  nest(data = -Gene.Name) %>%
  mutate(plots = purrr::map2(data, Gene.Name, lfc.plots))

result.lfc$plots[[1]]

result.lfc$plots[[3]]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading