Data input and manipulation

library(readxl)
yuanhe_family_top50_phylum_integrated <- read_excel("D:/(R) plots collection/R markdown/Stacked area/yuanhe_family_top50_phylum_integrated.xlsx")
View(yuanhe_family_top50_phylum_integrated)
yh = yuanhe_family_top50_phylum_integrated
for (i in 1:13) {
     yh[,i+1] = yh[,i+1]/sum(yh[,i+1])
}
library(reshape2)
yhdat = melt(yh)
## Using OTU_ID as id variables
yhdat$variable = c(rep(1, 11), rep(2, 11), rep(3, 11), rep(4, 11), rep(5, 11), rep(6, 11), rep(7, 11), rep(8, 11), rep(9, 11), rep(10, 11), rep(11, 11), rep(12, 11), rep(13, 11))

Plot!

# 设置颜色
library(RColorBrewer)
brewer.pal(12, 'Paired')
##  [1] "#A6CEE3" "#1F78B4" "#B2DF8A" "#33A02C" "#FB9A99" "#E31A1C" "#FDBF6F"
##  [8] "#FF7F00" "#CAB2D6" "#6A3D9A" "#FFFF99" "#B15928"
mycolors <- colorRampPalette(c("#A6CEE3", "#1F78B4", "#B2DF8A", "#33A02C", "#FB9A99", "#E31A1C", "#FDBF6F", "#FF7F00", "#CAB2D6", "#6A3D9A", "#B15928" ))(11)
library(ggplot2)
yhplot = ggplot(yhdat, aes(x=variable, y=value, fill=OTU_ID)) + geom_area( alpha = 0.8) + scale_fill_manual(values =  mycolors)
yhplot

yhplot = yhplot + theme(axis.ticks = element_line(colour = "black"),
     panel.grid.major = element_line(colour = NA),
     panel.grid.minor = element_line(colour = NA),
     axis.text = element_text(colour = "black"),
     panel.background = element_rect(fill = NA,
     colour = "gray0", size = 1, linetype = "solid")) +labs(x = NULL, fill = NULL) + theme(axis.text = element_text(color = 'black', size = 14))
yhplot

yhplot = yhplot + theme(axis.ticks = element_line(colour = "gray70"), panel.grid.major = element_line(colour = "gray70"),
panel.grid.minor = element_line(colour = "gray70"),
panel.background = element_rect(linetype = "blank")) +labs(y = NULL)
yhplot

yhplot = yhplot + theme(axis.ticks = element_line(colour = "black"),
     panel.grid.major = element_line(colour = NA),
     panel.grid.minor = element_line(colour = NA)) + theme(legend.text = element_text(size = 12))
yhplot

xlabels = colnames(yuanhe_family_top50_phylum_integrated[2:14])
xlabels = data.frame(xlabels)
yhplot + scale_x_continuous(breaks=1:13, labels = xlabels$xlabels) + theme(axis.text.x = element_text(size = 10, angle = 30, vjust = 0.8, hjust = 0.55))

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.