need help with a time series plotting problem

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

need help with a time series plotting problem

vibhava
Dear R Users,
                     I am a beginner in R programming and need some help with a simple plotting problem that i am having. My dataset consist of three columns: first one has data_id, second is the date and third is the actual data itself corresponding to each date. The date ranges from 1/1/2000-12/31/2009. I am trying to plot my data versus the dates as a long term time series but what's happening is that R is plotting each year on top of the previous year. so instead of getting 1 line (dated 2000-2009) in the plot i am getting 9 lines (1 line for each year). i tried to look for solutions online but found nothing. can someone suggest how can i make a plot with x-axis ranging from 2000-2009. my code is copied below:

setwd("J:/Rstuff/flow")
flow=read.delim("flow.dat",header=TRUE,sep="\t")
plot(flow$usgs1500~as.Date(flow$date, "%m/%d/%y"),type="l",xlab="date",ylab="daily discharge (m3/s) ",main="USGS1500",yaxs="i", xaxs="i",)


any help would be appreciated

regards

vibhava  
Reply | Threaded
Open this post in threaded view
|

Re: need help with a time series plotting problem

jholtman
you need to supply a subset of your data since the problem is probably related to its representation.  please follow the posting guidelines.

Sent from my iPad

On Dec 24, 2011, at 2:21, vibhava <[hidden email]> wrote:

> Dear R Users,
>                     I am a beginner in R programming and need some help
> with a simple plotting problem that i am having. My dataset consist of three
> columns: first one has data_id, second is the date and third is the actual
> data itself corresponding to each date. The date ranges from
> 1/1/2000-12/31/2009. I am trying to plot my data versus the dates as a long
> term time series but what's happening is that R is plotting each year on top
> of the previous year. so instead of getting 1 line (dated 2000-2009) in the
> plot i am getting 9 lines (1 line for each year). i tried to look for
> solutions online but found nothing. can someone suggest how can i make a
> plot with x-axis ranging from 2000-2009. my code is copied below:
>
> setwd("J:/Rstuff/flow")
> flow=read.delim("flow.dat",header=TRUE,sep="\t")
> plot(flow$usgs1500~as.Date(flow$date,
> "%m/%d/%y"),type="l",xlab="date",ylab="daily discharge (m3/s)
> ",main="USGS1500",yaxs="i", xaxs="i",)
>
>
> any help would be appreciated
>
> regards
>
> vibhava  
>
> --
> View this message in context: http://r.789695.n4.nabble.com/need-help-with-a-time-series-plotting-problem-tp4230672p4230672.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.

______________________________________________
[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: need help with a time series plotting problem

vibhava
thanks for the reply. here is subset of the data that i want to plot:
        date usgs700
1 10/1/2000 0.050970325
2 10/2/2000 0.041059428
3 10/3/2000 0.032564374
4 10/4/2000 0.02775051
.
.
.
.
.

4014 9/27/2011 0
4015 9/28/2011 0
4016 9/29/2011 0
4017 9/30/2011 0

my script again is:

setwd("J:/Rstuff/flow")
flow=read.delim("flow.dat",header=TRUE,sep="\t")
plot(flow$usgs1500~as.Date(flow$date, "%m/%d/%y"),type="l",xlab="date",ylab="daily discharge (m3/s) ",main="USGS1500",yaxs="i", xaxs="i",)


i wish to plot all this data as a single time series but my program is doing some weird stuff and the x-axis has labels Jan-dec and the long term series is broken and plotted on yearly basis.

regards

vibhava
Reply | Threaded
Open this post in threaded view
|

Re: need help with a time series plotting problem

David Winsemius

On Dec 24, 2011, at 9:24 AM, vibhava wrote:

> thanks for the reply. here is subset of the data that i want to plot:
> date usgs700
> 1 10/1/2000 0.050970325
> 2 10/2/2000 0.041059428
> 3 10/3/2000 0.032564374
> 4 10/4/2000 0.02775051
> .
> .

Instead of posting print-output why not post the results of  
dput(head(flow))

> > further  useless output deleted.

>
> my script again is:
>
> setwd("J:/Rstuff/flow")
> flow=read.delim("flow.dat",header=TRUE,sep="\t")
> plot(flow$usgs1500

There's no column with that name.

> ~as.Date(flow$date,
> "%m/%d/%y")

For another thing, you generally  need to use %Y for 4 digit years.

> ,type="l",xlab="date",ylab="daily discharge (m3/s)
> ",main="USGS1500",yaxs="i", xaxs="i",)

Not sure if that empty argument is going to be handled gracefully.

> i wish to plot all this data as a single time series but my program  
> is doing
> some weird stuff and the x-axis has labels Jan-dec and the long term  
> series
> is broken and plotted on yearly basis.

Your code is not reproducible.


--
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: need help with a time series plotting problem

vibhava
sorry about the change in variable name (data has column named usgs700 but code has usgs700). actually i am trying to get familiar with R as i need to make more complex time series plots in near future (stackplots, scatterplot etc.). Let me try to explain what i am intending to achieve here. below i have copied first few lines of a large dataset (daily flow records for 4017 days at 8 usgs locations). first column is date and rest of them are flow data measured at these 8 different location. now all i wish to do is to read them in and make time series plots for the entire period of record for each stations. i have attached some figures that i made in excel and i wish to do something like this using R.

I know my code might have some errors and that's the reason why i am requesting for help from people who know R better than I do. i am R user for less than a day but i know what i am trying to do is really simple and all i need is to read 9 columns and make a simple time series plot.

i would appreciate if anyone can correct the code that i have written below or if they have some alternate way of doing this i would be happy to learn something new

regards

vibhava

Date USGS700 USGS1000 USGS1500 USGS 1898 USGS1975 USGS2500 USGS2700 USGS2800
10/1/2000 0.05 0.57 2.32 2.27 4.11 29.45 29.45 29.45
10/2/2000 0.04 0.54 2.12 1.70 4.05 29.17 29.17 29.17
10/3/2000 0.03 0.48 1.93 1.98 3.96 28.88 28.88 28.88
10/4/2000 0.03 0.45 1.76 1.42 3.91 28.60 28.60 28.60
10/5/2000 0.03 0.42 1.64 1.27 3.82 28.32 28.32 28.32
10/6/2000 0.03 0.42 1.53 1.13 3.74 28.26 28.26 28.26
10/7/2000 0.11 0.51 1.59 5.66 3.68 28.23 28.23 28.23
10/8/2000 0.16 0.45 1.70 3.40 3.62 27.84 27.84 27.84
10/9/2000 0.10 0.42 1.78 2.55 3.54 26.56 26.56 26.56

> setwd("J:/Rstuff/flow")                                                                                     # defining the working directory
> flow=read.delim("flow.dat",header=TRUE,sep="\t")                              # opening the above tab separated data file
> plot(flow$USGS1500~as.Date(flow$Date, "%m/%d/%y"),type="l",xlab="date",ylab="daily discharge (m3/s)
> ",main="USGS1500",yaxs="i", xaxs="i")                                                       #just to test my code, i am trying to make time series  
                                                                                                                                     #plot of 1 variable USGS1500

flow.docx
Reply | Threaded
Open this post in threaded view
|

Re: need help with a time series plotting problem

jholtman
This should plot all the columns for you:

flow <- read.table(text = "Date    USGS700 USGS1000        USGS1500
    USGS1898       USGS1975        USGS2500        USGS2700
USGS2800
10/1/2000       0.05    0.57    2.32    2.27    4.11    29.45   29.45   29.45
10/2/2000       0.04    0.54    2.12    1.70    4.05    29.17   29.17   29.17
10/3/2000       0.03    0.48    1.93    1.98    3.96    28.88   28.88   28.88
10/4/2000       0.03    0.45    1.76    1.42    3.91    28.60   28.60   28.60
10/5/2000       0.03    0.42    1.64    1.27    3.82    28.32   28.32   28.32
10/6/2000       0.03    0.42    1.53    1.13    3.74    28.26   28.26   28.26
10/7/2000       0.11    0.51    1.59    5.66    3.68    28.23   28.23   28.23
10/8/2000       0.16    0.45    1.70    3.40    3.62    27.84   27.84   27.84
10/9/2000       0.10    0.42    1.78    2.55    3.54    26.56   26.56   26.56"
    , as.is = TRUE
    , header = TRUE
    )

# setup margins
par(mar = c(5, 5, 3, 3))

# plot each column of data
for (i in names(flow)[-1]){  # ignore the "Date" column
    plot(as.Date(flow$Date, "%m/%d/%y")
        , flow[[i]]  # column to plot
        ,type="l"
        ,xlab="date"
        ,ylab=expression("daily discharge (" * m^3/s * ")")
        ,main=i
        ,yaxs="i"
        , xaxs="i"
        )
}


On Sat, Dec 24, 2011 at 1:20 PM, vibhava <[hidden email]> wrote:

> sorry about the change in variable name (data has column named usgs700 but
> code has usgs700). actually i am trying to get familiar with R as i need to
> make more complex time series plots in near future (stackplots, scatterplot
> etc.). Let me try to explain what i am intending to achieve here. below i
> have copied first few lines of a large dataset (daily flow records for 4017
> days at 8 usgs locations). first column is date and rest of them are flow
> data measured at these 8 different location. now all i wish to do is to read
> them in and make time series plots for the entire period of record for each
> stations. i have attached some figures that i made in excel and i wish to do
> something like this using R.
>
> I know my code might have some errors and that's the reason why i am
> requesting for help from people who know R better than I do. i am R user for
> less than a day but i know what i am trying to do is really simple and all i
> need is to read 9 columns and make a simple time series plot.
>
> i would appreciate if anyone can correct the code that i have written below
> or if they have some alternate way of doing this i would be happy to learn
> something new
>
> regards
>
> vibhava
>
> Date    USGS700 USGS1000        USGS1500        USGS 1898       USGS1975        USGS2500        USGS2700        USGS2800
> 10/1/2000       0.05    0.57    2.32    2.27    4.11    29.45   29.45   29.45
> 10/2/2000       0.04    0.54    2.12    1.70    4.05    29.17   29.17   29.17
> 10/3/2000       0.03    0.48    1.93    1.98    3.96    28.88   28.88   28.88
> 10/4/2000       0.03    0.45    1.76    1.42    3.91    28.60   28.60   28.60
> 10/5/2000       0.03    0.42    1.64    1.27    3.82    28.32   28.32   28.32
> 10/6/2000       0.03    0.42    1.53    1.13    3.74    28.26   28.26   28.26
> 10/7/2000       0.11    0.51    1.59    5.66    3.68    28.23   28.23   28.23
> 10/8/2000       0.16    0.45    1.70    3.40    3.62    27.84   27.84   27.84
> 10/9/2000       0.10    0.42    1.78    2.55    3.54    26.56   26.56   26.56
>
>> setwd("J:/Rstuff/flow")
>> # defining the working directory
>> flow=read.delim("flow.dat",header=TRUE,sep="\t")
>> # opening the above tab separated data file
>> plot(flow$USGS1500~as.Date(flow$Date,
>> "%m/%d/%y"),type="l",xlab="date",ylab="daily discharge (m3/s)
>> ",main="USGS1500",yaxs="i", xaxs="i")
>> #just to test my code, i am trying to make time series
>
> #plot of 1 variable USGS1500
>
> http://r.789695.n4.nabble.com/file/n4231737/flow.docx flow.docx
>
> --
> View this message in context: http://r.789695.n4.nabble.com/need-help-with-a-time-series-plotting-problem-tp4230672p4231737.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.



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
[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: need help with a time series plotting problem

vibhava
In reply to this post by vibhava
thanks for your reply. i don't think your solution is what i am looking for. Please look at the attached two plots; first plot is what i have made using excel and the second plot is what i was getting when i was running my script through R yesterday night. I think my script was reading and implementing things nicely but the only problem was that at the end of each year my time series line use to start from the same axis label (instead of keep creating more labels and extending the x-axis.
                      therefore what happened was that unlike the excel plot which has x-axis from oct00-oct2011, i have x-axis labels jan-nov.  i am looking for something similar to excel where you select the axis and right click on it and you can extend the x-axis.
                     so to sum up i just need to know how to extend my x-axis (see attached figures) so that i get a continuous line (i need one line for each plot representing 11 years of record AND DO NOT want 11 lines representing the same thing).

regards

vibhava
flow.docx
Reply | Threaded
Open this post in threaded view
|

Re: need help with a time series plotting problem

jholtman
In reply to this post by jholtman
Try this; this should put the labels on the x-axis like Excel does:

flow <- read.table(text = "Date    USGS700 USGS1000        USGS1500
USGS1898       USGS1975        USGS2500        USGS2700 USGS2800
10/1/2001       0.05    0.57    2.32    2.27    4.11    29.45   29.45   29.45
10/1/2002       0.04    0.54    2.12    1.70    4.05    29.17   29.17   29.17
10/1/2003       0.03    0.48    1.93    1.98    3.96    28.88   28.88   28.88
10/1/2004       0.03    0.45    1.76    1.42    3.91    28.60   28.60   28.60
10/1/2005       0.03    0.42    1.64    1.27    3.82    28.32   28.32   28.32
10/1/2006       0.03    0.42    1.53    1.13    3.74    28.26   28.26   28.26
10/1/2007       0.11    0.51    1.59    5.66    3.68    28.23   28.23   28.23
10/1/2008       0.16    0.45    1.70    3.40    3.62    27.84   27.84   27.84
10/1/2009       0.10    0.42    1.78    2.55    3.54    26.56   26.56   26.56"

   , as.is = TRUE
   , header = TRUE
   )

# setup margins
par(mar = c(5, 5, 3, 3))

# create x-axis with yearly tick marks starting in Oct
x_range <- seq(from = as.Date("2001-10-1")
            , to = as.Date("2011-10-1")
            , by = '1 year'
            )
# now create the format of month-year
x_label <- sprintf("%s-%02d", format(x_range, "%b"),
as.POSIXlt(x_range)$year %% 100)

# plot each column of data
for (i in names(flow)[-1]){  # ignore the "Date" column
   plot(as.Date(flow$Date, "%m/%d/%Y")
       , flow[[i]]  # column to plot
       ,type="l"
       ,xlab="date"
       ,ylab=expression("daily discharge (" * m^3/s * ")")
       ,main=i
       ,yaxs="i"
       , xaxs="i"
       , xaxt = 'n'
       , xlim = range(x_range)
       )

   axis(1, at = x_range, labels = x_label, las = 2)
}




On Sat, Dec 24, 2011 at 3:23 PM, jim holtman <[hidden email]> wrote:

> This should plot all the columns for you:
>
> flow <- read.table(text = "Date    USGS700 USGS1000        USGS1500
>    USGS1898       USGS1975        USGS2500        USGS2700
> USGS2800
> 10/1/2000       0.05    0.57    2.32    2.27    4.11    29.45   29.45   29.45
> 10/2/2000       0.04    0.54    2.12    1.70    4.05    29.17   29.17   29.17
> 10/3/2000       0.03    0.48    1.93    1.98    3.96    28.88   28.88   28.88
> 10/4/2000       0.03    0.45    1.76    1.42    3.91    28.60   28.60   28.60
> 10/5/2000       0.03    0.42    1.64    1.27    3.82    28.32   28.32   28.32
> 10/6/2000       0.03    0.42    1.53    1.13    3.74    28.26   28.26   28.26
> 10/7/2000       0.11    0.51    1.59    5.66    3.68    28.23   28.23   28.23
> 10/8/2000       0.16    0.45    1.70    3.40    3.62    27.84   27.84   27.84
> 10/9/2000       0.10    0.42    1.78    2.55    3.54    26.56   26.56   26.56"
>    , as.is = TRUE
>    , header = TRUE
>    )
>
> # setup margins
> par(mar = c(5, 5, 3, 3))
>
> # plot each column of data
> for (i in names(flow)[-1]){  # ignore the "Date" column
>    plot(as.Date(flow$Date, "%m/%d/%y")
>        , flow[[i]]  # column to plot
>        ,type="l"
>        ,xlab="date"
>        ,ylab=expression("daily discharge (" * m^3/s * ")")
>        ,main=i
>        ,yaxs="i"
>        , xaxs="i"
>        )
> }
>
>
> On Sat, Dec 24, 2011 at 1:20 PM, vibhava <[hidden email]> wrote:
>> sorry about the change in variable name (data has column named usgs700 but
>> code has usgs700). actually i am trying to get familiar with R as i need to
>> make more complex time series plots in near future (stackplots, scatterplot
>> etc.). Let me try to explain what i am intending to achieve here. below i
>> have copied first few lines of a large dataset (daily flow records for 4017
>> days at 8 usgs locations). first column is date and rest of them are flow
>> data measured at these 8 different location. now all i wish to do is to read
>> them in and make time series plots for the entire period of record for each
>> stations. i have attached some figures that i made in excel and i wish to do
>> something like this using R.
>>
>> I know my code might have some errors and that's the reason why i am
>> requesting for help from people who know R better than I do. i am R user for
>> less than a day but i know what i am trying to do is really simple and all i
>> need is to read 9 columns and make a simple time series plot.
>>
>> i would appreciate if anyone can correct the code that i have written below
>> or if they have some alternate way of doing this i would be happy to learn
>> something new
>>
>> regards
>>
>> vibhava
>>
>> Date    USGS700 USGS1000        USGS1500        USGS 1898       USGS1975        USGS2500        USGS2700        USGS2800
>> 10/1/2000       0.05    0.57    2.32    2.27    4.11    29.45   29.45   29.45
>> 10/2/2000       0.04    0.54    2.12    1.70    4.05    29.17   29.17   29.17
>> 10/3/2000       0.03    0.48    1.93    1.98    3.96    28.88   28.88   28.88
>> 10/4/2000       0.03    0.45    1.76    1.42    3.91    28.60   28.60   28.60
>> 10/5/2000       0.03    0.42    1.64    1.27    3.82    28.32   28.32   28.32
>> 10/6/2000       0.03    0.42    1.53    1.13    3.74    28.26   28.26   28.26
>> 10/7/2000       0.11    0.51    1.59    5.66    3.68    28.23   28.23   28.23
>> 10/8/2000       0.16    0.45    1.70    3.40    3.62    27.84   27.84   27.84
>> 10/9/2000       0.10    0.42    1.78    2.55    3.54    26.56   26.56   26.56
>>
>>> setwd("J:/Rstuff/flow")
>>> # defining the working directory
>>> flow=read.delim("flow.dat",header=TRUE,sep="\t")
>>> # opening the above tab separated data file
>>> plot(flow$USGS1500~as.Date(flow$Date,
>>> "%m/%d/%y"),type="l",xlab="date",ylab="daily discharge (m3/s)
>>> ",main="USGS1500",yaxs="i", xaxs="i")
>>> #just to test my code, i am trying to make time series
>>
>> #plot of 1 variable USGS1500
>>
>> http://r.789695.n4.nabble.com/file/n4231737/flow.docx flow.docx
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/need-help-with-a-time-series-plotting-problem-tp4230672p4231737.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.
>
>
>
> --
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
[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: need help with a time series plotting problem

Rui Barradas
In reply to this post by vibhava
vibhava wrote
Dear R Users,
                     I am a beginner in R programming and need some help with a simple plotting problem that i am having. My dataset consist of three columns: first one has data_id, second is the date and third is the actual data itself corresponding to each date. The date ranges from 1/1/2000-12/31/2009. I am trying to plot my data versus the dates as a long term time series but what's happening is that R is plotting each year on top of the previous year. so instead of getting 1 line (dated 2000-2009) in the plot i am getting 9 lines (1 line for each year). i tried to look for solutions online but found nothing. can someone suggest how can i make a plot with x-axis ranging from 2000-2009. my code is copied below:

setwd("J:/Rstuff/flow")
flow=read.delim("flow.dat",header=TRUE,sep="\t")
plot(flow$usgs1500~as.Date(flow$date, "%m/%d/%y"),type="l",xlab="date",ylab="daily discharge (m3/s) ",main="USGS1500",yaxs="i", xaxs="i",)


any help would be appreciated

regards

vibhava
Hello,

If I'm understanding it well, this is a time series problem, use time series functions.

You can use 'stats::ts' or, what seems to be better for your problem, package zoo.
Here is an example.

library(zoo)

flow <- read.delim( ... etc ...


flow$Date <- as.Date(flow$Date, format="%m/%d/%Y")
head(flow$Date)  # see if it worked

zflow <- zoo(flow[,-1], order.by=flow$Date)  # make the time series

plot(zflow)                      # all 8 series, ugly axes
plot(zflow$USGS700)  # just the first series, ugly axes

If this helps, then make it pretty with Jim's ideas.
See also the 'plot.zoo' help page, it has several examples with fancy labels.

Rui Barradas
Reply | Threaded
Open this post in threaded view
|

Re: need help with a time series plotting problem

vibhava
Dear Jim and Rui,
                             Thanks for your suggestions. i just successfully prepared my first graph :). now i will move on to more interesting figures. thank you once again for your help and time.

regards

vibhava