library(readxl)
my_XPS <- read_excel("D:/PhD 5th-Si/XPS/my_XPS.xlsx")
View(my_XPS)
library(reshape2)
data <- melt(my_XPS, id.vars = 'Elements', variable.name = "name", value.name = "value", level = 1)
View(data)
## for图例顺序
data$Elements = factor(data$Elements, levels = data$Elements[1:7])
library(ggplot2)
library(RColorBrewer)
mycolors <- colorRampPalette(c(brewer.pal(7, "Paired")))(7)
ggplot(data, aes(x = name, y = value, fill = Elements )) + geom_bar(stat ="identity", width = 0.92, position = "fill") + theme_bw() + scale_fill_manual(values = mycolors) + theme(panel.border = element_blank()) + theme(axis.line = element_line(size=0.8, colour = "black")) + theme(panel.grid = element_blank()) + theme(axis.title = element_blank()) + theme(axis.text = element_text(colour = 'black', size = 12)) + theme(legend.text = element_text(size = 10)) + theme(legend.title = element_blank()) + theme(axis.ticks.y = element_line(size=0.6)) + theme(axis.ticks.length.y = unit(0.15, 'cm')) + theme(axis.ticks.x = element_blank()) + guides(fill=guide_legend(ncol=1))

library(patchwork)
p1 = ggplot(data, aes(x = name,y = value, fill = Elements )) + geom_bar(stat ="identity",width = 0.92,position ="fill") + theme_bw() + scale_fill_manual(values = mycolors) + theme(panel.border = element_blank()) + theme(axis.line = element_line(size=0.8, colour = "black")) + theme(panel.grid = element_blank()) + theme(axis.title = element_blank()) + theme(axis.text = element_text(colour = 'black', size = 12)) + theme(legend.text = element_text(size = 10)) + theme(legend.title = element_blank()) + theme(axis.ticks.y = element_line(size=0.6)) + theme(axis.ticks.length.y = unit(0.15, 'cm')) + theme(axis.ticks.x = element_blank()) + guides(fill=guide_legend(ncol=1))  + coord_cartesian(ylim = c(0,0.4))
p1

p2 = ggplot(data, aes(x = name,y = value, fill = Elements )) + geom_bar(stat ="identity",width = 0.92,position ="fill") + theme_bw() + scale_fill_manual(values = mycolors) + theme(panel.border = element_blank()) + theme(axis.line = element_line(size=0.8, colour = "black")) + theme(panel.grid = element_blank()) + theme(axis.title = element_blank()) + theme(axis.text = element_text(colour = 'black', size = 12)) + theme(legend.text = element_text(size = 10)) + theme(legend.title = element_blank()) + theme(axis.ticks.y = element_line(size=0.6)) + theme(axis.ticks.length.y = unit(0.15, 'cm')) + theme(axis.ticks.x = element_blank()) + guides(fill=guide_legend(ncol=1))  + coord_cartesian(ylim = c(0.9,1)) + scale_y_continuous(breaks = c(0.9, 1)) + theme(axis.text.x = element_blank(), axis.line.x = element_blank(), legend.position = 'none') 
p2

XPS_Tw = p2/p1
XPS_Tw + plot_layout(ncol = 1, heights = c(1, 4))