R chart by date
I've got transactional data from a SQL query which I turn into a data frame. The first column of the df contains UNIX timestamps (format="%Y/%d/%m %H:%M") which I would like to use to create a graphics plot using par to display 1 unique lineplot per date. At the moment I am fumbling around with splitting column 1 and comparing with previous ro开发者_如何学运维w to look for a change then assigning a dummy indicator to use in my plot command. Thanks, Will
Somewhat hard to answer without any example data but I'll take a shot.
I'm guessing your date looks like this: "2009-03-04 17:45"
It's probably being read as character. You can verify the class of each column of your data frame by running str(data.frame)
Using package stringr, you can just read the y/d/m and convert that to a Date class like so:
library(stringr)
date="2009-03-04 17:45"
date=as.Date(str_replace_all(str_sub(date,3,10),"-","/"), "%y/%d/%m")
You can then use date
as a group in ggplot2
to plot one line per date. You could also create separate panels (one per date) using + facet_wrap(~date)
in your ggplot call.
start by just getting the date part from your timestamp
SELECT *,DATE(timestampcolumn) as thedate FROM yourtable;
Convert date column to factor
mydf <- transform(mydf,as.factor(thedate))
Plot it with e.g. xyplot
library(lattice) xyplot(varx~vary|thedate,data=mydf)
精彩评论