2015-12-23 17 views
9

Il mio tavolo è data.combined con seguente struttura:R ggplot - stat_bin errore richiede variabile x continua

'data.frame': 1309 obs. of 12 variables: 
$ Survived: Factor w/ 3 levels "0","1","None": 1 2 2 2 1 1 1 1 2 2 ... 
$ Pclass : Factor w/ 3 levels "1","2","3": 3 1 3 1 3 3 1 3 3 2 ... 
$ Name : Factor w/ 1307 levels "Abbing, Mr. Anthony",..: 109 191 358 277 16 559 520 629 417 581 ... 
$ Sex  : num 2 1 1 1 2 2 2 2 1 1 ... 
$ Age  : num 22 38 26 35 35 NA 54 2 27 14 ... 
$ SibSp : int 1 1 0 1 0 0 0 3 0 1 ... 
$ Parch : int 0 0 0 0 0 0 0 1 2 0 ... 
$ Ticket : Factor w/ 929 levels "110152","110413",..: 524 597 670 50 473 276 86 396 345 133 ... 
$ Fare : num 7.25 71.28 7.92 53.1 8.05 ... 
$ Cabin : Factor w/ 187 levels "","A10","A14",..: 1 83 1 57 1 1 131 1 1 1 ... 
$ Embarked: Factor w/ 4 levels "","C","Q","S": 4 2 4 4 4 3 4 4 4 2 ... 
$ Title : Factor w/ 4 levels "Master.","Miss.",..: 3 3 2 3 3 3 3 1 3 3 ... 

voglio disegnare un grafico in modo da riflettere la relazione tra titolo ed è sopravvissuto, suddivisi per Pclass. Ho usato il codice seguente:

ggplot(data.combined[1:891,], aes(x=Title, fill = Survived)) + 
    geom_histogram(binwidth = 0.5) + 
    facet_wrap(~Pclass) + 
    ggtitle ("Pclass") + 
    xlab("Title") + 
    ylab("Total count") + 
    labs(fill = "Survived") 

Tuttavia questo si traduce in errore: Error: StatBin requires a continuous x variable the x variable is discrete. Perhaps you want stat="count"?

Se cambio variabile Titolo in numerica: data.combined$Title <- as.numeric(data.combined$Title) poi le opere di codice ma l'etichetta nel grafico è inoltre numerico (sotto). Per favore dimmi perché succede e come risolverlo. Grazie.

Btw, io uso R 3.2.3 su Mac El Capital.

Grafico: Al posto del sig, signorina, la signora l'asse x mostra i valori numerici 1,2,3,4

enter image description here

+0

A [ esempio riproducibile] (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) sarebbe fantastico qui. –

+0

Forse anche la tua versione di ggplot (vedi 'sessionInfo()'), dato che la mia versione (1.0.1) non ha stat = "count". E hai provato 'stat =" count "? Come dice il messaggio di errore (mantenendo il' Titolo' come un fattore)? –

+0

Grazie matemical.coffee, ho appena aggiornato alcune informazioni nella mia domanda. Io uso ggplot2_2.0.0, va bene? –

risposta

11

Somma la risposta dalle osservazioni di cui sopra:

1 - Sostituire geom_histogram(binwidth=0.5) con geom_bar(). Tuttavia in questo modo non consentirà la personalizzazione della larghezza di banda.

2 - L'utilizzo di stat_count(width = 0.5) anziché geom_bar() o geom_histogram(binwidth = 0.5) lo risolverebbe.

-1

Come affermato in precedenza l'uso geom_bar() al posto di geom_histogram, affidare le codice di esempio riportato qui di seguito (volevo grafico separato per ogni mese per i dati di data di nascita):

ggplot(data = pf,aes(x=dob_day))+ 
geom_bar()+ 
scale_x_discrete(breaks = 1:31)+ 
facet_wrap(~dob_month,ncol = 3) 
0

graph

extractTitle <- function(Name) {  
Name <- as.character(Name) 

    if (length(grep("Miss.", Name)) > 0) { 
    return ("Miss.") 
    } else if (length(grep("Master.", Name)) > 0) { 
    return ("Master.") 
    } else if (length(grep("Mrs.", Name)) > 0) { 
    return ("Mrs.") 
    } else if (length(grep("Mr.", Name)) > 0) { 
    return ("Mr.") 
} else { 
    return ("Other") 
    } 
} 

titles <- NULL 

for (i in 1:nrow(data.combined)){ 
    titles <- c(titles, extractTitle(data.combined[i, "Name"])) 
} 

data.combined$title <- as.factor(titles) 

ggplot(data.combined[1:892,], aes(x = title, fill = Survived))+ 
     geom_bar(width = 0.5) + 
     facet_wrap("Pclass")+ 
     xlab("Pclass")+ 
     ylab("total count")+ 
     labs(fill = "Survived") 
Problemi correlati