Hi R community, i have a data frame of streamflow for 23 years, i.e. date year month day wy yd wyd modQ 1 1965-10-01 1965 10 1 1966 274 1 0.3341630 2 1965-10-02 1965 10 2 1966 275 2 0.3223247 3 1965-10-03 1965 10 3 1966 276 3 0.3109057 i only want to plot 1 of the years, along with the date. i can accomplish this with: plot(mod.sage$date[mod.sage$wy==1976],mod.sage$modQ[mod.sage$wy==1976], type="l") however, this is a bit long and clunky. is there a way to plot just a portion of the full data record without creating a whole new object with subset? thank you for any help! Janet [[alternative HTML version deleted]]
On 01/14/2014 11:39 AM, Janet Choate wrote:> Hi R community, > i have a data frame of streamflow for 23 years, i.e. > > date year month day wy yd wyd modQ > 1 1965-10-01 1965 10 1 1966 274 1 0.3341630 > 2 1965-10-02 1965 10 2 1966 275 2 0.3223247 > 3 1965-10-03 1965 10 3 1966 276 3 0.3109057 > > i only want to plot 1 of the years, along with the date. > i can accomplish this with: > > plot(mod.sage$date[mod.sage$wy==1976],mod.sage$modQ[mod.sage$wy==1976], > type="l") > > however, this is a bit long and clunky. is there a way to plot just a > portion of the full data record without creating a whole new object with > subset? >Hi Janet, I suppose you could just pass the whole data frame and use: xlim=as.Date(c("1976-01-01","1976-31-12"),"%Y-%m-%d") assuming that you are plotting modQ against a Date object. Jim
On Jan 13, 2014, at 4:39 PM, Janet Choate wrote:> Hi R community, > i have a data frame of streamflow for 23 years, i.e. > > date year month day wy yd wyd modQ > 1 1965-10-01 1965 10 1 1966 274 1 0.3341630 > 2 1965-10-02 1965 10 2 1966 275 2 0.3223247 > 3 1965-10-03 1965 10 3 1966 276 3 0.3109057 > > i only want to plot 1 of the years, along with the date. > i can accomplish this with: > > plot(mod.sage$date[mod.sage$wy==1976],mod.sage$modQ[mod.sage$wy==1976], > type="l") > > however, this is a bit long and clunky. is there a way to plot just a > portion of the full data record without creating a whole new object with > subset??subset ?with with( subset( mod.sage, wy==1976), plot(date, $modQ, type="l") ) I didn't leave a new object in the global environment, although I did create a temporary subset-object. -- David Winsemius Alameda, CA, USA
Hi Janet, You may also check library(xts).? mod.sage <- read.table(text="date year month day? wy? yd wyd????? modQ 1 1965-10-01 1965??? 10? 1 1966 274? 1 0.3341630 2 1965-10-02 1965??? 10? 2 1966 275? 2 0.3223247 3 1965-10-03 1965??? 10? 3 1966 276? 3 0.3459057 4 1965-10-04 1965??? 10? 4 1966 277? 4 0.3841630 5 1965-10-05 1965??? 10? 5 1966 278? 5 0.3423247 6 1965-10-06 1965??? 10? 6 1966 279? 6 0.3209057 7 1966-10-04 1966??? 10? 4 1967 277? 4 0.3341630 8 1966-10-05 1966??? 10? 5 1967 278? 5 0.3223247 9 1966-10-06 1966??? 10? 6 1967 279? 6 0.3109057",sep="",header=TRUE,stringsAsFactors=FALSE) library(xts) # xt1 <- xts(mod.sage[,-1],order.by=as.Date(mod.sage[,1])) # plot(with(xt1["/1965"],modQ), main= "Year 1965") xt2 <- xts(mod.sage[,-c(1:7)],order.by=as.Date(mod.sage[,1])) plot(xt2["/1965"],main="Year 1965") A.K. On Monday, January 13, 2014 7:41 PM, Janet Choate <jsc.eco at gmail.com> wrote: Hi R community, i have a data frame of streamflow for 23 years, i.e. ? ? ? ? date year month day? wy? yd wyd? ? ? modQ 1 1965-10-01 1965? ? 10? 1 1966 274? 1 0.3341630 2 1965-10-02 1965? ? 10? 2 1966 275? 2 0.3223247 3 1965-10-03 1965? ? 10? 3 1966 276? 3 0.3109057 i only want to plot 1 of the years, along with the date. i can accomplish this with: plot(mod.sage$date[mod.sage$wy==1976],mod.sage$modQ[mod.sage$wy==1976], type="l") however, this is a bit long and clunky. is there a way to plot just a portion of the full data record without creating a whole new object with subset? thank you for any help! Janet ??? [[alternative HTML version deleted]] ______________________________________________ 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.
If you use the formula method for plot then you can use the subset argument, for example: plot(Sepal.Width ~ Sepal.Length, data=iris) plot(Sepal.Width ~ Sepal.Length, data=iris, subset=Species=='setosa') On Mon, Jan 13, 2014 at 5:39 PM, Janet Choate <jsc.eco at gmail.com> wrote:> Hi R community, > i have a data frame of streamflow for 23 years, i.e. > > date year month day wy yd wyd modQ > 1 1965-10-01 1965 10 1 1966 274 1 0.3341630 > 2 1965-10-02 1965 10 2 1966 275 2 0.3223247 > 3 1965-10-03 1965 10 3 1966 276 3 0.3109057 > > i only want to plot 1 of the years, along with the date. > i can accomplish this with: > > plot(mod.sage$date[mod.sage$wy==1976],mod.sage$modQ[mod.sage$wy==1976], > type="l") > > however, this is a bit long and clunky. is there a way to plot just a > portion of the full data record without creating a whole new object with > subset? > > thank you for any help! > Janet > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com