View source: R/visualize_fava.R
plot_relabund | R Documentation |
This function enables graphical visualization of a matrix of compostional data. In the output plot, each vertical bar represents a single vector; the height of each color in the bar corresponds to the abundance of each category in that vector. Because this function produces a ggplot object, its output can be modified using standard ggplot2 syntax.
plot_relabund(
relab_matrix,
group = NULL,
time = NULL,
w = NULL,
K = NULL,
arrange = FALSE
)
relab_matrix |
A matrix or data frame with rows containing non-negative entries that sum to 1. Each row represents
a sample, each column represents a category, and each entry represents the abundance of that category in the sample.
If |
group |
Optional; a string specifying the name of the column that describes which group each row (sample) belongs to. Use if |
time |
Optional; a string specifying the name of the column that describes the sampling time for each row. Include if you wish to weight FAVA by the distance between samples. |
w |
Optional; a vector of length |
K |
Optional; an integer specifying the number of categories in the data. Default is |
arrange |
Optional; controls horizontal ordering of samples and vertical ordering of categories.
If |
A ggplot object containing a bar plot visualization of the relative abundance matrix.
# Make an example matrix of compositional data
# Each row is an individual. Rows sum to 1.
population_A = matrix(c(
.5, .3, .2,
.4, .2, .4,
.5, .4, .1,
.6, .1, .3,
.2, 0, .8
),
nrow = 5,
byrow = TRUE
)
plot_relabund(relab_matrix = population_A,
K = 3, # How many categories per vector?
arrange = FALSE
)
plot_relabund(relab_matrix = population_A,
K = 3, # How many categories per vector?
arrange = "horizontal"
)
plot_relabund(relab_matrix = population_A,
K = 3, # How many categories per vector?
arrange = "vertical"
)
plot_relabund(relab_matrix = population_A,
K = 3, # How many categories per vector?
arrange = TRUE # could also be "both"
)
# You can modify the plot as you would any ggplot2 object
plot_relabund(relab_matrix = population_A,
K = 3, # How many categories per vector?
arrange = TRUE
) +
# Below are example, optional modifications to the default plot
ggplot2::ggtitle("Population A") +
ggplot2::scale_fill_brewer("Blues") +
ggplot2::scale_color_brewer("Blues") +
ggplot2::xlab("Individuals")
# Note that both scale_fill and scale_color are needed to change the color of the bars.
# Plot a dataset which has 2 populations
population_B = matrix(c(
.9, 0, .1,
.6, .4, 0,
.7, 0, .3,
.3, .4, .3,
.5, .3, .2
),
nrow = 5,
byrow = TRUE
)
populations_AB = cbind(data.frame(c("A", "A", "A", "A", "A",
"B", "B", "B", "B", "B")),
rbind(population_A, population_B))
colnames(populations_AB) = c("population", "category_1", "category_2", "category_3")
plot_relabund(relab_matrix = populations_AB, group = "population")
plot_relabund(relab_matrix = populations_AB, group = "population", arrange = "vertical")
plot_relabund(relab_matrix = populations_AB, group = "population", arrange = "horizontal")
plot_relabund(relab_matrix = populations_AB, group = "population", arrange = "both")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.