-
Notifications
You must be signed in to change notification settings - Fork 0
/
ortho.R
89 lines (75 loc) · 4.33 KB
/
ortho.R
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#Plot BUSCO with GGPLOT2
# Load libraries
library(ggplot2)
library("grid")
# Output
my_output <- paste("busco_figure.svg")
my_width <- 20
my_height <- 15
my_unit <- "cm"
# Colors
my_colors <- c("#56B4E9", "#3492C7", "#F0E442", "#F04442")
# Bar height ratio
my_bar_height <- 0.75
# Legend
my_title <- "Actinopterigi core orthologs"
# Font
my_family <- "sans"
my_size_ratio <- 1
# !!! SEE YOUR DATA HERE !!!
# Your data as generated by python, remove or add more
my_species <- c('Cvariegatus', 'Cvariegatus', 'Cvariegatus', 'Cvariegatus', 'Cnevadensis', 'Cnevadensis', 'Cnevadensis', 'Cnevadensis', 'Ctularosa', 'Ctularosa', 'Ctularosa', 'Ctularosa', 'Alinmaeus', 'Alinmaeus', 'Alinmaeus', 'Alinmaeus', 'Preticulata', 'Preticulata', 'Preticulata', 'Preticulata', 'Maculatus', 'Maculatus', 'Maculatus', 'Maculatus')
my_species <- factor(my_species)
my_species <- factor(my_species,levels(my_species)[c(length(levels(my_species)):1)]) # reorder your species here just by changing the values in the vector :
my_percentage <- c(93.6, 1.4, 1.1, 3.9, 70.2, 0.6, 8.8, 20.4, 94.8, 0.7, 1.0, 3.5, 91.6, 1.0, 2.0, 5.4, 94.8, 0.9, 0.9, 3.4, 96.7, 0.8, 0.4, 2.1)
my_values <- c(3406, 52, 39, 143, 2554, 21, 322, 743, 3452, 26, 35, 127, 3335, 36, 74, 195, 3449, 31, 32, 128, 3519, 28, 13, 80)
######################################
######################################
######################################
# Code to produce the graph
labsize = 1
if (length(levels(my_species)) > 10){
labsize = 0.66
}
print("Plotting the figure ...")
category <- c(rep(c("S","D","F","M"),c(1)))
category <-factor(category)
category = factor(category,levels(category)[c(4,1,2,3)])
df = data.frame(my_species,my_percentage,my_values,category)
figure <- ggplot() +
geom_bar(aes(y = my_percentage, x = my_species, fill = category), position = position_stack(reverse = TRUE), data = df, stat="identity", width=my_bar_height) +
coord_flip() +
theme_gray(base_size = 8) +
scale_y_continuous(labels = c("0","20","40","60","80","100"), breaks = c(0,20,40,60,80,100)) +
scale_fill_manual(values = my_colors,labels =c(" Complete (C) and single-copy (S) ",
" Complete (C) and duplicated (D)",
" Fragmented (F) ",
" Missing (M)")) +
ggtitle(my_title) +
xlab("") +
ylab("\n%BUSCOs") +
theme(plot.title = element_text(family=my_family, hjust=0.5, colour = "black", size = rel(2.2)*my_size_ratio, face = "bold")) +
theme(legend.position="top",legend.title = element_blank()) +
theme(legend.text = element_text(family=my_family, size = rel(1.2)*my_size_ratio)) +
theme(panel.background = element_rect(color="#FFFFFF", fill="white")) +
theme(panel.grid.minor = element_blank()) +
theme(panel.grid.major = element_blank()) +
theme(axis.text.y = element_text(family=my_family, colour = "black", size = rel(1.66)*my_size_ratio)) +
theme(axis.text.x = element_text(family=my_family, colour = "black", size = rel(1.66)*my_size_ratio)) +
theme(axis.line = element_line(size=1*my_size_ratio, colour = "black")) +
theme(axis.ticks.length = unit(.85, "cm")) +
theme(axis.ticks.y = element_line(colour="white", size = 0)) +
theme(axis.ticks.x = element_line(colour="#222222")) +
theme(axis.ticks.length = unit(0.4, "cm")) +
theme(axis.title.x = element_text(family=my_family, size=rel(1.2)*my_size_ratio)) +
guides(fill = guide_legend(override.aes = list(colour = NULL))) +
guides(fill=guide_legend(nrow=2,byrow=TRUE))
for(i in rev(c(1:length(levels(my_species))))){
detailed_values <- my_values[my_species==my_species[my_species==levels(my_species)[i]]]
total_buscos <- sum(detailed_values)
figure <- figure +
annotate("text", label=paste("C:", detailed_values[1] + detailed_values[2], " [S:", detailed_values[1], ", D:", detailed_values[2], "], F:", detailed_values[3], ", M:", detailed_values[4], ", n:", total_buscos, sep=""),
y=3, x = i, size = labsize*4*my_size_ratio, colour = "black", hjust=0, family=my_family)
}
ggsave(figure, file=my_output, device="svg", width = my_width, height = my_height, unit = my_unit)
print("Done")