Longitudinal Beta Diversity Dot Plot for Microbiome Data
Source:R/generate_beta_per_time_dotplot_long.R
generate_beta_per_time_dotplot_long.Rd
This function generates dot plots for visualizing the results of longitudinal beta diversity tests in microbiome data. It focuses on displaying how the beta diversity indices change over time across different groups.
Arguments
- data.obj
A MicrobiomeStat data object containing microbiome data and metadata.
- test.list
A list of test results from `generate_beta_change_per_time_test_long` function, representing the statistical analysis of beta diversity over time.
- group.var
A string specifying the group variable in meta.dat for between-group comparisons in the dot plot.
- time.var
A string representing the time variable in the meta.dat. This variable is used for the x-axis in the dot plot.
- t0.level
The baseline time level for time series analysis.
- ts.levels
The time series levels for analysis.
- base.size
Optional; numeric, base font size for text elements in the plot. Default is 16.
- theme.choice
Plot theme choice. Specifies the visual style of the plot. Can be one of the following pre-defined themes: - "prism": Utilizes the ggprism::theme_prism() function from the ggprism package, offering a polished and visually appealing style. - "classic": Applies theme_classic() from ggplot2, providing a clean and traditional look with minimal styling. - "gray": Uses theme_gray() from ggplot2, which offers a simple and modern look with a light gray background. - "bw": Employs theme_bw() from ggplot2, creating a classic black and white plot, ideal for formal publications and situations where color is best minimized. - "light": Implements theme_light() from ggplot2, featuring a light theme with subtle grey lines and axes, suitable for a fresh, modern look. - "dark": Uses theme_dark() from ggplot2, offering a dark background, ideal for presentations or situations where a high-contrast theme is desired. - "minimal": Applies theme_minimal() from ggplot2, providing a minimalist theme with the least amount of background annotations and colors. - "void": Employs theme_void() from ggplot2, creating a blank canvas with no axes, gridlines, or background, ideal for custom, creative plots. Each theme option adjusts various elements like background color, grid lines, and font styles to match the specified aesthetic. Default is "bw", offering a universally compatible black and white theme suitable for a wide range of applications.
- custom.theme
A custom ggplot theme provided as a ggplot2 theme object. This allows users to override the default theme and provide their own theme for plotting. Custom themes are useful for creating publication-ready figures with specific formatting requirements.
To use a custom theme, create a theme object with ggplot2::theme(), including any desired customizations. Common customizations for publication-ready figures might include adjusting text size for readability, altering line sizes for clarity, and repositioning or formatting the legend. For example:
“`r my_theme <- ggplot2::theme( axis.title = ggplot2::element_text(size=14, face="bold"), # Bold axis titles with larger font axis.text = ggplot2::element_text(size=12), # Slightly larger axis text legend.position = "top", # Move legend to the top legend.background = ggplot2::element_rect(fill="lightgray"), # Light gray background for legend panel.background = ggplot2::element_rect(fill="white", colour="black"), # White panel background with black border panel.grid.major = ggplot2::element_line(colour = "grey90"), # Lighter color for major grid lines panel.grid.minor = ggplot2::element_blank(), # Remove minor grid lines plot.title = ggplot2::element_text(size=16, hjust=0.5) # Centered plot title with larger font ) “`
Then pass `my_theme` to `custom.theme`. If `custom.theme` is NULL (the default), the theme is determined by `theme.choice`. This flexibility allows for both easy theme selection for general use and detailed customization for specific presentation or publication needs.
- palette
An optional parameter specifying the color palette to be used for the plot. It can be either a character string specifying the name of a predefined palette or a vector of color codes in a format accepted by ggplot2 (e.g., hexadecimal color codes). Available predefined palettes include 'npg', 'aaas', 'nejm', 'lancet', 'jama', 'jco', and 'ucscgb', inspired by various scientific publications and the `ggsci` package. If `palette` is not provided or an unrecognized palette name is given, a default color palette will be used. Ensure the number of colors in the palette is at least as large as the number of groups being plotted.
Optional; boolean, whether to save the plot as a PDF file.
- pdf.wid
Optional; numeric, width of the saved PDF file.
- pdf.hei
Optional; numeric, height of the saved PDF file.
Value
A list of ggplot objects, each representing a dot plot for a specific group. The plots can be further customized or directly rendered.
Details
The function uses ggplot2 to create dot plots, with options for customizing themes and aesthetics. It visualizes the significance and magnitude of changes in beta diversity indices, indicated by color and size of the dots. The function allows for detailed customization of plot features, including base font size and theme choice.
The function prepares data by merging and transforming the results from `generate_beta_test_long` to fit the plotting structure. It handles the display of statistical significance using color coding and sizes dots based on the estimate value from the statistical test.
Examples
if (FALSE) { # \dontrun{
data(subset_T2D.obj)
result1 <- generate_beta_change_per_time_test_long(
data.obj = subset_T2D.obj,
dist.obj = NULL,
time.var = "visit_number_num",
t0.level = unique(subset_T2D.obj$meta.dat$visit_number_num)[1],
ts.levels = unique(subset_T2D.obj$meta.dat$visit_number_num)[-1],
subject.var = "subject_id",
group.var = "subject_race",
adj.vars = NULL,
dist.name = c('BC', 'Jaccard')
)
# Visualizing the results for the Type 2 Diabetes dataset
dotplot_T2D <- generate_beta_per_time_dotplot_long(
data.obj = subset_T2D.obj,
test.list = result1,
group.var = "subject_race",
time.var = "visit_number_num",
t0.level = unique(subset_T2D.obj$meta.dat$visit_number_num)[1],
ts.levels = unique(subset_T2D.obj$meta.dat$visit_number_num)[-1],
)
data(ecam.obj)
dist.obj <- mStat_calculate_beta_diversity(ecam.obj, c('BC', 'Jaccard'))
result2 <- generate_beta_change_per_time_test_long(
data.obj = ecam.obj,
dist.obj = dist.obj,
time.var = "month",
t0.level = unique(ecam.obj$meta.dat$month)[1],
ts.levels = unique(ecam.obj$meta.dat$month)[-1],
subject.var = "subject.id",
group.var = "diet",
adj.vars = NULL,
dist.name = c('BC', 'Jaccard')
)
# Visualizing the results for the ECAM dataset
dotplot_ecam <- generate_beta_per_time_dotplot_long(
data.obj = ecam.obj,
test.list = result2,
group.var = "delivery",
time.var = "month",
t0.level = unique(ecam.obj$meta.dat$month)[1],
ts.levels = unique(ecam.obj$meta.dat$month)[-1],
base.size = 15
)
} # }