Sunday, 15 January 2012

r - Error message plotting timeseries with ggplot2 -



r - Error message plotting timeseries with ggplot2 -

i new ggplot2 , wondered whether help me simple problem. sample dataframe given follows. want plot time (hours , minutes) against number of counts.

date <- c("07/12/2012 05:00:00", "07/12/2012 06:00:00", "07/12/2012 07:00:00", "07/12/2012 08:00:00") date <- strptime(date, "%d/%m/%y %h:%m") counts <- c("0","3","10","6") counts <- as.numeric(counts) df1 <- data.frame(date,counts,stringsasfactors = false) #adds time dataframe. df1 <- within(df1,{ posb <- as.posixlt(date,format="%d/%m/%y %h:%m") hours <- posb$hour mins <- posb$min dates <- format(posb, "%x") time <- format(posb, "%h:%m") posb <- null # cleanup }) #draw graph (time versus counts) library(ggplot2) g = ggplot(df1, aes(x=time, y=counts)) g + geom_line()

i error message 'geom_path: each grouping consist of 1 observation. need adjust grouping aesthetic?'.

can help me rectify code allow graph plot correctly?

edit still trying formatting of time variable work, , plot graph. @ moment, it's still not recognising date format. able to: 1. plot set period of info (say 07:47:50 07:49:10). 2. inquire r plot x axis every whole minute. ... neither of these can work @ moment. subset of real info shown below. advice gratefully received.

day3 <- structure(list(date = c("11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012", "11/12/2012"), time = c("07:46:10", "07:46:20", "07:46:30", "07:46:40", "07:46:50", "07:47:00", "07:47:10", "07:47:20", "07:47:30", "07:47:40", "07:47:50", "07:48:00", "07:48:10", "07:48:20", "07:48:30", "07:48:40", "07:48:50", "07:49:00", "07:49:10", "07:49:20" ), axis1 = c(59l, 651l, 59l, 0l, 22l, 50l, 0l, 0l, 114l, 899l, 129l, 33l, 21l, 9l, 224l, 135l, 266l, 16l, 59l, 126l), steps = c(1l, 2l, 1l, 0l, 2l, 1l, 0l, 0l, 5l, 15l, 6l, 2l, 2l, 0l, 8l, 5l, 16l, 1l, 3l, 8l)), .names = c("date", "time", "axis1", "steps" ), row.names = 52838:52857, class = "data.frame") #creates new dataframe time column. day3 <- within(day3,{ posb <- as.posixlt(time,format="%h:%m:%s") posb <- null # cleanup }) library(ggplot2) g = ggplot(day3, aes(x=strptime(time, "%h:%m:%s"), y=axis1)) + geom_line(aes(group = 1)) + theme_bw() + xlab("time") + ylab("activity (counts per 10 seconds)") + scale_x_datetime(limits=c(as.posixct("07:47:50"),as.posixct("07:49:10"))) g

the problem due fact time variable character vector :

r> class(df1$time) [1] "character"

you must convet object of class posixlt, illustration :

ggplot(df1, aes(x=strptime(time, "%h:%m"), y=counts)) + geom_line()

or, much simpler, can straight utilize date variable, without transformation :

ggplot(df1, aes(x=date, y=counts)) + geom_line()

better yet, see ggplot2 automagically labels x axis depending on timespan along axis.

edit : if want define x-axis limits, can :

ggplot(df1, aes(x=date, y=counts)) + geom_line() + scale_x_datetime(limits=c(as.posixct("2012/12/07 04:00:00"),as.posixct("2012/12/07 10:00:00")))

r ggplot2

No comments:

Post a Comment