Tutorial:Making Tile plots to show Synonymous Non-Synonymous mutation fraction
0
0
Entering edit mode
9 months ago

There are times when you have fractions across genomic ranges and you want to plot them. A line plot is fine for plotting fractions or absolute counts, but if the range is large, the per-base resolution is poor, and line plots look like spikes, as can be seen below:

The image is visually irritating to look at since the graph has three samples. This will be further complicated if you want to plot synonymous and non-synonymous together as a facet. I think a possible solution is to make tile graphs that not only cover less space but are also visually less frustrating to look at. Below is a code chunk to make such plots. Here for the first time, I used ggeasy and ggplotify

## Loading necessary packages

library(ggplot2)
library(ggplotify)
library(grid)
library(gridExtra)
library(ggeasy)
library(ggpubr)
set.seed(123)

## Creating dummy data on the fraction of mutations per base. Here we will use it for representing syn and non-syn mutation both.
df <- reshape2::melt(
data.frame(Pos=1:100,
S1 = runif(100, min = 0.0, max = 0.1),
S2=runif(100, min = 0.0, max = 0.1),
S3=runif(100, min = 0.0, max = 0.1)),id="Pos")

# Create tile plots for syn and non-syn fraction
syn <- ggplot(df, aes(x = Pos, y = variable, fill = value)) +
geom_tile() +
scale_fill_gradient(low = "#f7f6f9", high = "#4385bd") +
labs(x = "", y = "") +
theme_minimal() +
ggeasy::easy_remove_x_axis()+
theme(legend.position = "none") +
scale_x_continuous(breaks = seq(0, 100, by = 20), expand = c(0, 0))+
coord_fixed(ratio=4)+
theme(plot.title = element_text(size = 10))+
#ggtitle("Syn") + #uncomment if you wish to add title
ggeasy::easy_center_title()+
theme(plot.margin = unit(c(0, 5.5, 0.5, 2.5), "pt"))  #reducing the margins so that two plots can be as close as possible when using grid.arrange.

nonsyn <- ggplot(df, aes(x = Pos, y = variable, fill = value)) +
geom_tile() +
scale_fill_gradient(low = "#f7f6f9", high = "#fdb64c") +
labs(x = "", y = "") +
theme_minimal() +
theme(legend.position = "none") +
scale_x_continuous(breaks = seq(0, 100, by = 20), expand = c(0, 0))+
coord_fixed(ratio=4)+
theme(plot.title = element_text(size = 10))+
#ggtitle("Non-Syn") +
ggeasy::easy_center_title()+
theme(plot.margin = unit(c(0, 5.5, 0.5, 2.5), "pt"))

g2 <- ggplotGrob(syn)
g3 <- ggplotGrob(nonsyn)
g <- rbind(g2, g3, size = "first")
g$widths <- unit.pmax(g2$widths, g3\$widths)

yleft <- textGrob(paste("%age Major AF or Minor AF"),
rot = 90, gp = gpar(fontsize = 12))
bt<- textGrob("Nucleotide Position", gp=gpar(fontsize=12))

g1<-grid.arrange(g,g,left = yleft,bottom=bt)

## Converting back gg_table to ggplot object if you want to arrange multiple plots like above in a single image using
## ggpubr::ggarrange() function.
g1<-as.ggplot(grid.arrange(g,g,left = yleft,bottom=bt)) ## you can also use as_ggplot() function from ggpubr package
g2<-g1

ggarrange(g1,g2, ncol = 2, nrow=1, labels = c("A","B"))


Note: I thought it would be easy to alter theme and axis labels once you convert the gtable object to ggplot2 and I would be able to use the labs() function or xlab(). But for some reason (maybe because we club ), those functions do not work on the objects you make from gtable objects. I am no expert on grammar of graphics so if you know ways to restore full ggplot object functionality, let me know

mutation ggplotify geom_tile ggplot2 • 471 views