The ggacmap
function is written by ggplot2
and take the advantages of using scale
or theme
to modify the details.
Loading the packages
## Need help getting started? Try the cookbook for R:
## http://www.cookbook-r.com/Graphs/
Load the H3 AC map
Plotting examples
Plot the map is easy, just using ggacmap(your_acmap)
:
Show color legend
Oops, this is not what we want. ggplot2
does the right thing as we map the color to the actual color vector.
How if we want to label these groups with representative strains?
## get the first strain of each group
cols <- agFill(h3map)
i <- which(!duplicated(cols))
strain <- rownames(agCoords(h3map))[i]
print(strain)
## [1] "BI/15793/68" "BI/21793/72" "VI/3/75" "BI/628/76" "BA/1/79"
## [6] "SH/11/87" "BE/352/89" "BE/32/92" "MA/G252/93" "AU/10/97"
## [11] "FU/411/02"
## [1] "BI/68" "BI/72" "VI/75" "BI/76" "BA/79" "SH/87" "BE/89"
## [8] "BE/92" "MA/G252" "AU/97" "FU/02"
### Changing the label of color legend
p + scale_color_identity("Antigenic group",
labels = sstrain,
breaks = cols[i],
guide = "legend")
Bonus: easy to put several figures in one page
The following example build a quick-and-dirty tree from the aligned sequences, and visualize the tree using ggtree with tip points colored by antigenic grouping information. We can therefore check whether the antigenic group information is consistent with genetic distance or not.
seq_file <- system.file("extdata/Day2/files/h3_2004/sequences.csv", package="ggacmap")
sequence <- read.csv(seq_file,
colClasses='character',
row.names=1,
check.names = FALSE,
stringsAsFactors=FALSE)
aaseq <- apply(sequence, 1, paste0, collapse='')
aaseq <- Biostrings::AAStringSet(aaseq)
tree <- ape::bionj(Biostrings::stringDist(aaseq, method='hamming'))
d <- data.frame(label = rownames(agCoords(h3map)),
color = agFill(h3map)
)
require(ggtree)
## Loading required package: ggtree
## Registered S3 method overwritten by 'treeio':
## method from
## root.phylo ape
## ggtree v1.16.1 For help: https://yulab-smu.github.io/treedata-book/
##
## If you use ggtree in published research, please cite the most appropriate paper(s):
##
## [36m-[39m Guangchuang Yu, Tommy Tsan-Yuk Lam, Huachen Zhu, Yi Guan. Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution 2018, accepted. doi: 10.1093/molbev/msy194[36m-[39m Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution 2017, 8(1):28-36, doi:10.1111/2041-210X.12628
p2 <- ggtree(tree) %<+% d + geom_point(aes(color = I(color)))
cowplot::plot_grid(p2, ggacmap(h3map), ncol=2, rel_widths=c(1, .4))