[R] need help with a time series plotting problem

jim holtman jholtman at gmail.com
Sun Dec 25 04:51:53 CET 2011


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 <jholtman at gmail.com> 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 <vibhavasrivastava at gmail.com> 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.
>>
>> ______________________________________________
>> R-help at r-project.org 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.



More information about the R-help mailing list