lawremi / ggbio

Grid and ggplot2 based visualization for biological data
111 stars 24 forks source link

Don't know how to export ggbio object to a figure #172

Open albustruong opened 5 months ago

albustruong commented 5 months ago

Aim

To generate Manhattan plot with plotGrandLinear function

Script

library(GenomicRanges) library(ggbio) library(readr) library(tidyr) library(dplyr)

input_dir = "/mnt/d/NGS/rna-seq/20240410/11_plot_prep" output_dir = "/mnt/d/NGS/rna-seq/20240410/13_manhattan_plot/ggbio" input_files <- list.files(path = input_dir, pattern = "\_transpose.csv$", full.names = TRUE)

for (file in input_files) { df <- read_csv(file, col_names = FALSE)

head(df)

colnames(df)[1] <- "Chromosome"

df_long <- df %>%
    pivot_longer(cols = -Chromosome, 
           names_to = "Position", 
           values_to = "ConversionRate") %>%
    drop_na(ConversionRate)

df_long$Position <- as.numeric(gsub("X", "", df_long$Position))

gr.snp <- GRanges(
    seqnames = as.factor(df_long$Chromosome),
    ranges = IRanges(start = df_long$Position, end = df_long$Position),
    ConversionRate = df_long$ConversionRate
)

head(gr.snp)

plot <- plotGrandLinear(gr.snp, aes(y = ConversionRate), geom = "point")

plot_name <- file.path(output_dir, paste0("plot_", basename(file), ".png"))
ggsave(plot_name, plot, width = 10, height = 6, dpi = 300)

}

Problem

Error: Error in ggsave(plot_name, plot, width = 10, height = 6, dpi = 300) : plot should be a ggplot2 plot or tracks object

But isn't it true that plotGrandLinear will return a ggplot object?

Solution

library(GenomicRanges) library(ggbio) library(readr) library(tidyr) library(dplyr)

input_dir = "/mnt/d/NGS/rna-seq/20240410/11_plot_prep" output_dir = "/mnt/d/NGS/rna-seq/20240410/13_manhattan_plot/ggbio" input_files <- list.files(path = input_dir, pattern = "\_transpose.csv$", full.names = TRUE)

for (file in input_files) { df <- read_csv(file, col_names = FALSE)

head(df)

colnames(df)[1] <- "Chromosome"

df_long <- df %>%
    pivot_longer(cols = -Chromosome, 
           names_to = "Position", 
           values_to = "ConversionRate") %>%
    drop_na(ConversionRate)

df_long$Position <- as.numeric(gsub("X", "", df_long$Position))

gr.snp <- GRanges(
    seqnames = as.factor(df_long$Chromosome),
    ranges = IRanges(start = df_long$Position, end = df_long$Position),
    ConversionRate = df_long$ConversionRate
)

head(gr.snp)

plotGrandLinear(gr.snp, aes(y = ConversionRate), geom = "point")

}

Problem

It runs (please see the attached for the log. But the plot photos are nowhere to be found, including the input_dir, output_dir and the working directory.

Request

Please help me with this. I would really appreciate it.

Albus log.txt

mvolar commented 5 months ago

Hi, im not in any way included in the development of GGbio, and was also extremely annoyed by the fact that ggbio::ggsave() doesn't work in any shape or form, however you can hack your way around using ggplot2 save command:

#save your plotGrandLinear to a ggbio::GGbio S4 object
p <- plotGrandLinear(gr, aes(y = N_scale))

#call the ggplot2 ggsave() explicitly on the ggbio plot S4 object by accessing the S3 ggplot slot using the @ operator, 
ggplot2::ggsave("test.svg",p@ggplot,units = "px",
       height = 1000,width = 1000)

For me this command produced the following plot and saved it as "test.svg" to my file path. test

albustruong commented 5 months ago

Hi @mvolar,

Thank you so much!!! It can generate the figures for me now although I believe there are still a lot of things to fix. I attached one of the figures for your reference.

New script is a follows

library(GenomicRanges) library(ggbio) library(readr) library(tidyr) library(dplyr) library(ggplot2)

input_dir = "/mnt/d/NGS/rna-seq/20240410/11_plot_prep" output_dir = "/mnt/d/NGS/rna-seq/20240410/13_manhattan_plot/ggbio" input_files <- list.files(path = input_dir, pattern = "\_plot.csv$", full.names = TRUE)

for (file in input_files) { df <- read_csv(file, col_names = FALSE)

head(df)

colnames(df)[1] <- "Chromosome"

df_long <- df %>%
    pivot_longer(cols = -Chromosome, 
           names_to = "Position", 
           values_to = "ConversionRate") %>%
    drop_na(ConversionRate)

df_long$Position <- as.numeric(gsub("X", "", df_long$Position))

gr.snp <- GRanges(
    seqnames = as.factor(df_long$Chromosome),
    ranges = IRanges(start = df_long$Position, end = df_long$Position),
    ConversionRate = df_long$ConversionRate
)

head(gr.snp)

p <- plotGrandLinear(gr.snp, aes(y = ConversionRate), geom = "point")

plot_name <- file.path(output_dir, paste0("plot_", basename(file), ".png"))
ggplot2::ggsave(plot_name, p@ggplot, units = "in", height = 5, width = 5)

} plot_Ca13mCh_plot csv