Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - plotly - cannot sort graph by value

Tags:

r

plotly

I have some data that is sorted but in plotly the horizontal bar is only sorting in reverse alphabetical order but I need it to sort by a VALUE which is a number; see image 'YOY' column. enter image description here

ecomm_yoy <- ecomm_data2019 %>%
      inner_join(ecomm_data2018, by = "Brand") %>%
      mutate(YOY = round(((Value.x - Value.y)/Value.y)*100, 2)) 

    ecomm_yoy2 <- ecomm_yoy[order(-ecomm_yoy$YOY),]

    plot_ly(x = ecomm_yoy2$YOY, y = ecomm_yoy2$Brand, type = 'bar', orientation = 'h') 

enter image description here

like image 979
Matthew Appleyard Avatar asked May 06 '26 07:05

Matthew Appleyard


1 Answers

Add (using a pipe) this layout to your chart:

layout(yaxis = list(categoryorder = "array", categoryarray = ecomm_yoy2$YOY)) 
like image 183
José Luiz Ferreira Avatar answered May 09 '26 00:05

José Luiz Ferreira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!