Skip to content

Commit 649e517

Browse files
committed
ts viz
1 parent 9d4b0f5 commit 649e517

File tree

103 files changed

+4546
-1640
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+4546
-1640
lines changed

.Rhistory

+495-495
Large diffs are not rendered by default.

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: microbiomeutilities
22
Type: Package
3-
Title: microbiomeutilities: An R package for utilities to guide in-depth marker gene amplicon data analysis
4-
Version: 1.00.08
3+
Title: microbiomeutilities: Utilities for Microbiome Analytics
4+
Version: 1.00.09
55
Authors@R: c(
66
person("Sudarshan", "Shetty", email = "[email protected]", role = c("aut", "cre")),
77
person("Leo", "Lahti", role = "aut"))

NAMESPACE

+4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ export(plasticity)
1414
export(plot_abund_prev)
1515
export(plot_alpha_diversities)
1616
export(plot_alpha_rcurve)
17+
export(plot_area)
1718
export(plot_diversity_stats)
1819
export(plot_listed_taxa)
1920
export(plot_ordination_utils)
2021
export(plot_ordiplot_core)
22+
export(plot_paired_abundances)
2123
export(plot_read_distribution)
2224
export(plot_select_taxa)
25+
export(plot_spaghetti)
2326
export(plot_taxa_boxplot)
2427
export(plot_taxa_composition)
2528
export(plot_taxa_cv)
@@ -55,3 +58,4 @@ importFrom(stats,median)
5558
importFrom(stats,quantile)
5659
importFrom(stats,sd)
5760
importFrom(tibble,rownames_to_column)
61+
importFrom(tidyr,pivot_longer)

R/data.R

+30
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,33 @@
2525
#' }
2626
#'
2727
"zackular2014"
28+
29+
#' Test data 2
30+
#'
31+
#' Data from a Stansfield J, Dozmorov M. "16s rRNA sequencing data
32+
#' from the Human Microbiome Project 2". Randomly choosen 13 particpants
33+
#' with multiple timepoints for rectum samples.
34+
#'
35+
#' @docType data
36+
#'
37+
#' @usage data("hmp2")
38+
#'
39+
#' @format An object of class \code{"phyloseq"}.
40+
#'
41+
#' @keywords datasets
42+
#' @references
43+
#' \itemize{
44+
#' \item{}{Stansfield J, Dozmorov M (2019). HMP2Data: 16s rRNA sequencing data
45+
#' from the Human Microbiome Project 2. R package version 1.1.0,
46+
#' \url{ https://bioconductor.org/packages/HMP2Data/}}
47+
#' }
48+
#' @examples
49+
#' \dontrun{
50+
#' library(microbiomeutilities)
51+
#' data("hmp2")
52+
#' pseq <- hmp2
53+
#' print(hmp2)
54+
#' }
55+
#'
56+
"hmp2"
57+

R/plot_area.R

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#' @title Area plot
2+
#' @description Create an area plot for longitudinal samples
3+
#' with \code{\link{ggplot2}} package.
4+
#' @param x \code{\link{phyloseq-class}} object.
5+
#' @param xvar Column name to plot on x-axis.
6+
#' @param facet.by Column with variable that has multiple measurements.
7+
#' @param fill.colors brewer.pal(6,"Paired"). Specify colors.
8+
#' @param abund.thres = 0.001 check \code{\link{microbiome}} package aggregate_rare function.
9+
#' @param prev.thres = 0.1 check \code{\link{microbiome}} package aggregate_rare function.
10+
#' @param level Taxonomic level. OTU/ASV level not supported.
11+
#' @param ncol wrap, specify number of columns.
12+
#' @param nrow wrap, specify number of rows.
13+
#' @return \code{\link{ggplot}} object.
14+
#' @export
15+
#' @examples
16+
#' \dontrun{
17+
#' library(microbiomeutilities)
18+
#' data("hmp2")
19+
#' ps <- hmp2
20+
#' ps.rel <- microbiome::transform(ps, "compositional")
21+
#' p <- plot_area(ps.rel, xvar="visit_number",
22+
#' level = "Phylum",
23+
#' facet.by = "subject_id",
24+
#' fill.colors=brewer.pal(6,"Paired"))
25+
#'}
26+
#'
27+
#' @keywords visualization
28+
#'
29+
plot_area <- function(x,
30+
xvar=NULL,
31+
level = NULL,
32+
facet.by = NULL,
33+
fill.colors=brewer.pal(6,"Paired"),
34+
abund.thres=0.001,
35+
prev.thres=0.5,
36+
ncol=5,
37+
nrow=5){
38+
39+
if(is.null(xvar)){
40+
stop("xvar cannot be empty")
41+
}
42+
if(is.null(facet.by)){
43+
stop("facet.by cannot be empty")
44+
}
45+
x.lev<-xdf<-comp.plt<-facet_var <- NULL
46+
x.lev <- aggregate_rare(x,
47+
level=level,
48+
detection = abund.thres,
49+
prevalence = prev.thres)
50+
51+
xdf <- phy_to_ldf(x.lev, transform.counts = NULL)
52+
xdf$facet_var <- xdf[,facet.by]
53+
comp.plt <- ggplot(xdf)
54+
comp.plt <- comp.plt +
55+
geom_area(aes_string(x = xvar,
56+
y = "Abundance",
57+
fill = level)) +
58+
facet_wrap(~facet_var,
59+
scales = "free",
60+
ncol = ncol,
61+
nrow = nrow) +
62+
scale_fill_manual("Taxa", values = fill.colors)
63+
return(comp.plt + theme_biome_utils() + ylab("Relative abundance"))
64+
65+
}

R/plot_listed_taxa.R

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,17 @@ plot_listed_taxa <- function(x,
5151
ncol = NULL,
5252
nrow = NULL) {
5353
x.rel <- x.prun <- x.df <- p.box <- p.vio <- p.strp <- NULL
54-
54+
55+
if(is.na(group) | is.null(group)) {
56+
stop(" 'group' argument cannot be empty")
57+
}
58+
5559
x.rel <- microbiome::transform(x, "compositional")
5660

5761
x.prun <- prune_taxa(select.taxa, x.rel)
5862

5963
x.df <- phy_to_ldf(x.prun, transform.counts = NULL)
64+
6065

6166
if (!is.null(group.order)) {
6267
x.df[, group] <- factor(x.df[, group],

R/plot_paired_abundances.R

+218
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
#' @title A paired-boxplot for user specified list of taxa
2+
#' @description User specified taxa are plotted.
3+
#' @details Useful for instances where user is interested only in some taxa and thier change after
4+
#' an intervention. This can also be used at higher taxonomic
5+
#' levels, output from phyloseq::tax_glom or microbiome::aggregate_taxa.
6+
#'
7+
#' @param x \code{\link{phyloseq-class}} object.
8+
#' @param select.taxa a character list of taxa to be plotted. eg. select.taxa <- c("OTU-370251", "OTU-311173", "OTU-341024").
9+
#' @param group Grouping variable to compare. x axis, eg. before-after, t1-t2.
10+
#' @param group.colors Colors for plotting groups.
11+
#' @param dot.opacity For ggplot alpha to determine opacity for points.
12+
#' @param dot.size For ggplot point size.
13+
#' @param jitter.width Value to avoid over plotting by moving points.
14+
#' @param add.box Logical. If boxplot to the added. Default=TRUE
15+
#' @param box.opacity For ggplot alpha to determine opacity for box.
16+
#' @param add.violin Logical. If half violin to the added. Default=TRUE
17+
#' @param violin.opacity If add.violin=TRUE, opacity for violin.
18+
#' @param group.order Default is NULL. a list specifying order of x-axis.
19+
#' @param ncol If 2 or more taxa to plot, specify number of columns.
20+
#' @param nrow If 2 or more taxa to plot, specify number of rows.
21+
#' @param line Variable to use for lines. E.g. "subject" before-after
22+
#' @param line.down Line Color for change when negative. Decreased abundance.
23+
#' @param line.stable Line Color for no change.
24+
#' @param line.up Line Color for change when positive. Increased abundance.
25+
#' @param line.na.value "grey50" for no/missing observations.
26+
#' @param line.guide "none" to plot guide for line.
27+
#' @param line.opacity Line opacity.
28+
#' @param line.size Size of line to plot.
29+
#'
30+
#' @return \code{\link{ggplot}} object. This can be further modified using ggpubr.
31+
#' @importFrom tidyr pivot_longer
32+
#' @export
33+
#' @examples
34+
#' \dontrun{
35+
#' library(microbiome)
36+
#' library(microbiomeutilities)
37+
#' library(gghalves)
38+
#' library(tidyr)
39+
#' data(peerj32) # Source: https://peerj.com/articles/32/
40+
#' pseq <- peerj32$phyloseq # Ren
41+
#' pseq.rel <- microbiome::transform(pseq, "compositional")
42+
#' select.taxa <- c("Akkermansia", "Dialister")
43+
#' group.colors <- c("brown3", "steelblue", "grey70")
44+
#' p <- plot_paired_abundances(pseq.rel,
45+
#' select.taxa = select.taxa,
46+
#' group = "time",
47+
#' group.colors = group.colors,
48+
#' dot.opacity = 0.25,
49+
#' dot.size = 2,
50+
#' group.order = NULL,
51+
#' line = "subject"
52+
#' )
53+
#' p
54+
#' }
55+
#' @keywords visualization
56+
57+
plot_paired_abundances <- function(x,
58+
select.taxa = NULL,
59+
group = NULL,
60+
group.colors = NULL,
61+
dot.opacity = 0.25,
62+
dot.size = 2,
63+
add.box = FALSE,
64+
box.opacity = 0.25,
65+
group.order = NULL,
66+
add.violin = TRUE,
67+
violin.opacity = 0.25,
68+
ncol = NULL,
69+
nrow = NULL,
70+
line = NULL,
71+
line.down = "#7209b7",
72+
line.stable = "#8d99ae",
73+
line.up = "#14213d",
74+
line.na.value = "grey50",
75+
line.guide = "legend",
76+
line.opacity = 0.25,
77+
line.size = 1,
78+
jitter.width = 0) {
79+
Abundance <- change <- change.order <- change.sign <- linevar <- NULL
80+
# check arguments
81+
if (is.na(line) | is.null(line)) {
82+
stop(" 'line' argument cannot be empty")
83+
}
84+
85+
if (is.na(group) | is.null(group)) {
86+
stop(" 'group' argument cannot be empty")
87+
}
88+
#x <- pseq.rel
89+
xmeta <- meta(x)
90+
for (i in select.taxa) {
91+
df.tx <- as.data.frame(abundances(x)[i, ])
92+
colnames(df.tx) <- i
93+
xmeta <- cbind(xmeta, df.tx)
94+
}
95+
96+
if(length(unique(xmeta[, group])) > 2){
97+
stop("Only two group comparison e.g. before n after")
98+
}
99+
100+
# check if factor else convert to factor
101+
if (!is.factor(xmeta[, group])) {
102+
xmeta$group <- factor(as.character(xmeta[, group]))
103+
}
104+
105+
# convert to wide format
106+
xmeta_lf <- xmeta %>%
107+
pivot_longer(
108+
cols = all_of(select.taxa),
109+
names_to = "taxa",
110+
values_to = "Abundance"
111+
)
112+
113+
xmeta_lf$linevar <- factor(xmeta_lf[[line]])
114+
115+
x.grp <- sym(group)
116+
117+
df2 <- suppressWarnings(xmeta_lf %>%
118+
arrange(taxa,linevar, !!x.grp) %>%
119+
group_by(taxa,linevar) %>%
120+
summarise(change = diff(Abundance)))
121+
122+
xmeta_lf_2 <- suppressWarnings(xmeta_lf %>%
123+
arrange(taxa,linevar, !!x.grp) %>%
124+
group_by(taxa,linevar))
125+
126+
xmeta_lf_2 <- xmeta_lf_2 %>%
127+
left_join(df2)
128+
129+
#xmeta_lf$change <- df2$change[match(xmeta_lf$linevar, df2$linevar)]
130+
131+
xmeta_lf_2$change.sign <- sign(xmeta_lf_2$change)
132+
133+
if (!is.null(group.order)) {
134+
xmeta_lf_2[, group] <- factor(xmeta_lf_2[, group],
135+
levels = group.order
136+
)
137+
}
138+
xmeta_lf_2 <- xmeta_lf_2 %>%
139+
mutate(change.order = ifelse(change.sign == 1, "Up",
140+
ifelse(change.sign == -1, "Down",
141+
"Stable"
142+
)
143+
))
144+
# start plotting
145+
p <- ggplot(
146+
data = xmeta_lf_2,
147+
aes_string(
148+
x = "group",
149+
y = "Abundance",
150+
fill = "group"
151+
)
152+
) +
153+
geom_point(aes_string(
154+
x = "group",
155+
fill = "group"
156+
),
157+
position = position_jitter(width = jitter.width),
158+
size = dot.size,
159+
alpha = dot.opacity,
160+
shape = 21
161+
) +
162+
geom_line(aes(
163+
group = linevar,
164+
color = change.order
165+
),
166+
size = line.size,
167+
alpha = line.opacity
168+
#lty = line.type
169+
)
170+
171+
p <- p + scale_fill_manual(values = group.colors)
172+
173+
if (add.box == TRUE) {
174+
p <- p + geom_boxplot(
175+
width = 0.2,
176+
outlier.shape = NA,
177+
alpha = box.opacity
178+
)
179+
}
180+
181+
#geom_half_violin(
182+
# data = iris %>% filter(Species=="versicolor"),
183+
# aes(x = Species, y = Sepal.Length, fill = Species), side="r")
184+
if (add.violin == TRUE) {
185+
p <- p + geom_half_violin(data = xmeta_lf_2 %>%
186+
filter(group==unique(xmeta_lf_2$group)[1]),
187+
position = position_nudge(x = -0.15, y = 0),
188+
alpha = violin.opacity,
189+
side = "l"
190+
) +
191+
geom_half_violin(data = xmeta_lf_2 %>%
192+
filter(group==unique(xmeta_lf_2$group)[2]),
193+
position = position_nudge(x = 0.15, y = 0),
194+
alpha = violin.opacity,
195+
side = "r"
196+
)
197+
}
198+
199+
if (length(select.taxa) >= 2) {
200+
p <- p + facet_wrap(~taxa,
201+
scales = "free",
202+
ncol = ncol,
203+
nrow = nrow
204+
)
205+
}
206+
p <- p + scale_color_manual(
207+
values = c(
208+
Up = line.up,
209+
Down = line.down,
210+
Stable = line.stable
211+
),
212+
guide = line.guide
213+
)
214+
215+
p <- p + theme_biome_utils() + xlab(group)
216+
217+
return(p)
218+
}

0 commit comments

Comments
 (0)