Data input

library(readxl)
data <- read_excel("D:\\(R) plots collection\\R markdown\\Stacked bar plot\\data.xlsx")
data
## # A tibble: 15 x 5
##    level3                                 `7 days` `14 days` `21 days` `1 month`
##    <chr>                                     <dbl>     <dbl>     <dbl>     <dbl>
##  1 Amino sugar and nucleotide sugar meta~  455027.   606215.   215976.   392294.
##  2 Ascorbate and aldarate metabolism        58523.    71471.    36553.    51600.
##  3 Butanoate metabolism                    437294.   521413.   203639.   358935.
##  4 C5-Branched dibasic acid metabolism     171427.   203192.    91690.   152646.
##  5 Citrate cycle (TCA cycle)               387931.   511313.   188861.   336222.
##  6 Fructose and mannose metabolism         192549.   284099.   106224.   162998.
##  7 Galactose metabolism                    151236.   204645.    74156.   127196.
##  8 Glycolysis / Gluconeogenesis            478083.   659387.   244168.   411171.
##  9 Glyoxylate and dicarboxylate metaboli~  613223.   718853.   324665.   520469.
## 10 Inositol phosphate metabolism            79807.   107585.    42297.    65463.
## 11 Pentose and glucuronate interconversi~   95996.   127140.    46539.    81477.
## 12 Pentose phosphate pathway               319482.   426293.   155893.   273239.
## 13 Propanoate metabolism                   422280.   528635.   203437.   360288.
## 14 Pyruvate metabolism                     607480.   773526.   294709.   531967.
## 15 Starch and sucrose metabolism           231567.   338718.   102591.   194938.

Load required R packages

library(ggplot2)
library(reshape2)

Data organization

data <- melt(data, id.vars = 'level3', variable.name = "name", value.name = "value", level = 1)

Plot! (reordered by value)

ggplot(data, aes(x = name,y = value, fill = reorder(level3, value) )) + geom_bar(stat ="identity",width = 0.92,position ="stack", colour = "white") + theme_bw()  + 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))

Change color!

library(RColorBrewer)
mycolors <- colorRampPalette(c(brewer.pal(12, "Paired")))(15) #1. expand the palette 2. why 15?
ggplot(data, aes(x = name,y = value, fill = reorder(level3, value) )) + geom_bar(stat ="identity",width = 0.92,position ="stack", colour = "white") + 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))

# another set
mycolors <- colorRampPalette(c(brewer.pal(8, "Accent")))(15) 
ggplot(data, aes(x = name,y = value, fill = reorder(level3, value) )) + geom_bar(stat ="identity",width = 0.92,position ="stack", colour = "white") + 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))

Normalization by “position = ‘fill’”, so fucking convenient

ggplot(data, aes(x = name,y = value, fill = reorder(level3, value) )) + geom_bar(stat ="identity",width = 0.92,position ="fill", colour = "white") + 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))

Flip the plot!

ggplot(data, aes(x = name,y = value, fill = reorder(level3, value) )) + geom_bar(stat ="identity",width = 0.92,position ="fill", colour = "white") + 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=2)) + coord_flip() + theme(legend.position = 'bottom')

Change the coordinates!

ggplot(data, aes(x = name,y = value, fill = reorder(level3, value) )) + geom_bar(stat ="identity",width = 0.98,position ="fill", colour = "white") + 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 = 10)) + theme(legend.text = element_text(size = 8), legend.key.size = unit(15, "pt")) + 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_polar(theta = "x")

Change the coordinates again!

ggplot(data, aes(x = name,y = value, fill = reorder(level3, value) )) + geom_bar(stat ="identity",width = 0.98,position ="fill", colour = "white") + 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 = 10)) + theme(legend.text = element_text(size = 8), legend.key.size = unit(15, "pt")) + 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_polar(theta = "y")