library(ggplot2)
library(vegan)
library(cowplot)
library(dplyr)
set.seed(123)
n_replicates <- 3
df_line <- expand.grid(
Day = c(0, 5, 10, 15, 25, 30),
Treatment = LETTERS[1:5],
Replicate = 1:n_replicates
)
for (i in 1:nrow(df_line)) {
day <- df_line$Day[i]
treatment <- df_line$Treatment[i]
base_value <- 15 + day * 0.2
df_line$Part.C[i] <- rnorm(1, mean = base_value, sd = 1.5)
}
df_summary <- df_line %>%
group_by(Day, Treatment) %>%
summarise(
Mean = mean(Part.C),
SD = sd(Part.C),
.groups = "drop"
)
set.seed(456)
n_samples <- 30
n_otus <- 10
otu_table <- matrix(rpois(n_samples * n_otus, lambda = 10), nrow = n_samples, ncol = n_otus)
rownames(otu_table) <- paste0("Sample", 1:n_samples)
metadata <- data.frame(
Sample = rownames(otu_table),
Treatment = rep(LETTERS[1:5], each = 6),
Day = rep(c(0, 5, 10, 15, 25, 30), times = 5)
)
p_line <- ggplot(df_summary, aes(x = Day, y = Mean, color = Treatment, group = Treatment)) +
geom_line(linewidth = 1) +
geom_point(size = 4,shape=18) +
geom_errorbar(
aes(ymin = Mean - SD, ymax = Mean + SD),
width = 0,
linewidth = 0.8
) +
scale_color_manual(values = c("#FFC95E", "#FC7E86", "#507564", "#73C5DC", "#000000")) +
labs(
x = "Day",
y = "Concentration (Part.C)",
title = ""
) +
theme_bw()+theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+
theme(axis.text=element_text(colour='black',size=9))+
theme(
legend.position = "NA",
plot.title = element_text(face = "bold", hjust = 0)
)
dist_matrix <- vegdist(otu_table, method = "bray")
pcoa_result <- cmdscale(dist_matrix, k = 2, eig = TRUE)
pcoa_scores <- as.data.frame(pcoa_result$points)
colnames(pcoa_scores) <- c("PCo1", "PCo2")
pcoa_scores$Treatment <- metadata$Treatment
pcoa_scores$Day <- metadata$Day
variance <- round(pcoa_result$eig / sum(pcoa_result$eig) * 100, 1)
p_pcoa <- ggplot(pcoa_scores, aes(x = PCo1, y = PCo2, color = Treatment, shape = factor(Day))) +
geom_point(size = 4, shape=24, alpha = 1) +
scale_color_manual(values = c("#FFC95E", "#FC7E86", "#507564", "#73C5DC", "#000000")) +
scale_shape_manual(values = c(16, 17, 15, 18, 8, 3)) +
labs(
x = paste0("PCo1 (", variance[1], "%)"),
y = paste0("PCo2 (", variance[2], "%)"),
title = "",
shape = "Day"
) +
theme_bw()+theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+
theme(axis.text=element_text(colour='black',size=9))+
theme(
legend.position = "right",
plot.title = element_text(face = "bold", hjust = 0)
)
library(cowplot)
cowplot::plot_grid(p_line, p_pcoa ,ncol= 2, rel_widths = c(1, 1.4))