Skip to content
Prev 304363 / 398503 Next

date in plot, can't add regression line

First of all, a practical way to supply data is to use the function dput()

Just do dput(mydata) and copy and paste the results into your email.  The reader can copy and paste into R and have an identical data set.

I am not sure I have followed exactly what you are doing but here is something that may approach what you want, done using the ggp;pt2 package.  Do 'install.packages("ggplot2) if you do not have it.

Anyway here is roughly your data set in the dput format

mydata  <-  structure(list(tradeflavor = c(1L, 2L, 1L, 1L, 1L, 2L, 16L, 1L, 
16L, 1L, 1L, 16L, 1L, 4L, 8L, 1L, 8L, 16L, 1L, 8L, 16L, 1L, 4L, 
8L, 16L, 1L, 2L, 4L, 8L, 1L, 4L, 8L, 16L, 1L, 2L, 4L, 8L, 16L, 
1L, 2L), timestamp = structure(c(14266, 14266, 14267, 14271, 
14272, 14272, 14272, 14273, 14273, 14274, 14277, 14277, 14278, 
14278, 14278, 14279, 14279, 14279, 14280, 14280, 14280, 14281, 
14281, 14281, 14281, 14284, 14284, 14284, 14284, 14285, 14285, 
14285, 14285, 14286, 14286, 14286, 14286, 14286, 14287, 14287
), class = "Date"), x = c(1L, 1L, 1L, 54L, 105L, 2L, 2L, 71L, 
2L, 42L, 19L, 2L, 36L, 2L, 3L, 73L, 12L, 7L, 53L, 6L, 9L, 38L, 
6L, 2L, 3L, 42L, 2L, 1L, 2L, 87L, 2L, 4L, 3L, 55L, 6L, 4L, 2L, 
8L, 153L, 6L)), .Names = c("tradeflavor", "timestamp", "x"), row.names = c(NA, 
-40L), class = "data.frame")

#=====================================
library(ggplot2)

# first subset

m1data  <-  subset(mydata, tradeflavor == 1)

# plot for tradeflavor = 1
p1   <-  ggplot(m1data , aes( timestamp, x)) + geom_point()   +
           geom_smooth(method = lm, se = FALSE)
p1

m2data  <-  subset(mydata, tradeflavor == 2)

p2   <-  ggplot(m2data , aes( timestamp, x )) + geom_point()   +
           geom_smooth(method = lm, se = FALSE)
p2

# plot a grid of results
pgrid  <-  p   <-  ggplot(mydata , aes( timestamp, x)) + geom_point()   +
           geom_smooth(method = lm, se = FALSE) + facet_grid(tradeflavor ~ .)
pgrid

# Have fun with R.




John Kane
Kingston ON Canada
____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails