Sort each column from bar chart (stack) from lower to higher
1
0
Entering edit mode
David ▴ 860
@david-3335
Last seen 6.0 years ago

 I would like to sort each histogram from a stacked barplot so that each column has the higher value at the bottom.

Here is an example of what i obtain that you can reproduce

library(plotly)
m <- matrix(0, ncol = 3, nrow = 20)
m <- data.frame(m)
colnames(m)= c("variable","sample","value")
m$variable <- rep(c("A","B","C","D","E"),4)
m$sample <- rep(paste("Sample",seq(1,4),sep="_"),each=5)
m$value = c(20,1,50,17,34,23,45,1,4,8,10,24,45,32,17,49,43,25,14,5)

plot_ly(m,
x = ~sample,
y = ~value,
color = ~variable,
    type = "bar")%>%
layout(barmode = 'stack')

 

How can i sort each column so that most abundant values are sorted (the higher at the bottom at the bottom ???)

 

 

plotly R • 2.6k views
ADD COMMENT
0
Entering edit mode
@james-w-macdonald-5106
Last seen 18 hours ago
United States

The plotly package is a CRAN package, not Bioconductor, so you should ask your questions on the R-help listserv (r-help@r-project.org), or on the plot.ly community help.

But do note that the primary usefulness of a stacked barchart comes from having everything stacked in the same order in each bar. That's like the whole point of doing a stacked barchart, so I would be rather surprised if there was an easy way to undo the one redeeming feature of that plot.
 

ADD COMMENT

Login before adding your answer.

Traffic: 460 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