Stacked bar ggplot2, descending sorting of bar values
2
1
Entering edit mode
2.3 years ago
milady81 ▴ 70

Dear All,

I am asking for help very rare but now I need your help.

I have stacked barplot in ggplot2 it looks like that:

enter image description here

sts_dataframe <- as.data.frame(sts)
sts_dataframe$Collection <- factor(sts_dataframe$Collection, levels = c("T", "NORM"))
mylevels <- c("ST10","ST12","ST14","ST31","ST34","ST38","ST46","ST48","ST58","ST59","ST68","ST69","ST73","ST88","ST93","ST95","ST117","ST131","ST144","ST155","ST156","ST167","ST192","ST345","ST357","ST372","ST393","ST394","ST398","ST405","ST410","ST442","ST448","ST450","ST453","ST484","ST540","ST617","ST636","ST648","ST744","ST746","ST773","ST847","ST977","ST993","ST1193","ST1290","ST1303","ST1312","ST1324","ST1421","ST1431","ST1434","ST1588","ST1611","ST1722","ST1852","ST2178","ST3268","ST3889","ST4110","ST4381","ST4450","ST4981","ST4985","ST5127","ST5150","ST5584","ST6082","ST7358","ST12011","ST12665")
sts_dataframe$ST <- factor(sts_dataframe$ST, levels = mylevels)
sts_dataframe$Collection2 <- as.numeric(sts_dataframe$Collection)

st_graph <- ggplot(sts_dataframe, aes(x=Collection, y=Percent, fill=ST )) +
geom_point() + 
ggtitle("") + 
ggeasy::easy_center_title() +
facet_grid(~Phylogroup) + 
geom_bar(position = "stack", stat = "identity", width = 0.85) + 
xlab("") + ylab("Proportion of isolates (%)") +
scale_x_discrete(guide = guide_axis(angle = 40)) +
geom_text(aes(label=ST), size = 3, position = position_stack(vjust = 0.5)) +
scale_y_continuous(expand = expansion(mult = c(0, 0))) +
theme(axis.line = element_line(colour = "black", size = 1.2, linetype = "solid"), legend.key.size = unit(0.5, 
'cm'), legend.text = element_text(size=20), legend.title = element_text(size=25), axis.text = 
element_text(size=21), axis.text.x = element_text(vjust = 1.0), axis.title = element_text(size=27), strip.text.x= element_text(size = 25), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank())

st_graph + theme(text=element_text(family="Times New Roman", face="plain", size=6))

My question is (because I have no idea how to do it) how to sort inside bar values in descending order of the Percent (last column). Important: the legend should stay like it is sorted in ascending order by ST number.

The data frame looks like (example)

Collection  Phylogroup  ST  Percent
T   A   ST1290  0.6
T   A   ST1303  0.6
T   A   ST1312  0.6
T   A   ST1324  0.6
T   A   ST93    1.2
T   A   ST540   1.8
T   A   ST10    7.2
NORM    A   ST773   0.8
NORM    A   ST617   1.7

I would be glad for any help!

Best regards, Dorota

ggplot2 R • 3.5k views
ADD COMMENT
3
Entering edit mode
2.3 years ago
library(forcats)
ggplot(sts_dataframe,aes(x=Collection, y=Percent, fill= fct_reorder(ST,Percent)))+
  geom_bar(stat = "identity", width = 0.85,position = position_fill(reverse = TRUE))+
  scale_fill_discrete(breaks=mylevels)

Results

ADD COMMENT
0
Entering edit mode
2.3 years ago
milady81 ▴ 70

Thank you, thank you, thank you a lot!!!:) Amazing!

ADD COMMENT

Login before adding your answer.

Traffic: 1710 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6