-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgridarrange.R
More file actions
executable file
·62 lines (43 loc) · 1.86 KB
/
gridarrange.R
File metadata and controls
executable file
·62 lines (43 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/env Rscript
## Load packages
library(ggplot2)
library(gridExtra)
##Create Fake Data for the eventual plots
gene=c("Abc", "def", "ghi")
data1=as.data.frame(matrix(data = NA, nrow = 20, ncol = 3))
names(data1)=c("Sample", "Gene", "Pct")
data1$Sample=sample(c("Pop1", "Pop2", "Pop3"),replace=T,20)
data1$Gene=sample(gene, replace=T, 20)
data1$Pct=rnorm(20, mean = 0, sd=1)
## Create Plots
one=ggplot(data = data1, mapping = aes(x=Sample, y=Pct, fill=Gene))+
geom_bar(stat = "identity", position = "dodge")+
scale_fill_manual("legend", values = c("A" = "green3", "B" = "blue2", "C" = "green3"))+ggtitle(label = "Plot 1")
two=ggplot(data = data1, mapping = aes(x=Sample, y=Pct, fill=Gene))+
geom_point()+
scale_fill_manual("legend", values = c("A" = "lightblue", "B" = "blue2", "C" = "green3"))+ggtitle(label = "Plot 2")
three=ggplot(data = data1, mapping = aes(x=Sample, y=Pct, fill=Gene, col="green"))+
geom_bar(stat = "identity", position = "dodge")+
ggtitle(label = "Plot 3")
four=ggplot(data = data1, mapping = aes(x=Sample, y=Pct, fill=Gene))+
geom_jitter(shape=2)+
ggtitle(label = "Plot 4")
## Arrange using grid.arrange function from grid.extra package
## Use "widths" to define how many partitions to create within the matrix
## Use layout_matrix to define where the plots go. They will be ordered numbers
## the same way as they are input as ggplot objects.
grid.arrange(one,two,three,four,
widths = c(1,1, 1, 1),
layout_matrix = rbind(c(1, 1, 1,2),
c(3, 3,4, 2))
)
grid.arrange(one,two,three,four,
widths = c(2,1, 1),
layout_matrix = rbind(c(1, 1, NA),
c(2,3,4))
)
## Can also just simply define number of rows and columns if you want to be uniform
grid.arrange(one,two,three,four,
ncol = 2,
nrow = 2
)