2016-07-25 6 views
1

Ich versuche plotly zu verwenden, um ein Balkendiagramm mit Zeichenfolgen (Kombinationsnummer) als x-Achse zu zeichnen.
("1", "2", "3", "4 - 5", "6 - 8", "9 - 13", "14 bis 21", "22 - 34", "35 - 55")R plotly X-Achse Zeichen/Faktor (Kombinationsnummer und -). Plot zeigt nur Achsen an, die nur Zahlen enthalten. Fehler?

Es werden jedoch nur 3 Daten angegeben. ("1", "2", "3")

Hier ist der Code:

library(plotly) 

dfb = structure(groups = c("1", "2", "3", "4 - 5", "6 - 8", 
         "9 - 13", "14 - 21", "22 - 34", "35 - 55"), 
    counts = c(29090,10074, 4573, 4029, 2289, 1120, 337, 78, 15)), 
    class = c("data.frame"), 
    row.names = c(NA,-9L), 
    .Names = c("groups","counts") 
) 

    plot_ly(dfb, 
     x = groups, 
     y = counts, 
     type = "bar") 

kehrt:

enter image description here

Aber wenn ich auf den Gruppen gefiltert, die nur enthalten Zahlen, ist es gut funktioniert:

dfc=subset(dfb,dfb$groups!='1') 
    plot_ly(dfc, 
     x = groups, 
     y = counts, 
     type = "bar") 

enter image description here

Warum passiert das? Und wie löst man das?

Ich verwende plotly, weil ich es mit glänzenden verwendet werden soll, und jetzt ist es das für mich fit.

habe ich nicht benutzt ggplotly (ggplot machen und dann wandeln sie plotly zu), weil manchmal Achse Pünktchen abgeschnitten zu werden.

Antwort

1

ich Angst habe, ist dies eine doppelte Frage: Plotly R: Cant get points to plot when two values present per x factor level. Aber hier haben Sie ein paar Lösungen:

Lösung 1(auch in der Vorlagefrage vorgeschlagen):

library(plotly) 

dfb = data.frame(groups= c("1", "2", "3", "4 - 5", "6 - 8","9 - 13", "14 - 21", "22 - 34", "35 - 55"), 
      counts=c(29090,10074, 4573, 4029, 2289, 1120, 337, 78, 15)) 

plot_ly(dfb, 
    x = groups, 
    y = counts, 
    type = "bar") %>% 
    layout(xaxis=list(type='category')) 

Lösung 2(Art und Weise weniger elegant)

groups_str <- c("1", "2", "3", "4 - 5", "6 - 8","9 - 13", "14 - 21", "22 - 34", "35 - 55") 
groups <- 1:length(groups_str) 

dfb = data.frame(groups= groups, 
      counts=c(29090,10074, 4573, 4029, 2289, 1120, 337, 78, 15)) 

plot_ly(dfb, 
    x = groups, 
    y = counts, 
    type = "bar") %>% 
    layout(xaxis=list(tickmode='array', tickvals=groups, ticktext=groups_str)) 

Beide produzieren die Figur(mit unterschiedlichen xaxis Titel, obwohl) enter image description here