1

I have a frame with temperature data and the time it was read. It looks like this:

> head(dx)
                       t Temp
1218 2012-11-13 00:01:15   79
1219 2012-11-13 00:07:19   80
1220 2012-11-13 00:15:19   78
1221 2012-11-13 00:22:57   82
1222 2012-11-13 00:30:25   78
1223 2012-11-13 00:43:19   75
...
> tail(dx)
                       t Temp
2240 2012-11-17 13:37:19  106
2241 2012-11-17 13:43:23  106
2242 2012-11-17 13:49:25  106
2243 2012-11-17 13:55:25  106
2244 2012-11-17 14:00:00  107
2245 2012-11-17 14:06:23  107

I want to show it in a graph with ticks for every day. I use

plot(dx$t, dx$Temp/10)

to display the data, which works well, but the ticks shown on x-axis are too few. I would like to show one tick for each day. What's the best way to do it? There are many questions with slightly different requests, so I got lost. If this questions was asked already I would appreciate a link.

rslite
  • 81,705
  • 4
  • 44
  • 47
  • The `zoo` package will be helpful. Take a look [here](http://stackoverflow.com/questions/4355042/label-x-axis-in-time-series-plot-using-r). – Justin Nov 17 '12 at 23:10
  • I looked at zoo and xts, but I don't have the time to read through all the documentation to really understand how they work :( This is a side project and can't allocate much time to it :( – rslite Nov 17 '12 at 23:56

1 Answers1

2

Here's one way to achieve this with base plots, but as Justin suggested, you could also look into packages that offer additional flexibility for plotting date objects, such as zoo and xts.

t <- seq(ISOdatetime(2012,1,1,11,10,25), ISOdatetime(2012,03,31,11,10,25), "hours")
dx <- data.frame(t=t, Temp=runif(length(t)))
plot(dx$t, dx$Temp/10, xaxt='n')
axis(1, at=as.POSIXct(unique(format(dx$t, '%Y-%m-%d')), format='%Y-%m-%d'),
     labels=unique(format(dx$t, '%Y-%m-%d')))
jbaums
  • 27,115
  • 5
  • 79
  • 119
  • I tried this and doesn't work (doesn't show the ticks) :(. If I change the x data from date-time to just the date it shows the ticks, but of course all the temperature data is in one column, not as I want it. I feel I'm missing something elementary here... – rslite Nov 18 '12 at 00:01
  • Is dx$t a character string, or a POSIX object? – jbaums Nov 18 '12 at 00:03
  • I've edited my answer with some fake data that hopefully better matches yours. – jbaums Nov 18 '12 at 00:19
  • Yes! :) It works. Thank you! I used strptime to convert text to date/time - I'm not sure which format that would be – rslite Nov 18 '12 at 00:57