2013-01-08 8 views
8

Ho un frame di dati come questo:tracciando solo il tempo usando ggplot2

head(yy) 
    Team  Date STime ETime 
1 A 2012-03-06 07:03 10:13 
2 A 2012-03-06 07:03 10:13 
3 A 2012-03-06 07:03 10:13 
4 A 2012-03-06 07:03 10:13 
5 A 2012-03-06 07:03 10:13 
6 A 2012-03-06 07:03 10:13 

dput (yy)

dput(yy) 
structure(list(Team = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "A", class = "factor"), 
Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "2012-03-06", class = "factor"), 
STime = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "07:03", class = "factor"), 
ETime = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "10:13", class = "factor")), .Names = c("Team", 
"Date", "STime", "ETime"), class = "data.frame", row.names = c(NA, 
-50L)) 

mi piace vedere all'asse y dalle 00:00 23:59 Incremento di 2 ore ed essere in grado di tracciare una linea rossa sul valore di STime.

ho somthing come questo, ma non guardare a destra:

ggplot(yy, aes(Date, ETime, group="Team")) + geom_jitter(size=0.05) + facet_wrap(~ Team) + geom_hline(yintercept=yy$Stime, colour="red", size=2) 

enter image description here come è possibile fare questo in ggplot2? Qualcuno può darmi dei consigli/mi ha avviato nella giusta direzione?

saluti,

+2

Potresti postare 'dput (df)' (o 'dput (head (df))' se è troppo grande) in modo che possiamo riprodurre i tuoi dati? –

+0

@DavidRobinson, ho appena inserito l'output di dput. – user1471980

+0

Non ci sono variazioni nei dati. Se stai semplicemente provando a produrre un'illustrazione dovresti dare un'occhiata a [inkscape] (http://inkscape.org/) è un grande pezzo di software libero come R. –

risposta

6

Dovete formattare i tempi in tempi reali. In questo momento sono fattori (Controlla il tuo data frame con str(yy)). Quando viene tracciato ETime, la singola ora viene tracciata come 1 ed etichettata "10:13". Quindi, la soluzione seguente converte prima la stringa "10:13" in un tempo (strptime), quindi la converte in POSIXct o in secondi da un'origine (1/1/1970).

library(ggplot2); library(scales) 

#Convert date string into POSIXct format 
yy$STime <- as.POSIXct(strptime(yy$STime, format = "%H:%M", tz = "UTC")) 
yy$ETime <- as.POSIXct(strptime(yy$ETime, format = "%H:%M", tz = "UTC")) 

#Define y-axis limits 
lims <- as.POSIXct(strptime(c("0:00","23:59"), format = "%H:%M", tz= "UTC"))  

ggplot(yy, aes(Date, ETime, group="Team")) + geom_jitter(size=1) + facet_wrap(~ Team) + 
    geom_hline(data = yy, aes(yintercept= as.numeric(STime)), colour="red", size=2) + 
    scale_y_datetime(limits =lims, breaks=date_breaks("2 hour"), 
        labels=date_format("%H:%M", tz = "UTC")) 

datetime y-axis Nota sulla geom_line to date axis.

Prestare attenzione anche ai propri fusi orari. Altrimenti R/ggplot formatterà le cose in base al fuso orario locale.

Problemi correlati