Time series plots from vectors

Perhaps the most basic type of plot to make is a TIME SERIES PLOT

x-axis is …

Example time series

Time series plot - Wolves of Yellowstone NP

Time series plot - Wolves of Yellowstone NP

Some classic time series plots

Classic time series

Classic time series

R comes packaged with some time series plots in a special format

Can be plotted in 2 easy steps

R comes packaged with some time series plots in a special format

data(lynx) # load data
plot(lynx)

Most data in R is stored on 1 of 2 DATA STRUCTURES

VECTORS

DATA FRAMES

Vectors are like the COLUMNS of a spreadsheet

##    year.1.15.
## 1        1995
## 2        1996
## 3        1997
## 4        1998
## 5        1999
## 6        2000
## 7        2001
## 8        2002
## 9        2003
## 10       2004
## 11       2005
## 12       2006
## 13       2007
## 14       2008
## 15       2009

Vectors are conceptually like columns, but printed in R in a ROW

year
##  [1] 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
## [16] 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020

Dataframes are like a whole spreadsheet

##    year packs wolves
## 1  1995   3.0     21
## 2  1996   9.0     51
## 3  1997   9.0     86
## 4  1998  11.0    112
## 5  1999  11.0    118
## 6  2000   8.0    119
## 7  2001  10.0    132
## 8  2002  14.0    148
## 9  2003  13.5    174
## 10 2004  16.0    171
## 11 2005  13.0    118
## 12 2006  13.0    136
## 13 2007  11.0    171
## 14 2008  12.0    124
## 15 2009  14.0     96

Time series plots are made from 2 VECTORS of data

year
##  [1] 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
## [16] 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
wolves
##  [1]  21  51  86 112 118 119 132 148 174 171 118 136 171 124  96  97  98  83  95
## [20] 104  98 108  97  80  94 123

The 2 vectors MUST be the same length

length(year)
## [1] 26
length(wolves)
## [1] 26

We can TEST FOR EQUALITY of lengths

length(year) == length(wolves)
## [1] TRUE

If 2 vectors are different length then returns FALSE

# the vector
wolves_wrong
## [1]  21  51  86 112 118 119
# its length
length(wolves_wrong)
## [1] 6

If 2 vectors are different length then returns FALSE

length(year) == length(wolves_wrong)
## [1] FALSE

How would this operation be phrased in words?

length(year) == length(wolves_wrong)
## [1] FALSE

This is asking the question “Does 26 = 6”

26 == 6
## [1] FALSE

plot() he defaults are blah

DEFAULTS are the initial settings of a function

Some improvements

Some more improvements

Many plots in base R can be made with the plot() command

plot(wolves ~ year)

A TITLE is added with main = ...

plot(wolves ~ year, main = "A plot made with plot()")

The plot() command can plot points, lines or both

the default is for points

The plot() command can plot points, lines or both

par(mfrow = c(2,2), mar = c(2,2,4,1)) #ignore this line
plot(wolves ~ year, main = "(default)") # default
plot(wolves ~ year, type = "p", main = "type = 'p'") # type  = "p"
plot(wolves ~ year, type = "l", main = "type = 'p'") # type  = 'l'
plot(wolves ~ year, type = "b", main = "type = 'b'") # type  = "l"

col = ... set the COLOR

par(mfrow = c(2,2), mar = c(2,2,4,1)) #ignore this line
plot(wolves ~ year, main = "(default)") # default
plot(wolves ~ year, col = 1, main = "col = 1") # black
plot(wolves ~ year, col = 2, main = "col = 2") # red
plot(wolves ~ year, col = 3, main = "col = 3") # green

For points, pch = ... sets the type of point

par(mfrow = c(2,2), mar = c(2,2,4,1)) #ignore this line
plot(wolves ~ year, main = "(default)") 
plot(wolves ~ year, pch = 2, main = "pch = 2")
plot(wolves ~ year, pch = 10, main = "pch = 3") 
plot(wolves ~ year, pch = 16, main = "pch = 16") 

My favorite pch values are in the teens

par(mfrow = c(2,2), mar = c(2,2,4,1)) #ignore this line
plot(wolves ~ year, pch = 15,  main = "pch = 15") 
plot(wolves ~ year, pch = 16, main = "pch = 16")
plot(wolves ~ year, pch = 17, main = "pch = 17") 
plot(wolves ~ year, pch = 18, main = "pch = 18") 

Multiple arguments can be combined

Multiple arguments can be combined

Multiple arguments can be combined

par(mfrow = c(1,1), mar = c(4,4,4,4)) # ignore this
plot(wolves ~ year, pch = 16,  col = 2, type = "b",
     xlab = "Year",
     ylab = "Wolves (N)") 

NOTE: ggplot2 is a TOTALLY different way to do things

ggplot(data = wolves,
       aes(y = packs, x = year)) +
  geom_point() + 
  geom_line()

Vectors are made with the c() function

packs  <- c( 3, 9,  9,11,  11, 8,10,14,13.5,16,
             13,13,11,12,  14,11,10,10,10,  11,
             10,11,11, 9,   8, 9)

EACH element within the vector separated by a comma ,

packs  <- c( 3, 9,  9,11, 11, 8,10,14,13.5,16,
             13,13,11,12, 14,11,10,10,10,  11,
             10,11,11, 9,  8, 9)

Long vectors often split between lines

Spaces ok too - I use them to line things up

packs  <- c( 3, 9,  9,11, 11, 8,10,14,13.5,16,
             13,13,11,12, 14,11,10,10,10,  11,
             10,11,11, 9, 8, 9)

Vectors can contain text

Text MUST be in quotes

wolf_names  <- c("white fang", "fluffy", "bingo","minnie", "percy")

Single ELEMENTS of a vector accessed using BRACKETS

aka BRACKET NOTATION

# names
wolf_names  <- c("white fang", "fluffy", "bingo","minnie", "percy")

# first name - uses 1 (not 0!)
wolf_names[1]
## [1] "white fang"
# 2nd names name - starts at 1 (not 0!)
wolf_names[2]
## [1] "fluffy"

Multple ELEMENTS of a vector accessed using :

# first two
wolf_names[1:2]
## [1] "white fang" "fluffy"
# 2nd two
wolf_names[2:3]
## [1] "fluffy" "bingo"

This is called INDEXING

“The index for ‘fluffy’ is 2”

wolf_names[2]
## [1] "fluffy"

MUST have 2 values when using :

ERRORS

wolf_names[1:]
wolf_names[:2]

Can call everything if you want

wolf_names[1:5]
## [1] "white fang" "fluffy"     "bingo"      "minnie"     "percy"

Can call everything BUT the 1st like this

wolf_names[2:5]
## [1] "fluffy" "bingo"  "minnie" "percy"

First real programming trick

Use NEGATIVE INDEXING to drop elements

wolf_names[-1]
## [1] "fluffy" "bingo"  "minnie" "percy"
wolf_names[-2]
## [1] "white fang" "bingo"      "minnie"     "percy"

Next real programming trick - can “pass” vectors of indices to vectors

A vector

wolf_names  <- c("white fang", "fluffy", "bingo","minnie", "percy")

a vector element via an index value

wolf_names[1]
## [1] "white fang"

2 vector elements via an index values

wolf_names[1:2]
## [1] "white fang" "fluffy"

a vector of indices

i <- c(1,2)

2 vector elements via a vector of indices

wolf_names[i]
## [1] "white fang" "fluffy"

I can make a vector of numbers 2 ways

I will usually use the first for clarity

i1 <- c(1, 2, 3)

i2 <- c(1:3)

How do I test for equality of the two vectors

length(i1) == length(i2)
## [1] TRUE

Operations on vectors can be VECTORIZED

Some vectors

DNA <- c("A","T","C","G")

RNA <- c("A","U","C","G")

Their length

length(DNA)
## [1] 4
length(RNA)
## [1] 4

The equality of their lengths

length(DNA) == length(RNA)
## [1] TRUE

Access 1st elements

DNA[1]
## [1] "A"
RNA[1]
## [1] "A"

Equality of first elements

DNA[1] == RNA[1]
## [1] TRUE

Access 2nd elements

DNA[2]
## [1] "T"
RNA[2]
## [1] "U"

IN-Equality of 2nd elements

DNA[2] == RNA[2]
## [1] FALSE

Assess equality of ALL elements

DNA == RNA
## [1]  TRUE FALSE  TRUE  TRUE

VECTORIZED operations are common in R

taking the log of something is very common in math, stats, ML, bio…

natural log = log() in R

log base 10 = log10()

log base 2 = lo2g()

log(10)
## [1] 2.302585
log10(10)
## [1] 1
log2(10)
## [1] 3.321928

Functions can be applied to entire vectors

VECTORIZED operations are common in R

log(wolves)
##        year    packs   wolves
## 1  7.598399 1.098612 3.044522
## 2  7.598900 2.197225 3.931826
## 3  7.599401 2.197225 4.454347
## 4  7.599902 2.397895 4.718499
## 5  7.600402 2.397895 4.770685
## 6  7.600902 2.079442 4.779123
## 7  7.601402 2.302585 4.882802
## 8  7.601902 2.639057 4.997212
## 9  7.602401 2.602690 5.159055
## 10 7.602900 2.772589 5.141664
## 11 7.603399 2.564949 4.770685
## 12 7.603898 2.564949 4.912655
## 13 7.604396 2.397895 5.141664
## 14 7.604894 2.484907 4.820282
## 15 7.605392 2.639057 4.564348
## 16 7.605890 2.397895 4.574711
## 17 7.606387 2.302585 4.584967
## 18 7.606885 2.302585 4.418841
## 19 7.607381 2.302585 4.553877
## 20 7.607878 2.397895 4.644391
## 21 7.608374 2.302585 4.584967
## 22 7.608871 2.397895 4.682131
## 23 7.609367 2.397895 4.574711
## 24 7.609862 2.197225 4.382027
## 25 7.610358 2.079442 4.543295
## 26 7.610853 2.197225 4.812184

Math can be done on entire vectors

The average wolf it 95 pounds

Wolf BIOMASS each year

wolves*95
##      year  packs wolves
## 1  189525  285.0   1995
## 2  189620  855.0   4845
## 3  189715  855.0   8170
## 4  189810 1045.0  10640
## 5  189905 1045.0  11210
## 6  190000  760.0  11305
## 7  190095  950.0  12540
## 8  190190 1330.0  14060
## 9  190285 1282.5  16530
## 10 190380 1520.0  16245
## 11 190475 1235.0  11210
## 12 190570 1235.0  12920
## 13 190665 1045.0  16245
## 14 190760 1140.0  11780
## 15 190855 1330.0   9120
## 16 190950 1045.0   9215
## 17 191045  950.0   9310
## 18 191140  950.0   7885
## 19 191235  950.0   9025
## 20 191330 1045.0   9880
## 21 191425  950.0   9310
## 22 191520 1045.0  10260
## 23 191615 1045.0   9215
## 24 191710  855.0   7600
## 25 191805  760.0   8930
## 26 191900  855.0  11685

Yellowstone NP is 3500 suare miles

Wolves per square mile

wolves / 3500
##         year        packs     wolves
## 1  0.5700000 0.0008571429 0.00600000
## 2  0.5702857 0.0025714286 0.01457143
## 3  0.5705714 0.0025714286 0.02457143
## 4  0.5708571 0.0031428571 0.03200000
## 5  0.5711429 0.0031428571 0.03371429
## 6  0.5714286 0.0022857143 0.03400000
## 7  0.5717143 0.0028571429 0.03771429
## 8  0.5720000 0.0040000000 0.04228571
## 9  0.5722857 0.0038571429 0.04971429
## 10 0.5725714 0.0045714286 0.04885714
## 11 0.5728571 0.0037142857 0.03371429
## 12 0.5731429 0.0037142857 0.03885714
## 13 0.5734286 0.0031428571 0.04885714
## 14 0.5737143 0.0034285714 0.03542857
## 15 0.5740000 0.0040000000 0.02742857
## 16 0.5742857 0.0031428571 0.02771429
## 17 0.5745714 0.0028571429 0.02800000
## 18 0.5748571 0.0028571429 0.02371429
## 19 0.5751429 0.0028571429 0.02714286
## 20 0.5754286 0.0031428571 0.02971429
## 21 0.5757143 0.0028571429 0.02800000
## 22 0.5760000 0.0031428571 0.03085714
## 23 0.5762857 0.0031428571 0.02771429
## 24 0.5765714 0.0025714286 0.02285714
## 25 0.5768571 0.0022857143 0.02685714
## 26 0.5771429 0.0025714286 0.03514286

Math can be done using variable

Make variables with constants

wolve_weight <- 95
YNP_size <- 3500

Do math using varibales

wolves/wolve_weight
##        year      packs    wolves
## 1  21.00000 0.03157895 0.2210526
## 2  21.01053 0.09473684 0.5368421
## 3  21.02105 0.09473684 0.9052632
## 4  21.03158 0.11578947 1.1789474
## 5  21.04211 0.11578947 1.2421053
## 6  21.05263 0.08421053 1.2526316
## 7  21.06316 0.10526316 1.3894737
## 8  21.07368 0.14736842 1.5578947
## 9  21.08421 0.14210526 1.8315789
## 10 21.09474 0.16842105 1.8000000
## 11 21.10526 0.13684211 1.2421053
## 12 21.11579 0.13684211 1.4315789
## 13 21.12632 0.11578947 1.8000000
## 14 21.13684 0.12631579 1.3052632
## 15 21.14737 0.14736842 1.0105263
## 16 21.15789 0.11578947 1.0210526
## 17 21.16842 0.10526316 1.0315789
## 18 21.17895 0.10526316 0.8736842
## 19 21.18947 0.10526316 1.0000000
## 20 21.20000 0.11578947 1.0947368
## 21 21.21053 0.10526316 1.0315789
## 22 21.22105 0.11578947 1.1368421
## 23 21.23158 0.11578947 1.0210526
## 24 21.24211 0.09473684 0.8421053
## 25 21.25263 0.08421053 0.9894737
## 26 21.26316 0.09473684 1.2947368
wolves/YNP_size
##         year        packs     wolves
## 1  0.5700000 0.0008571429 0.00600000
## 2  0.5702857 0.0025714286 0.01457143
## 3  0.5705714 0.0025714286 0.02457143
## 4  0.5708571 0.0031428571 0.03200000
## 5  0.5711429 0.0031428571 0.03371429
## 6  0.5714286 0.0022857143 0.03400000
## 7  0.5717143 0.0028571429 0.03771429
## 8  0.5720000 0.0040000000 0.04228571
## 9  0.5722857 0.0038571429 0.04971429
## 10 0.5725714 0.0045714286 0.04885714
## 11 0.5728571 0.0037142857 0.03371429
## 12 0.5731429 0.0037142857 0.03885714
## 13 0.5734286 0.0031428571 0.04885714
## 14 0.5737143 0.0034285714 0.03542857
## 15 0.5740000 0.0040000000 0.02742857
## 16 0.5742857 0.0031428571 0.02771429
## 17 0.5745714 0.0028571429 0.02800000
## 18 0.5748571 0.0028571429 0.02371429
## 19 0.5751429 0.0028571429 0.02714286
## 20 0.5754286 0.0031428571 0.02971429
## 21 0.5757143 0.0028571429 0.02800000
## 22 0.5760000 0.0031428571 0.03085714
## 23 0.5762857 0.0031428571 0.02771429
## 24 0.5765714 0.0025714286 0.02285714
## 25 0.5768571 0.0022857143 0.02685714
## 26 0.5771429 0.0025714286 0.03514286

When two vectors are used in an operation their elements are compared PAIRWISE

Vectors of wolves and number of packs at same time

wolves[1:5]
## [1]  21  51  86 112 118
packs[1:5]
## [1]  3  9  9 11 11
year[1:5]
## [1] 1995 1996 1997 1998 1999

Number of wolves and packs year 1

wolves[1]
## [1] 21
packs[1]
## [1] 3

Wolves per pack

21/3
## [1] 7

Wolves per pack via index values

wolves[1]/packs[1]
## [1] 7

Wolves per pack for ALL YEARS

wolves/packs
##  [1]  7.000000  5.666667  9.555556 10.181818 10.727273 14.875000 13.200000
##  [8] 10.571429 12.888889 10.687500  9.076923 10.461538 15.545455 10.333333
## [15]  6.857143  8.818182  9.800000  8.300000  9.500000  9.454545  9.800000
## [22]  9.818182  8.818182  8.888889 11.750000 13.666667

Wolves per pack 1st 2 years

Using :

wolves[1:2]/packs[1:2]
## [1] 7.000000 5.666667

Using indices in a vector via c()

i <- c(1,2)
wolves[i]
## [1] 21 51

Using raw index

wolves[c(1,2)]/packs[c(1,2)]
## [1] 7.000000 5.666667

Wolves per pack ignoring just first year

wolves[-1]/packs[-1]
##  [1]  5.666667  9.555556 10.181818 10.727273 14.875000 13.200000 10.571429
##  [8] 12.888889 10.687500  9.076923 10.461538 15.545455 10.333333  6.857143
## [15]  8.818182  9.800000  8.300000  9.500000  9.454545  9.800000  9.818182
## [22]  8.818182  8.888889 11.750000 13.666667