How to plot sunrise over a year in R? -


i have date looks this:

"date", "sunrise" 2009-01-01, 05:31 2009-01-02, 05:31 2009-01-03, 05:33 2009-01-05, 05:34 .... 2009-12-31, 05:29 

and want plot in r, "date" x-axis, , "sunrise" y-axis.

you need work bit harder r draw suitable plot (i.e. suitable axes). have data similar yours (here in csv file convenience:

"date","sunrise" 2009-01-01,05:31 2009-01-02,05:31 2009-01-03,05:33 2009-01-05,05:34 2009-01-06,05:35 2009-01-07,05:36 2009-01-08,05:37 2009-01-09,05:38 2009-01-10,05:39 2009-01-11,05:40 2009-01-12,05:40 2009-01-13,05:41 

we can read data in and format appropriately r knows special nature of data. read.csv() call includes argument colclasses r doesn't convert dates/times factors.

dat <- read.csv("foo.txt", colclasses = "character") ## convert imported data appropriate types dat <- within(dat, { date <- as.date(date) ## no need 'format' argument data in correct format sunrise <- as.posixct(sunrise, format = "%h:%m") }) str(dat) 

now comes tricky bit r gets axes wrong (or perhaps better aren't want) if do

plot(sunrise ~ date, data = dat) ## or with(dat, plot(date, sunrise)) 

the first version gets both axes wrong, , second can dispatch correctly on dates gets x-axis correct, y-axis labels not right.

so, suppress plotting of axes, , add them using axis.foo functions foo date or posixct:

plot(sunrise ~ date, data = dat, axes = false) with(dat, axis.posixct(x = sunrise, side = 2, format = "%h:%m")) with(dat, axis.date(x = date, side = 1)) box() ## complete plot frame 

hth


Comments

Popular posts from this blog

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -

openssl - Load PKCS#8 binary key into Ruby -