library(ggplot2)
library(tidyr)

sp = read.csv("common_splicing.csv", header=TRUE, sep=,)
head(sp)
##   GeneName    Q2    Q3    Q5  NO14  NO16  NO20  NO23   NO3   NO4   NO5
## 1   HP1BP3 41.41 22.58 33.33  7.50  4.65  9.62  9.86  5.00 12.82  3.57
## 2      AK2 50.00 54.17 43.90 85.00 88.89 73.91 81.08 80.56 80.00 72.41
## 3   SERBP1 78.79 42.86 62.50 20.00 15.38 31.58 22.73 24.00 15.00  6.45
## 4     IDI1 66.67 62.12 56.76 30.95 38.00 55.81 48.08 27.94 28.57 26.79
## 5    CELF2 55.10 33.33 70.00 88.89 88.46 85.00 83.87 79.17 90.00 92.86
## 6 RNASEH2C 39.06 37.50 49.33 74.39 89.62 56.94 73.45 66.67 76.56 56.25
##     NO6   NO8
## 1  5.26  6.06
## 2 84.62 90.00
## 3 28.57 16.00
## 4 47.73 22.03
## 5 88.24 94.74
## 6 77.78 87.60
df_sp = sp %>% gather(sample, value, -GeneName)
head(df_sp)
##   GeneName sample value
## 1   HP1BP3     Q2 41.41
## 2      AK2     Q2 50.00
## 3   SERBP1     Q2 78.79
## 4     IDI1     Q2 66.67
## 5    CELF2     Q2 55.10
## 6 RNASEH2C     Q2 39.06
ggplot(df_sp, aes(x = sample, y = GeneName, fill = value)) + geom_tile()

**tidy it reshape2 package
library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
dd = melt(sp, id.vars = "GeneName")
head(dd)
##   GeneName variable value
## 1   HP1BP3       Q2 41.41
## 2      AK2       Q2 50.00
## 3   SERBP1       Q2 78.79
## 4     IDI1       Q2 66.67
## 5    CELF2       Q2 55.10
## 6 RNASEH2C       Q2 39.06
heatmap
ggplot(df_sp, aes(x = sample, y = GeneName, fill = value)) + geom_tile() + scale_fill_gradient(low = "red", high = "blue") + ylab("GeneName") + xlab("patients")

ggplot(df_sp, aes(x = sample, y = GeneName, fill = value)) + geom_tile() + xlab("patients") + theme(legend.title = element_text(size = 10), plot.title = element_text(size = 14), axis.title = element_text(size = 10, face = "bold"), axis.text.y = element_text(size = 8), axis.text.x = element_text(angle = 90, hjust = 1)) + labs(fill = "psi%")

psi barplot
p = read.csv("common_psi.csv", header=TRUE, sep=,)
p$indx = row.names(p)

ggplot(p, aes(x=indx, y=psi)) + geom_bar(stat="identity")

ggplot(p, aes(x=indx, y=psi)) + geom_bar(stat="identity") +theme(axis.text.x = element_blank(), axis.title.x = element_blank(), axis.ticks.x = element_blank())

ggplot(p, aes(x=indx, y=psi)) + coord_flip() + geom_bar(stat="identity") +theme(axis.text.x = element_blank(), axis.title.x = element_blank(), axis.ticks.x = element_blank())

ggplot(p, aes(x=indx, y=psi)) + coord_flip() + geom_bar(stat="identity") +theme(axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank())

put together heatmap and barplot
library(gridExtra)
library(grid)
library(cowplot)
## 
## Attaching package: 'cowplot'
## The following object is masked from 'package:ggplot2':
## 
##     ggsave
hm1 = ggplot(df_sp, aes(x = sample, y = GeneName, fill = value)) + geom_tile() + scale_fill_gradient(low = "blue", high = "red") + theme(legend.title = element_text(size = 10), legend.text = element_text(size = 8), plot.title = element_text(size = 14), axis.ticks.y = element_blank(), axis.text.y = element_text(size = 8), axis.title.x = element_blank(), axis.title.y = element_blank()) + labs(fill = "psi%")


hm1.x.title.90 = ggplot(df_sp, aes(x = sample, y = GeneName, fill = value)) + geom_tile() + scale_fill_gradient(low = "blue", high = "red") + theme(legend.title = element_text(size = 10), legend.text = element_text(size = 8), plot.title = element_text(size = 14), axis.ticks.y = element_blank(), axis.text.y = element_text(size = 8), axis.text.x = element_text(angle = 90, hjust = 1), axis.title.x = element_blank(), axis.title.y = element_blank()) + labs(fill = "psi%")


bar1 = ggplot(p, aes(x=indx, y=psi)) + coord_flip() + geom_bar(stat="identity") + theme(axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank(), axis.title.x = element_blank())

grid.arrange(hm1, bar1, nrow = 1, ncol = 2, widths = c(40,10))

tmp = ggplot_gtable(ggplot_build(hm1))
leg = which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend = tmp$grobs[[leg]]

hm1.no.legend = hm1 + theme(legend.position = "none")

grid.arrange(hm1.no.legend, bar1, legend, nrow = 1, ncol = 3, widths = c(40,10, 5))

fpkm = read.csv("common_fpkm.csv", header=TRUE, sep=,)

df_fpkm = fpkm %>% gather(sample, value, -GeneName)
hm2 = ggplot(df_fpkm, aes(x = sample, y = GeneName, fill = value)) + geom_tile() + scale_fill_gradient(low = "blue", high = "red") + theme(legend.title = element_text(size = 10), legend.text = element_text(size = 8), plot.title = element_text(size = 14), axis.ticks.y = element_blank(), axis.text.y = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank()) + labs(fill = "fpkm")

tmp = ggplot_gtable(ggplot_build(hm2))
leg = which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend2 = tmp$grobs[[leg]]

hm2.no.legend = hm2 + theme(legend.position = "none")

grid.arrange(hm2.no.legend, hm1.no.legend, bar1, legend2, legend, nrow = 1, ncol = 5, widths = c(40, 40, 10, 5, 5))

grouping GeneName
df2 = data.frame(type = c("Alternative Acceptor", "Alternative Donor", "Cassette"), len = c(14, 13, 110), sub = c(1, 1, 1))
df2
##                   type len sub
## 1 Alternative Acceptor  14   1
## 2    Alternative Donor  13   1
## 3             Cassette 110   1
gp = ggplot(df2, aes(x=sub, y=len, fill=type)) + geom_bar(stat="identity") +theme(axis.text.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank())

gp

tmp = ggplot_gtable(ggplot_build(gp))
leg = which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legendT = tmp$grobs[[leg]]

gp.no.legend = gp + theme(legend.position = "none")
gp.no.legend

grid.arrange(gp.no.legend, hm2.no.legend, hm1.no.legend, bar1, legend2, legend, legendT, nrow = 1, ncol = 7, widths = c(5, 40, 40, 10, 5, 5, 5))

How to force specific order of the variables on the Y axis using ggplot2

Reorder the levels in order of first appearance

library(forcats)

df3 = data.frame(GeneName = fpkm$GeneName, sample = c(rep("G", 137)), value = c(rep(10, 14), rep(50, 13), rep(100, 110)), idx = c(1:137))

bar2 = ggplot(df3, aes(x = sample, y = fct_reorder(GeneName, idx, .desc = TRUE), fill = value)) + geom_tile() + scale_fill_gradient(low = "yellow", high = "green") + theme(axis.text.y = element_blank(), axis.ticks.y = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.ticks.x = element_blank(), legend.position = "none")

bar2