Recipes Docs

Abundances

Some convenience plotting types are available using MicrobiomePlots, Which also re-exports everything in StatsPlots

using Microbiome
using MicrobiomePlots
using Distributions

# add some high abundance bugs to be a bit more realistic
function spikein(spikes, y, x)
    m = rand(LogNormal(), y, x)
    for s in spikes
        m[s, :] = rand(LogNormal(3., 0.5), x)'
    end
    return m
end

# 100 species in 10 samples, with every 10th bug a bit more abundant
bugs = spikein(1:10:100, 100, 10)

abund = abundancetable(bugs,
    ["sample_$x" for x in 1:10],
    ["species_$x" for x in 1:100]);

relativeabundance!(abund)
abundanceplot(abund, xticks=(1:10, samplenames(abund)), xrotation=45)
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT

"first abundance plot"

Metadata

Perhaps you have some metadata that you'd like to add as well:

labels = ["a","a","b","a","b","b","b","b","a","a"]

plot(
    abundanceplot(abund, xticks=(1:10, samplenames(abund)), xrotation=45),
    plot(annotationbar(labels)),
    layout=grid(2,1, heights=[0.9,0.1]))
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT
GKS: String is too long in routine TEXT

"annotated abundance plot"

Distances

To plot this, use the MDS or PCA implementations from MultivariateStats and plotting functionality from StatsPlots.

using MultivariateStats
using Distances

# Note - once [SpatialEcology #36](https://github.com/EcoJulia/SpatialEcology.jl/pull/36)
# is released, one will be able to do `pairwise(BrayCurtis(), abund)` directly
dm = pairwise(BrayCurtis(), occurrences(abund))

mds = fit(MDS, dm, distances=true)

plot(mds)
-0.2 -0.1 0.0 0.1 0.2 -0.20 -0.15 -0.10 -0.05 0.00 0.05 MDS1 MDS2 y1

Optimal Leaf Ordering

I also wrote a plotting recipe for making treeplots for Hclust objects from the Clustering.jl package, and the recipe for plotting was moved into StatsPlots:

using Clustering
using Distances
using MicrobiomePlots
using Random

n = 40

mat = zeros(Int, n, n)
# create banded matrix
for i in 1:n
    last = minimum([i+Int(floor(n/5)), n])
    for j in i:last
        mat[i,j] = 1
    end
end

# randomize order
mat = mat[:, randperm(n)]
dm = pairwise(Euclidean(), mat, dims=2)

# normal ordering
hcl1 = hclust(dm, linkage=:average)
plot(
    plot(hcl1, xticks=false),
    heatmap(mat[:, hcl1.order], colorbar=false, xticks=(1:n, ["$i" for i in hcl1.order])),
    layout=grid(2,1, heights=[0.2,0.8])
    )
0 5 10 15 4 40 26 37 6 15 20 8 33 12 21 30 31 28 10 27 1 17 24 32 39 3 14 9 16 23 36 2 18 22 13 35 38 7 25 11 29 34 5 19 10 20 30 40

Compare to:

# optimal ordering
hcl2 = hclust(dm, linkage=:average, branchorder=:optimal)
plot(
    plot(hcl2, xticks=false),
    heatmap(mat[:, hcl2.order], colorbar=false, xticks=(1:n, ["$i" for i in hcl2.order])),
    layout=grid(2,1, heights=[0.2,0.8])
    )
0 5 10 15 39 14 3 36 23 9 16 13 35 22 18 2 25 7 38 11 29 19 5 34 12 21 31 30 17 1 24 32 28 27 10 40 4 37 26 6 15 33 8 20 10 20 30 40
MicrobiomePlots.annotationbar — Method
annotationbar(colors::AbstractArray{<:Color,1})
annotationbar(labels::AbstractArray{<:AbstractString,1})
annotationbar(labels::AbstractArray{<:AbstractString,1}, colormap::Dict{<:AbstractString,<:Color})
annotationbar(labels::AbstractArray{<:AbstractString,1}, colors::AbstractArray{<:Color,1})

Create an AnnotationBar from a vector of colors or a vector of labels (Strings).

  • A vector of Colors will be used directly to create an AnnotationBar
  • For an array of Strings, each unique value will be assigned a random color from Colors.color_names.
  • A vector of labels may be passed with a Dict with keys for each label mapping to a Color value.
  • A vector of labels and colors may be used.
    • If the lengths of these vectors are the same, the colors will be used to create an AnnotationBar
    • If the length of labels is longer than the length of colors, each unique label will be assigned to one of the colors
source