2012-10-18 14 views
6

Apprezzerei qualsiasi idea su come comportarsi nel mio codice o binwidth. Il mio set di dati contiene punti di dati con data e ora raccolti ogni poche ore "4" e ogni giorno "24". Sto cercando di tracciare gli istogrammi impilati di 4 ore a sinistra e gli istogrammi impilati a 24 ore a destra. Pertanto, voglio che la larghezza di banda sulla destra sia 6 volte più larga della larghezza di banda sinistra. Ma tutto ciò che ho provato con binwidth non ha funzionato. I dati sull'asse x, dati3 $ dts sembrano essere continui e non discreti, ma forse non lo sto facendo bene.ggplot2 binwidth non risponde in istogramma facet_wrap istogramma

Nota importante sui dati: i dati che vengono visualizzati sul lato destro, le ore = 24 dati, hanno un valore Dts che è sempre un numero intero. I dati a sinistra, le ore = 4 dati, hanno valori di Dts non interi.

   "dts" "Yes" "No" "Maybe" "hours" "days" 
"258" 15627.668 8  0 1  4 "7 Days" 
"259" 15627.832 13  11 18  4 "7 Days" 
"260" 15628  34  47 89  4 "7 Days" 
"261" 15628  37  47 90  24 "7 Days" 
"262" 15628.168 3  0 1  4 "7 Days" 
"40" 15571  345  419 674  24 "90 Days" 
"41" 15571.5  91  145 130  4 "90 Days" 
"42" 15571.668 158  149 284  4 "90 Days" 
"43" 15571.832 96  125 260  4 "90 Days" 
"44" 15572  55  33 137  4 "90 Days" 
"45" 15572  1050 1119 2660 24 "90 Days" 

codice con i dati tirato da pastebin:

library (ggplot2) 
library (scales) 
library(grid) 
library(gridExtra) 

color3 <- c("mediumspringgreen","red","grey44") 
titles.days <- c("7 Days", "90 Days") 
names.facetby <- c ("dts", "hours", "days") 

data3 <- read.table ("http://pastebin.com/download.php?i=wUQQUXP4", header=TRUE) 
data3.melt <- melt (data3 , id = names.facetby) 
data3.melt$days <- factor (data3.melt$days, levels = titles.days) # put the factor in the right order, so the graphs are in the right order 

a <- ggplot  (data3.melt 
     , aes (  x = dts #as.Date(dts , date1970) 
       , y = value 
       , fill = variable)) + 
     opts (axis.text.x=theme_text(angle=0, hjust=1)) + 
     scale_fill_manual(values = color3) + 
     scale_x_date(labels = date_format("%m/%d\n %a")) + 
     geom_histogram (stat = "identity", position = "stack", binwidth=6.2) + 
     facet_wrap(days ~ hours, ncol=2, scales="free")    

print(a)   

risultati attuali, mostrando i grafici lati giusti con modo binwidth troppo stretta:

enter image description here

+1

Se mi chiedi, i raccoglitori hanno la stessa larghezza. La differenza è che le trame di 90 giorni hanno circa ... 90/7 volte più scomparti da mostrare! (lo puoi vedere usando 'scale = 'free_y'' nel tuo' facet_wrap'. – Justin

+0

Justin grazie, hai ragione che i raccoglitori hanno la stessa larghezza, ma è quello che sto cercando di sistemare. i grafici a lati dovrebbero avere larghezze binarie che coprono l'intera giornata, ad esempio 6X più larghe dei grafici a sinistra Inoltre, sto già usando scale = "libere" che comprende scale = free_x e scale = free_y? – hhk

+1

'scale =" libero " 'Permette ad entrambi di variare, ma per illustrare il mio punto, ho vincolato la scala' x' in modo che corrisponda a tutti e quattro i grafici. [qui] (https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/ aQQ2hTYRQF8) è un vecchio collegamento a una discussione ggplot su questa e sulla soluzione di Hadley (ma le cose sono potenzialmente cambiate di recente) – Justin

risposta

3

@ justin's link to Hadley Wickham's post ha la risposta, che consiste nel tracciare grafici sinistro e destro in diversi livelli.

codice aggiornato che traccia correttamente con 2 nuove linee geom_histogram all'interno ggplot:

libreria (ggplot2) libreria (scale) libreria (grid) libreria (gridExtra)

color3 <- c("mediumspringgreen","red","grey44") 
titles.days <- c("7 Days", "90 Days") 
names.facetby <- c ("dts", "hours", "days") 

data3 <- read.table ("http://pastebin.com/download.php?i=wUQQUXP4", header=TRUE) 
data3.melt <- melt (data3 , id = names.facetby) 
data3.melt$days <- factor (data3.melt$days, levels = titles.days) # put the factor in the right order, so the graphs are in the right order 



a <- ggplot  (data3.melt 
     , aes (  x = dts #as.Date(dts , date1970) 
       , y = value 
       , fill = variable)) + 
     opts (axis.text.x=theme_text(angle=0, hjust=1)) + 
     scale_fill_manual(values = color3) + 
     scale_x_date(labels = date_format("%m/%d\n%a")) + 

    # bad idea, good ideas follow geom_histogram (stat = "identity", position = "stack", binwidth=6.2) + #, breaks = breaks.x 
     geom_histogram (data = subset(data3.melt, hours == 4), stat = "identity", position = "stack", binwidth=0.3) + #, breaks = breaks.x 
     geom_histogram (data = subset(data3.melt, hours == 24), stat = "identity", position = "stack", binwidth=0.9) + #, breaks = breaks.x 

     facet_wrap(days ~ hours, ncol=2, scales="free")    

print(a)  # plot the thing 

grafico rettificato : http://imgur.com/9j1Xz

1

I bidoni sono in realtà la stessa larghezza. La differenza è perché ci sono molti altri contenitori nelle trame dei 90 giorni.

si può vedere questo impostando scales="free_y" in facet_wrap

Si può anche dare un'occhiata a this post che descrive un potenziale tecnica per fare quello che stai cercando.