-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrepare_Figure_2.R
More file actions
executable file
·135 lines (106 loc) · 4.81 KB
/
Copy pathPrepare_Figure_2.R
File metadata and controls
executable file
·135 lines (106 loc) · 4.81 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/local/bin/Rscript
#Prepares four bar plots from a combined metadata file in preparation for paper
#The metadata file must contain the fields: country, date collected,
#host variety and source
#Import required libraries
library("ggplot2")
library("optparse")
library("gridExtra")
library("dplyr")
library("stringr")
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")
library("rgeos")
library("extrafont")
#When using extrafont for the first time
#run font_import() and loadfonts() in an interactive R session
#Parse command line arguments
opt_list <- list(
make_option("--metadata_all", type = "character",
help = "tab separated file of all metadata used to
build expression browser"),
make_option("--variety_count", type = "character",
help = "tab separated file of the number of confirmed host
varities in each country"),
make_option("--out", type = "character", help = "output plot in pdf format")
)
opt <- parse_args(OptionParser(option_list = opt_list))
f_all <- opt$metadata_all
f_variety <- opt$variety_count
o <- opt$out
#Create dataframe of metadata
metadata_all <- read.delim(f_all,
header = TRUE, stringsAsFactors = FALSE, quote = "", sep = "\t")
metadata_field <- metadata_all %>% filter(Source == "Field")
variety_count <- read.delim(f_variety, header = TRUE, stringsAsFactors = FALSE,
quote = "", sep = "\t")
#Prepare country plot
#Pull in country information
world <- ne_countries(scale = "medium", returnclass = "sf")
world_mod <- subset(world, name_long != "Antarctica")
#Prepare country data
countries <- count(metadata_field, country)
countries_merged <- merge(x = world_mod, y = countries,
by.x = "name_long", by.y = "country", all.x = TRUE)
#Prepare map
country_plot <- ggplot(data = countries_merged) +
geom_sf(aes(fill = n), size = 0.2) +
scale_fill_viridis_c(option = "viridis", name = "Number of
Samples", direction = -1, na.value = "white") + labs(tag = "A") +
theme(panel.grid.major = element_blank(), panel.grid.minor =
element_blank(), panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill = NA, size =
1), axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_text(family = "Arial", size = 7),
legend.text = element_text(family = "Arial", size = 7),
legend.title = element_text(family = "Arial", size = 7),
plot.tag = element_text(family = "Arial", size = 10))
#Prepare variety plot
variety_plot <- ggplot(variety_count,
aes(x = reorder(Variety, -Count), y = Count)) +
xlab("Wheat Variety") + ylab("Frequency") +
geom_bar(stat = "identity", fill = "navy") + labs(tag = "D") +
theme(panel.grid.major = element_blank(), panel.grid.minor =
element_blank(), panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill = NA, size =
1), axis.text = element_text(family = "Arial", size = 6, colour = "black"),
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
axis.title = element_text(family = "Arial", size = 7),
plot.tag = element_text(family = "Arial", size = 10))
#Prepare dates plot
dates <- as.data.frame(metadata_field$Date.Isolated)
split_dates <- as.data.frame(str_split_fixed(
as.character(dates$"metadata_field$Date.Isolated"), "/", 3))
split_date_counts <- count(split_dates, V1)
reduced_count <- split_date_counts %>% filter(V1 >= 2013)
dates_plot <- ggplot(reduced_count, aes(x = V1, y = n)) +
xlab("Year Collected") + ylab("Frequency") +
geom_bar(stat = "identity", fill = "navy") + labs(tag = "B") +
theme(panel.grid.major = element_blank(), panel.grid.minor =
element_blank(), panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill = NA, size =
1), axis.text = element_text(family = "Arial", size = 6, colour = "black"),
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
axis.title = element_text(family = "Arial", size = 7),
plot.tag = element_text(family = "Arial", size = 10))
#Prepare sample plot
type_of_sample <- count(metadata_all, Source)
sample_plot <- ggplot(type_of_sample, aes(x = reorder(Source, -n), y = n)) +
xlab("Source of Sample") + ylab("Frequency") +
geom_bar(stat = "identity", fill = "navy") + labs(tag = "C") +
theme(panel.grid.major = element_blank(), panel.grid.minor =
element_blank(), panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill = NA, size =
1), axis.text = element_text(family = "Arial", size = 6, colour = "black"),
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
axis.title = element_text(family = "Arial", size = 7),
plot.tag = element_text(family = "Arial", size = 10))
#Save Plots
plot_list <- list(country_plot, dates_plot, variety_plot, sample_plot)
pdf(o, onefile = FALSE)
layout <- rbind(c(1, 1, 1, 1), c(1, 1, 1, 1), c(3, 3, 4, 4), c(2, 2, 2, 2))
grid.arrange(country_plot, variety_plot, dates_plot, sample_plot,
layout_matrix = layout)
dev.off()