####################
#### CANADA MAP ####
####################
#meta_data = mtdt
#species_names = "Aspergillus_chevalieri"
mapCanada <- function(meta_data, species_names = NA, default_theme = TRUE, shift_dots = TRUE) {
### MAP DATA
# uploding shape file.
shp <- base::readRDS(system.file("extdata", "map_cnd.RData", package = "AODPvisual"))
cndshp <- shp$map
cndshp$Province <- factor(cndshp$id)
levels(cndshp$Province) <- c("AB", "BC", "MB", "NB", "NL", "NWT", "NS", "NU", "ON", "PEI", "QC", "SK", "YT")
### DATA TABLE FOR PLOT
dtst <- dplyr::left_join(meta_data[["AODP_match"]], meta_data[["Metadata"]], by = "ID") %>%
dplyr::left_join(meta_data[["Sample_totals"]], by = "ID") %>%
dplyr::group_by(Species, ID) %>%
dplyr::mutate(seq_spp = length(unique(seq_ID)), value = (length(unique(seq_ID)) / (unique(s_num)))) %>%
dplyr::filter(Province != "World")
map_dots <- dtst %>%
dplyr::group_by(Province,City) %>%
dplyr::summarise(value = (mean(value))) %>%
dplyr::inner_join(cndshp, by = "Province")
# Filtering by species
if(!is.na(species_names)) {
#dtst <- dplyr::filter(dtst, Species == species_names)
dtst <- dtst[base::grep(species_names, dtst$Species),]
}
if(base::nrow(dtst) == 0) {
stop("Provided sprcies name does not exist in data set! \n
Try encrease 'species_number' parameter in metaData.")
}
# Dots table
comMap <- dplyr::group_by(dtst, City,Substrate) %>%
dplyr::summarise(latitude = mean(lat), longitude = mean(long), valueC = (mean(value))) %>%
dplyr::filter(!is.na(longitude))
if(base::identical(TRUE, shift_dots)) {
### DOTS DATA
# Estimate shift matrix
val = length(unique(dtst$Substrate))
valS = (c(1:val)/(val/2))*(pi/2)
valC = (c(1:val)/(val/2))*(2*pi)
valLat <- matrix(c(sin(valS), cos(valC)), nrow = val, byrow = T)
valnm <- unique(dtst$Substrate)
dimnames(valLat) <- list(valnm, c("Ln", "Lt"))
comMap <- dplyr::group_by(comMap, Substrate) %>%
dplyr::mutate(latitude = latitude+valLat[as.character(Substrate),2], longitude = longitude+valLat[as.character(Substrate),1])
}
# Plot Colours
clr = c("#771155", "#AA4488", "#CC99BB", "#114477", "#4477AA", "#77AADD", "#117777", "#44AAAA", "#77CCCC", "#117744", "#44AA77", "#88CCAA", "#777711", "#AAAA44", "#DDDD77", "#774411", "#AA7744", "#DDAA77", "#771122", "#AA4455", "#DD7788")
clr = colorRampPalette(clr)(length(unique(dtst$Substrate)))
names(clr) = sort(unique(dtst$Substrate))
### PLOT ###
pp <- ggplot() +
geom_map(data = map_dots, map = map_dots, aes(x = long, y = lat, map_id = id, fill = value), colour = "black", size = 0.1) +
geom_point(data = comMap, aes(x = longitude, y = latitude, colour = Substrate, size = valueC), alpha = 0.8, shape = 19, inherit.aes = F) +
geom_point(data = comMap, aes(x = longitude, y = latitude, size = valueC),shape = 21, colour = "#525252", inherit.aes = F)
# DEFAULT THEME
if(base::identical(TRUE, default_theme)) {
pp <- pp + scale_fill_gradient(na.value = "white", low = "#fff7bc", high = "#fe9929", guide = guide_legend(title = "% of Genus")) +
scale_colour_manual(na.value = "black", guide = guide_legend(title = "Substrate",ncol = 2, override.aes = list(size = 12)), values = clr) +
scale_size(guide = guide_legend(title = paste("% of", species_names, sep = " ")), range = c(4,20)) +
theme(panel.background=element_rect(fill="#ffffff"),
legend.key = element_rect(fill = "white", color = "white"),
#plot.title = element_text(lineheight=.8, face="bold"),
#title=element_text(size=15,colour="black"),
strip.background = element_rect(colour = "white", fill = "#ffffff"),
strip.text = element_text(size = 12, face = "bold", hjust = 0),
#strip.background = element_rect(colour="black", fill="#ffffff"),
#axis.title.y=element_text(size=18,colour="black", angle = 90),
axis.title=element_text(size=0,colour="grey"),
axis.line = element_line(colour="grey"),
#axis.text.x = element_text(angle = 45),
axis.ticks = element_line(colour = "grey"),
#text=element_text(size=10,colour="black"),
panel.grid.major=element_line(size=0.5,colour="gray80",linetype = "dotted"),
panel.grid.minor=element_line(size=0.5,colour="gray80",linetype = "dotted"),
panel.border = element_rect(fill = NA, colour = "grey"),
#legend.position=c(1.2,0.7),
legend.position = "right",
legend.text = element_text(size = 12, colour="black"),
legend.title = element_text(size = 22),
axis.text = element_text(size = 14, colour = "grey", face = "italic"))
}
return(pp)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.