2017-07-24 2 views
1

Ich möchte ein vertikales Balkendiagramm basierend auf dem Niveau der quantitativen Variable auf der x-Achse sortieren.Sortierung Plot Vertikales Balkendiagramm nach Häufigkeit

Reproduzierbare Beispiel:

library(plotly) 
library(dplyr) 

df <- data.frame(a = c(1000,100,500,1), b = c('blue', 'green', 'yellow', 'red')) 

plot_ly(
    data = df, 
    x = ~a, 
    y = ~b, 
    type = 'bar', 
    orientation = 'h' 
) %>% 
    layout(
     yaxis = list(
      categoryorder = "array", 
      categoryarray = ~a 
      ) 
     ) 

Deshalb mag ich eine vertikale Balkendiagramm, wo die Bestellung auf der y-Achse nach unten: ‚blau‘, ‚gelb‘, ‚grün‘ und ‚rot‘. Ich habe über categoryorder gelesen, was eine gute Lösung schien, aber irgendwie funktioniert es in der Praxis nicht.

Antwort

0

Option 1

library(plotly) 
library(dplyr) 

df <- data.frame(a = c(1000,100,500,1), b = c('blue', 'green', 'yellow', 'red')) 
df$b = factor(df$b,levels =c("red","green","yellow", "blue")) 

plot_ly(
    data = df, 
    x = ~a, 
    y = ~b, 
    type = 'bar', 
    orientation = 'h' 
) 

Option 2

library(plotly) 
library(dplyr) 

df <- data.frame(a = c(1000,100,500,1), b = c('blue', 'green', 'yellow', 'red')) 

plot_ly(
    data = df, 
    x = ~a, 
    y = ~b, 
    type = 'bar', 
    orientation = 'h' 
) %>% 
    layout(
    yaxis = list(
     categoryorder = "array", 
     categoryarray = ~c("red","green","yellow", "blue")) 
    ) 
) 

hoffe, das hilft! enter image description here

+0

Vielen Dank für die Antwort, meine Hoffnung, es ohne Unordnung mit irgendwelchen Faktorstufen zu tun war, und es direkt im 'plot_ly()' Funktion – Michael

+0

zu tun, die auch möglich sind, fügte hinzu, dass als zweite Option auf meine Antwort. Bitte akzeptiere/upvote meine Antwort, wenn du es hilfreich gefunden hast, danke! – Florian

Verwandte Themen