Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting only time using ggplot2

Tags:

r

ggplot2

I have a data frame like this:

 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))

I like to see the y-axis from 00:00 23:59 in 2 hours increment and be able to draw a red line on STime value.

I have somthing like this but it does not look right:

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 how would you do this in ggplot2? Can somebody give me pointers/start me in the right direction?

Regards,

like image 494
user1471980 Avatar asked Jan 08 '13 16:01

user1471980


People also ask

How do you plot a time series object in R?

Creating a time seriesThe ts() function will convert a numeric vector into an R time series object. The format is ts(vector, start=, end=, frequency=) where start and end are the times of the first and last observation and frequency is the number of observations per unit time (1=annual, 4=quartly, 12=monthly, etc.).

How do you visualize time series data in R?

The R programming language provides a strong of tools in the ggplot2 package to visualize data. We can use the geom_line() function to visualize the time-series data using a line plot.

What does Geom_point () do when used with ggplot ()?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot. ggplot2 comes with many geom functions that each add a different type of layer to a plot.


1 Answers

You have to format your times into actual times. Right now they are factors (Check your data frame with str(yy)). When ETime is plotted, the single time is plotted as 1 and labeled "10:13." So, the solution below first converts the string "10:13" into a time (strptime) then converts it to POSIXct, or seconds since an origin (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 Note on geom_line to date axis.

Pay attention to your timezones too. Otherwise R/ggplot will format things according to your local time zone.

like image 110
oshun Avatar answered Sep 24 '22 03:09

oshun