开发者

ggplot2: time scale graphing question

开发者 https://www.devze.com 2023-04-05 16:31 出处:网络
I have a some GPS data from my Garmin I\'d like to graph time scaled. I found some example on the \'net for using ggplot2 to do this, but have had issues with it - I believe i might need to reformat m

I have a some GPS data from my Garmin I'd like to graph time scaled. I found some example on the 'net for using ggplot2 to do this, but have had issues with it - I believe i might need to reformat my datetime column, but couldn't figure it out searching 'net and looking at the ggplot2 source code. Any help much appreciated.

> library(ggplot2)
> x <- read.csv("tst.csv",header=TRUE,colClasses="character")
> head(x)
  counter HeartRateBpm_AVG LastTimeStamp
1       1           102.25 9/3/2011 7:32
2       2           138.75 9/3/2011 7:33
3       3           138.75 9/3/2011 7:33
4       4           138.75 9/3/2011 7:33
5       5           138.75 9/3/2011 7:33
6       6           138.75 9/3/2011 7:34
> ggplot(data=x,aes(x=LastTimeStamp,y=HeartRat开发者_Go百科eBpm_AVG)) + scale_x_date(format = "%S") + geom_line()


I do not believe you could have used that input process to get that data.frame. You only have one item in the "colClasses" vector. When I used your method, the result was not much different than if I had used readLines. Your date-time column is not printing as though it made it in in a manner that could be converted without a format-spec. Try instead:

x <- read.csv(textConnection("  counter, HeartRateBpm_AVG, LastTimeStamp
 1 ,         102.25, 9/3/2011 7:32
 2  ,        138.75 ,9/3/2011 7:33
 3   ,       138.75, 9/3/2011 7:33
 4    ,      138.75 ,9/3/2011 7:33
 5     ,     138.75, 9/3/2011 7:33
 6      ,    138.75 ,9/3/2011 7:34"), 
         header=TRUE,  colClasses=c("numeric", "numeric", "character"))
# Then the formating as date-time works well.
x$LastTimeStamp2 <- as.POSIXct(x$LastTimeStamp, format="%m/%d/%Y %H:%M")
 head(x)


Here is one solution

x = transform(x, LastTimeStamp = as.POSIXct(LastTimeStamp))
qplot(format(LastTimeStamp), HeartRateBpm_Avg, data = x)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号