[R] Plotting multiple trends on one graph
Duncan Mackay
dulcalma at bigpond.com
Mon Nov 25 01:05:57 CET 2013
Hi Natalie
Here is an option using lattice. I think below will get you some way to what
you want
This is your data formatted. in future please dput your data as your data
was scrambled.
# dput(dat)
dat <- structure(list(TagID = c(4926L, 4926L, 4926L, 4926L, 4926L, 4926L,
4926L, 4926L, 4926L, 4926L, 4926L, 4929L, 4929L, 4929L, 4929L,
4929L, 4929L, 4929L, 4929L, 4929L, 4929L, 4929L, 4929L, 4929L,
4929L), Station = c("KLB", "MS01", "MS02", "MS03", "MS04", "MS05",
"MS06", "MS07", "MS08", "MS09", "MS10", "KLB", "MS01", "MS02",
"MS03", "MS04", "MS05", "MS06", "MS07", "MS08", "MS09", "MS10",
"MS11", "MS12", "MS13"), datetime = c("12/21/2012 1:52", "12/21/2012 2:38",
"12/21/2012 3:48", "12/21/2012 4:19", "12/21/2012 4:34", "12/21/2012 5:01",
"12/21/2012 6:54", "12/21/2012 7:21", "12/21/2012 10:23", "12/21/2012
12:16",
"12/21/2012 14:38", "12/21/2012 1:08", "12/21/2012 2:12", "12/21/2012 3:33",
"12/21/2012 3:59", "12/21/2012 4:13", "12/21/2012 5:00", "12/21/2012 6:52",
"12/21/2012 7:32", "12/21/2012 10:16", "12/21/2012 11:43", "12/21/2012
14:02",
"12/22/2012 2:50", "12/22/2012 5:04", "12/22/2012 13:59"), gspd_mps = c(NA,
0.851, 0.629, 0.86, 1.131, 0.9, 0.798, 0.853, 0.694, 0.6, 0.647,
NA, 0.611, 0.563, 1.04, 1.082, 0.475, 0.796, 0.563, 0.809, 0.783,
0.657, 0.326, 0.709, 0.688)), .Names = c("TagID", "Station",
"datetime", "gspd_mps"), class = "data.frame", row.names = c(NA,
-25L))
# factor of id
dat[,1] <- factor(dat[,1])
# convert to datetime
x <- paste(dat[,3])
x <- strptime(x, "%m/%d/%Y %H:%M")
I have added a few extra formatting options but I will leave you to format
the x labels as an exercise.
# lattice plot conditioned by station
library(lattice)
xyplot(gspd_mps ~ as.POSIXct(x)|Station, dat,
as.table = TRUE,
layout = c(1,14),
groups = TagID,
strip = FALSE,
type = c("p","g"),
par.settings = list(strip.background = list(col = "transparent"),
superpose.symbol = list(cex = c(1,0.7),
col = c("red","blue"),
pch = c(20,3))),
strip.left = strip.custom(par.strip.text = list(cex = 0.65) ),
scales = list(x = list(alternating = FALSE,
rot = 90)),
auto.key = TRUE
)
# using latticeExtra conditioned by station and tag
library(latticeExtra)
useOuterStrips(strip = strip.custom(factor.levels = paste("TagID",
unique(dat$TagID)),
par.strip.text = list(cex = 0.85)),
strip.left = strip.custom(horizontal = TRUE,
par.strip.text = list(cex = 0.75)),
strip.left.lines = 2,
xyplot(gspd_mps ~ as.POSIXct(x)|TagID*Station, dat,
as.table = TRUE,
scales = list(x = list(alternating = FALSE,
rot = 90)),
type = c("p","g")
)
) ## useOuterStrips
useOuterStrips(strip = strip.custom(factor.levels = paste("TagID",
unique(dat$TagID)),
par.strip.text = list(cex = 0.85)),
strip.left = strip.custom(horizontal = TRUE,
par.strip.text = list(cex = 0.75)),
strip.left.lines = 2,
xyplot(gspd_mps ~ as.POSIXct(x)|TagID*Station, dat,
as.table = TRUE,
scales = list(x = list(alternating = FALSE,
rot = 90)),
panel = function(x,y, ...){
panel.grid(h = 0, v= -1)
panel.xyplot(x,y,...)
}
)
) ## useOuterStrips
HTH
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Natalie Houghton McNair
Sent: Monday, 25 November 2013 02:49
To: R-help at r-project.org
Subject: [R] Plotting multiple trends on one graph
>
> Hello all,
>
> I am tracking hundreds of animals through a system with multiple
> timing points. I want to graph the movement of individuals through
> the whole array on one graph, but I can't figure out how to do that.
> An example of my data is below. Basically for each 'TagID', I want to
graph the 'date'
> or 'gspd_mps' on the X axis and 'Station' on the Y axis, with all
> TagID's on one graph.
>
Thanks for the help!! I'm very new to R.
Natalie
TagID Station datetime gspd_mps 4926 KLB 12/21/2012 1:52 NA 4926
MS01 12/21/2012
2:38 0.851 4926 MS02 12/21/2012 3:48 0.629 4926 MS03 12/21/2012 4:19 0.86
4926 MS04 12/21/2012 4:34 1.131 4926 MS05 12/21/2012 5:01 0.9 4926
MS06 12/21/2012
6:54 0.798 4926 MS07 12/21/2012 7:21 0.853 4926 MS08 12/21/2012 10:23
0.694 4926 MS09 12/21/2012 12:16 0.6 4926 MS10 12/21/2012 14:38 0.647
4929 KLB 12/21/2012 1:08 NA 4929 MS01 12/21/2012 2:12 0.611 4929
MS02 12/21/2012
3:33 0.563 4929 MS03 12/21/2012 3:59 1.04 4929 MS04 12/21/2012 4:13 1.082
4929 MS05 12/21/2012 5:00 0.475 4929 MS06 12/21/2012 6:52 0.796 4929
MS07 12/21/2012
7:32 0.563 4929 MS08 12/21/2012 10:16 0.809 4929 MS09 12/21/2012 11:43
0.783 4929 MS10 12/21/2012 14:02 0.657 4929 MS11 12/22/2012 2:50 0.326
4929 MS12 12/22/2012 5:04 0.709 4929 MS13 12/22/2012 13:59 0.688
>
>
[[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.
More information about the R-help
mailing list