plotting date data over couple of months

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

plotting date data over couple of months

vamshi999
Hi .. i am a beginner in R

I have data in the format of

date
04/27/11
04/27/11
04/30/11
04/30/11
05/03/11
05/03/11
05/04/11....

Now i want to plot the data something like this on x-axis...
     
             ------------------
               27 28 29 30 1 2 3 4
               April             May
I also wanted to know how to place a "*" when a date is missing from the data..

my other question is plotting the error bars.. since you can see that i have dates repeating. how to plot the error bars for count number of dates detected...

I would appreciate if someone could answer as soon as possible...

thank you in advance..
Reply | Threaded
Open this post in threaded view
|

Re: plotting date data over couple of months

David Winsemius

On Jul 13, 2011, at 6:56 PM, vamshi999 wrote:

> Hi .. i am a beginner in R
>
> I have data in the format of
>
> date
> 04/27/11
> 04/27/11
> 04/30/11
> 04/30/11
> 05/03/11
> 05/03/11
> 05/04/11....

So they are not yet dates, but rather character vectors. R dates don't  
look like that unless you make special efforts to mess them up.

>
> Now i want to plot the data something like this on x-axis...
>
>             ------------------
>               27 28 29 30 1 2 3 4
>               April             May
> I also wanted to know how to place a "*" when a date is missing from  
> the
> data..

If you convert to dates, then the axis function should be able to  
properly label a plot.
Read:

?DateTimeClasses

>
> my other question is plotting the error bars.. since you can see  
> that i have
> dates repeating. how to plot the error bars for count number of dates
> detected...

Need a better description of the data. So far you have only said you  
have dates. You should probably... no, make that definitely...  read  
the Posting Guide.

>
> I would appreciate if someone could answer as soon as possible...

Answers given to the extent possible given the limitations of the  
question.

>
> thank you in advance..
>

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: plotting date data over couple of months

vamshi999
I am sorry i was not clear in my early post....

I have used the below code to change my data into date format..

r<-as.POSIXlt(paste(data2$date,data2$time))
library(gsubfn)
library(chron)
r<-as.chron(r)

data2$date<-strftime(strptime(r, format="(%m/%d/%y %H:%M:%S)"),"%Y-%m-%d")
data2$time<-strftime(strptime(r, format="(%m/%d/%y %H:%M:%S)"),"%H:%M:%S")

now i have a date variable in the below format..

"2011-04-20"

while plotting dates using the above format.. i was getting the x-axis in this way..

        ----------------------
        2011-04-29    2011-05-05

but i want the values in my x-axis to be in this format..

                 ------------------
               27 28 29 30 1 2 3 4
               April             May


about the error plots.... i was not able to compute the mean and sd values ..

My data has  the following columns..

date, time, height..

 this is the bird data which we recorded.. so i have date, time, height at which the birds are being detected . so now i want to plot errorbar for total no.of birds detected on each day..

for a simple plot ..i used.. counts<-table(data$date).. i was able to get the count on each day..

then.. barplot(counts).. but i was not able to calculate the mean and sd for counts..

thank you
Reply | Threaded
Open this post in threaded view
|

Re: plotting date data over couple of months

David Winsemius


So, post some data. I'm tired of unsuccessful attempts at mind  
reading. If the data set is too big, then just use:

dput(data2[which( data2$date < as.Date("2011-05-05") &
                                           data2$date >  
as.Date("2011-04-27") ),
           ]
     )

--
David




On Jul 13, 2011, at 8:09 PM, vamshi999 wrote:

> I am sorry i was not clear in my early post....
>
> I have used the below code to change my data into date format..
>
> r<-as.POSIXlt(paste(data2$date,data2$time))
> library(gsubfn)
> library(chron)
> r<-as.chron(r)
>
> data2$date<-strftime(strptime(r, format="(%m/%d/%y %H:%M:%S)"),"%Y-
> %m-%d")
> data2$time<-strftime(strptime(r, format="(%m/%d/%y %H:%M:%S)"),"%H:
> %M:%S")
>
> now i have a date variable in the below format..
>
> "2011-04-20"
>
> while plotting dates using the above format.. i was getting the x-
> axis in
> this way..
>
>        ----------------------
>        2011-04-29    2011-05-05
>
> but i want the values in my x-axis to be in this format..
>
>                 ------------------
>               27 28 29 30 1 2 3 4
>               April             May
>
>
> about the error plots.... i was not able to compute the mean and sd  
> values
> ..
>
> My data has  the following columns..
>
> date, time, height..
>
> this is the bird data which we recorded.. so i have date, time,  
> height at
> which the birds are being detected . so now i want to plot errorbar  
> for
> total no.of birds detected on each day..
>
> for a simple plot ..i used.. counts<-table(data$date).. i was able  
> to get
> the count on each day..
>
> then.. barplot(counts).. but i was not able to calculate the mean  
> and sd for
> counts..
>
> thank you


David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: plotting date data over couple of months

vamshi999
I am sorry again..

I am posting the sample data..

my data set is huge.. dimension of my data set is 50000 x 6
   scan.no    bird.no             date                   time            height
"1" 24       5 "2011-04-29" "21:17:56"      357
"2" 78     48 "2011-04-29" "21:20:08"      131
"3" 61    105 "2011-04-29" "21:24:58"      432
"4" 64    122 "2011-04-29" "21:25:05"      673
"5" 64    124 "2011-04-30" "21:25:05"      123
"6" 64    121 "2011-04-30" "21:25:05"      479
"7" 65    123 "2011-05-03" "21:25:07"      269
"8" 73    380 "2011-05-03" "21:25:40"      131
"9" 79  1073 "2011-05-05" "21:25:41"      356
"10" 82  1093 "2011-05-05" "21:26:24"      378
"11" 82  1101 "2011-05-05" "21:27:30"      412

I have dates values from april and whole month of may (except some days.. when i was not able to record the data)

thank you

Reply | Threaded
Open this post in threaded view
|

Re: plotting date data over couple of months

David Winsemius

On Jul 13, 2011, at 8:32 PM, vamshi999 wrote:

> I am sorry again..
>
> I am posting the sample data..
>
> my data set is huge.. dimension of my data set is 50000 x 6
>   scan.no    bird.no             date                   time
> height
> "1" 24       5 "2011-04-29" "21:17:56"      357
> "2" 78     48 "2011-04-29" "21:20:08"      131
> "3" 61    105 "2011-04-29" "21:24:58"      432
> "4" 64    122 "2011-04-29" "21:25:05"      673
> "5" 64    124 "2011-04-30" "21:25:05"      123
> "6" 64    121 "2011-04-30" "21:25:05"      479
> "7" 65    123 "2011-05-03" "21:25:07"      269
> "8" 73    380 "2011-05-03" "21:25:40"      131
> "9" 79  1073 "2011-05-05" "21:25:41"      356
> "10" 82  1093 "2011-05-05" "21:26:24"      378
> "11" 82  1101 "2011-05-05" "21:27:30"      412
>
> I have dates values from april and whole month of may (except some  
> days..
> when i was not able to record the data)
>

You are exhibiting discourteous posting behavior by failing to include  
context. Nabble is not r-help. Read the Posting Guide.

Here is a worked example using base plotting functions. The data was  
read into a dataframe with this structure:

 > str(dat)
'data.frame': 11 obs. of  5 variables:
  $ scan.no: num  24 78 61 64 64 64 65 73 79 82 ...
  $ bird.no: num  5 48 105 122 124 ...
  $ date   : Date, format: "2011-04-29" "2011-04-29" ...
  $ time   : chr  "21:17:56" "21:20:08" "21:24:58" "21:25:05" ...
  $ height : num  357 131 432 673 123 479 269 131 356 378 ...


plot(dat$date, dat$height, xaxt="n")
  axis(1, at=dat$date[c(1,11)],     # only plot beginning and ending  
month names
         labels=month.abb[ 1+as.POSIXlt(dat$date[c(1,11)])$mon], # add  
1 to index
                              # the month.abb constant vector
         line=2,              # shifts these down
         lwd=0)  # this sppress the extra axis lines
  axis(1, at=dat$date, labels=as.POSIXlt(dat$date)$mday)  # plot the  
days

The aggregate function could be used to calculate daily means and sd's  
you mentioned in an earlier posting.

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: plotting date data over couple of months

vamshi999
i am sorry again..

this is my dataset..


'data.frame':   46376 obs. of  19 variables:
 $ scan.no  : int  24 78 61 64 64 64 65 73 79 82 ...
 $ track.no : int  5 48 105 122 124 121 123 380 1073 1093 ...
 $ blip.no  : int  70 323 514 547 549 553 558 5355 7014 7119 ...
 $ date     : chr  "2011-04-29" "2011-04-29" "2011-04-29" "2011-04-29" ...
 $ time     : chr  "18:17:56" "18:20:08" "18:24:58" "18:25:05" ...
 $ timestamp: num  1.30e+09 1.30e+09 1.30e+09 1.30e+09 1.30e+09 ...
 $ range    : int  1379 937 555 562 562 555 545 2708 593 2354 ...
 $ x        : int  -997 -487 -1 230 139 -1 223 -2362 163 2017 ...
 $ y        : int  -883 -763 -536 -491 -525 -536 -477 1123 -549 1049 ...
 $ z        : int  357 243 144 145 146 144 141 701 153 609 ...
 $ ns       : int  378 72 72 98 115 72 41 31 44 32 ...
 $ area     : int  16579 2144 1270 1750 2061 1270 710 2668 829 2394 ...
 $ int      : num  0.96 0.82 1 0.999 0.969 1 1 0.816 1 1 ...
 $ max      : num  1 0.984 1 1 1 1 1 0.933 1 1 ...
 $ aspan    : int  69 28 36 40 44 36 40 13 12 8 ...
 $ rspan    : int  8 5 2 4 4 2 2 6 5 4 ...
 $ perim    : int  670 613 125 288 206 125 108 445 221 211 ...
 $ Dir      : num  1 1 1 2 1 1 2 1 2 1 ...
 $ bearing  : num  228 213 180 155 165 ...


now previously i have not mentioned these variable because i will be removing them eventually....

when i run the above code.. i am getting this error message..

plot(dat$date, dat$height, xaxt="n")
>   axis(1, at=new_data2$date[c(1,46376)],
+ labels=month.abb[ 1+as.POSIXlt(new_data2$date[c(1,46376)])$mon],
+  line=2,
+ lwd=0)

Error in axis(1, at = new_data2$date[c(1, 46376)], labels = month.abb[1 +  :
  no locations are finite
In addition: Warning message:
In axis(1, at = new_data2$date[c(1, 46376)], labels = month.abb[1 +  :
  NAs introduced by coercion

axis(1, at=new_data2$date, labels=as.POSIXlt(new_data2$date)$mday)
Error in axis(1, at = new_data2$date, labels = as.POSIXlt(new_data2$date)$mday) :
  no locations are finite
In addition: Warning message:
In axis(1, at = new_data2$date, labels = as.POSIXlt(new_data2$date)$mday) :
  NAs introduced by coercion

i am looking for an output same as the plot in the image below..

y - axis is the mean passage rate ( total no. of birds /hr)

i have read the posting guide as you have suggested.....

thank you ..
Reply | Threaded
Open this post in threaded view
|

Re: plotting date data over couple of months

David Winsemius

On Jul 13, 2011, at 9:42 PM, vamshi999 wrote:

> i am sorry again..
>
> this is my dataset..
>
>
> 'data.frame':   46376 obs. of  19 variables:
> $ scan.no  : int  24 78 61 64 64 64 65 73 79 82 ...
> $ track.no : int  5 48 105 122 124 121 123 380 1073 1093 ...
> $ blip.no  : int  70 323 514 547 549 553 558 5355 7014 7119 ...
> $ date     : chr  "2011-04-29" "2011-04-29" "2011-04-29"  
> "2011-04-29" ...

You told us this was a date. So I used ... , colClasses=c("character",  
"numeric","numeric", "Date", "character", "numeric") in my read  
statement.

But. It's a character vector.

> $ time     : chr  "18:17:56" "18:20:08" "18:24:58" "18:25:05" ...

And so is time

> $ timestamp: num  1.30e+09 1.30e+09 1.30e+09 1.30e+09 1.30e+09 ...

And I cannot tell what information might be in time stamp.

> $ range    : int  1379 937 555 562 562 555 545 2708 593 2354 ...
> $ x        : int  -997 -487 -1 230 139 -1 223 -2362 163 2017 ...
> $ y        : int  -883 -763 -536 -491 -525 -536 -477 1123 -549  
> 1049 ...
> $ z        : int  357 243 144 145 146 144 141 701 153 609 ...
> $ ns       : int  378 72 72 98 115 72 41 31 44 32 ...
> $ area     : int  16579 2144 1270 1750 2061 1270 710 2668 829 2394 ...
> $ int      : num  0.96 0.82 1 0.999 0.969 1 1 0.816 1 1 ...
> $ max      : num  1 0.984 1 1 1 1 1 0.933 1 1 ...
> $ aspan    : int  69 28 36 40 44 36 40 13 12 8 ...
> $ rspan    : int  8 5 2 4 4 2 2 6 5 4 ...
> $ perim    : int  670 613 125 288 206 125 108 445 221 211 ...
> $ Dir      : num  1 1 1 2 1 1 2 1 2 1 ...
> $ bearing  : num  228 213 180 155 165 ...
>
>
> now previously i have not mentioned these variable because i will be
> removing them eventually....
>
> when i run the above code.. i am getting this error message..
>
> plot(dat$date, dat$height, xaxt="n")
>>  axis(1, at=new_data2$date[c(1,46376)],
> + labels=month.abb[ 1+as.POSIXlt(new_data2$date[c(1,46376)])$mon],
> +  line=2,
> + lwd=0)
>
> Error in axis(1, at = new_data2$date[c(1, 46376)], labels =  
> month.abb[1 +  :
>  no locations are finite
> In addition: Warning message:
> In axis(1, at = new_data2$date[c(1, 46376)], labels = month.abb[1 +  :
>  NAs introduced by coercion


So you need to change a character vector to a date for which the  
function as.Date or strptime are useful

Try this to get dates:

dat$dt.real <- as.Date(dat$date)  # should work with the default format

And then the code should give reasonable results.

Further But; reading down below, you want values per hour... funny  
this is the first I heard of this.

dt.time.real <- strptime (paste( dat$date, dat$time), fformat="%Y-%m-
%d %H:%M:%S")

Now I see the plot. The plot linked below has one level per date so I  
guess you are intending to just divide daily counts by 24. Jeez. All  
that work on getting times was wasted. It's midnight. I'm going to bed.

The plotting of what are known in these parts as "dynamite plots" is  
supported by functions in plotrix and other packages.
Search in RSiteSearch for plotCI or similar. Also Bolker has a page:  http://emdbolker.wikidot.com/blog:dynamite
And Harrell: http://biostat.mc.vanderbilt.edu/twiki/bin/view/Main/DynamitePlots

>
> axis(1, at=new_data2$date, labels=as.POSIXlt(new_data2$date)$mday)
> Error in axis(1, at = new_data2$date, labels =
> as.POSIXlt(new_data2$date)$mday) :
>  no locations are finite
> In addition: Warning message:
> In axis(1, at = new_data2$date, labels = as.POSIXlt(new_data2$date)
> $mday) :
>  NAs introduced by coercion
>
> i am looking for an output same as the plot in the image below..
> http://r.789695.n4.nabble.com/file/n3666468/graph.jpg
> y - axis is the mean passage rate ( total no. of birds /hr)
>
> i have read the posting guide as you have suggested.....
>
> thank you ..
>
> --
> View this message in context: http://r.789695.n4.nabble.com/plotting-date-data-over-couple-of-months-tp3666298p3666468.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: plotting date data over couple of months

vamshi999
This post was updated on .
thank you for your reply..

As i have told you earlier... i want to plot the total no.of birds counted for each day and plot total no.of birds for each day.. one level for each day ..

i wanted to normalize the data.. since i don't have the data for equal no.of hours for all days.. for example
on 2011-04-23 i have a data for 2 hours.. but on 2011-05-03 i have data for 8 hours.. that is why i am dividing the data by the no.of hours of data on that particular day..

so... i will create a dataset with dates from the whole month using the above axis command you have mentioned.. .. and plot my data for these values..

since my data has some dates missing.. i won't get any bars on those particular days.... how can i place a * on those missing days..


thank you