-
Notifications
You must be signed in to change notification settings - Fork 10
/
Plotting_FLR_objects_using_lattice.Rmd
220 lines (152 loc) · 8.31 KB
/
Plotting_FLR_objects_using_lattice.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
---
title: "Plotting FLR objects using *lattice*"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
github_document:
mathjax: TRUE
pdf_document:
fig_width: 6
fig_height: 4
tags: [FLR]
license: Creative Commons Attribution-ShareAlike 4.0 International Public License
---
```{r, ini, echo=FALSE, results='hide', message=FALSE, warnings=FALSE, cache=FALSE}
library(knitr)
source("R/ini.R")
```
The lattice package (Sarkar, 2008) improves on base R graphics by providing better defaults and the ability to easily display multivariate relationships. In particular, the package supports the creation of trellis graphs - graphs that display a variable or the relationship between variables, conditioned on one or more other variables. Lattice methods (e.g. `xyplot()`, `bwplot()`, `dotplot()`) are available for FLR classes in FLCore, and the standard plots (`plot()`) in FLR are lattice-based.
## Required packages
To follow this tutorial you should have installed the following packages:
- CRAN: [lattice](https://cran.r-project.org/web/packages/lattice/index.html)
- FLR: [FLCore](http://www.flr-project.org/FLCore/)
You can do so as follows:
```{r, eval=FALSE}
install.packages(c("lattice"))
install.packages(c("FLCore"), repos="http://flr-project.org/R")
```
Load necessary packages:
```{r, pkgs}
library(FLCore)
library(lattice)
```
# GRAPH TYPES AVAILABLE
The typical formula for lattice plots is `graphtype(formula, data=FLQuant)` where *graphtype* is selected from the table below, and *formula* specifies the variable(s) to display and any conditioning variables. For example `~ data | A` means display numeric variables for each level of factor A in separate graphs, and `data ~ A` means display numeric variables for each level of factor A.
The following lattice graph options could be used for FLR objects:
Graph Type | Description | Formula
-----------|------------------- |---------
barchart | bar plot | `data ~ A`
bwplot | boxplot | `data ~ A`
dotplot | dotplot | `data ~ A`
histogram | histogram | `~ data | A`
xyplot | scatterplot | `data ~ A` or `data ~ A | B`
wireframe | 3D wireframe graph | `data ~ A + B`
bubbles | bubble plot | `A ~ B`
In case of lattice-based *plot* the name of the object can be simply used, e.g. `plot(ple4)`
# STANDARD LATTICE BASED FLR PLOTS
The standard `plot()` function in FLR returns lattice-based plots for FLR objects. In the following examples, the FLStock object *ple4* and the FLSR object *nsher* are used.
```{r, datasets}
#Read FLR examples
data(ple4)
data(nsher)
```
Plots of the main FLQuants of an FLStock can be generated using the `plot()` function
```{r, fig1}
# Plot FLStock
plot(ple4)
```
A specific object (FLQuant) of the FLStock can be also plotted by age and year using the `plot()` function. In the following example, the `scales` argument has been used to allow different scales on the y axis of the plot for each age.
```{r, fig2}
# plot FLQuant
plot(catch.n(ple4)/1000, ylab="Catch numbers (thousands)",
scales = list(y = list(relation = 'free')))
```
The `plot()` function can also be used to produce summary graphs of objects other then FLStock, such as FLSR objects.
```{r, fig3}
# Plot FLSR
plot(nsher)
```
# PLOTTING FLR OBJECTS USING LATTICE
It is useful to check what the objects look like before plotting them.
```{r, dataframe}
head(as.data.frame(catch(ple4)))
```
## XYPLOTS for FLR
`xyplot` produces bivariate scatterplots or time-series plots for FLR objects. Standard features, e.g. division by number, could be used in order to update standard FLR units of measurement, if needed. Lattice options from [user guide](https://cran.r-project.org/web/packages/lattice/lattice.pdf) can be used to update your graphs.
```{r, fig4}
xyplot(data/1000~year, data=catch(ple4), type='b', pch=19,
ylab="catch (thousand tonnes)",xlab='')
```
One could also group the data, e.g. by age, area or season, and plot the FLQuant by age class, area or season.
```{r, fig5}
xyplot(data/1000~year, groups=age, data=catch.n(ple4), type='l',
auto.key=list(space='bottom',columns=5, cex=0.7),
ylab='Catch numbers at age (10^6)',xlab='')
```
Groups can be analysed by plotting them separately
```{r, fig6}
xyplot(data/1000~year|factor(age), data=catch.n(ple4), type='l',
scales = list(y = list(relation = 'free')), ylab='Catch numbers (10^6)',xlab='')
```
Methods are also available for plotting multiple FLQuants, called by name using 'qname'
```{r, fig7}
xyplot(data~year|qname, data=FLQuants(SSB=ssb(ple4), Yield=catch(ple4),
Landings=landings(ple4)),xlab='',type='l')
```
## BARCHARTS
Similar to the xyplots, barcharts also allow exploring FLQuants.
```{r, fig8}
barchart(data/1000~factor(year),
data=landings(ple4),
ylab =list(label="thousand tonnes",cex=0.8),scales=list(x=list(rot=90)),
type="v", main = "Total landings" )
```
## BWPLOTS
Boxplots can be created using `bwplot()`. In the following example, some stochasticity has been added to Fbar and the resulting values per year have been plotted as boxplots.
```{r, fig10}
bwplot(data~year, rlnorm(200, fbar(ple4), 0.15),scales=list(x=list(rot=90)), ylab="Fbar")
```
## DOTPLOTS
Dotplots can be used to plot FLQuants. In the following example, F at age is plotted for the available time-series and colours are used to indicate the different age classes.
```{r, fig11}
dotplot(data~year,groups=age,harvest(ple4),
scales=list(x=list(rot=90)), auto.key=list(space='bottom',columns=5, cex=0.7),ylab="F at age")
```
## HISTOGRAM
Histograms can be implemented to show what percent of total entries of an FLQuant falls within a specific range of values. The following example shows how younger age classes exhibit greater variability in terms of catch numbers compared to older age classes, because they are affected more by the size of incoming cohorts.
```{r, fig12}
histogram(~data|age, catch.n(ple4), xlab='Catch numbers')
```
## WIREFRAME
Three-dimensional surface plots can also be used for for plotting FLQuants. In the following example, F at age by year is plotted as a three dimensional surface.
```{r, fig13}
wireframe(data~age+year, data=harvest(ple4),zlab="F",drape = TRUE,
col.regions = colorRampPalette(c("green", "red"))(100))
```
# FLR specific plots, BUBBLES
The bubble plots have been created specifically for FLR, and they are typically used to visualise data by age classes.
```{r, fig14}
bubbles(age~year, data=catch.n(ple4), xlab='', bub.scale=5)
```
One could also specify parts of the object of interest, e.g. select age classes between 5 and 10 as in the following:
```{r, fig15}
bubbles(age~year, data=catch.n(ple4)[5:10,], xlab='', bub.scale=10)
```
# References
[Sarkar, D. 2008. Lattice: Multivariate Data Visualization with R. Springer-Verlag, New York, NY, 2008. ISBN 978-0-387-75968-5. 268 pp.](http://lmdvr.r-forge.r-project.org/figures/figures.html)
[Package "lattice" user manual](https://cran.r-project.org/web/packages/lattice/lattice.pdf)
[Quick Lattice Graphs](http://statmethods.net/advgraphs/trellis.html)
[A quick introduction to FLR](http://flr-project.org/doc/A_quick_introduction_to_FLR.html)
# More information
* You can submit bug reports, questions or suggestions on this tutorial at <https://github.com/flr/doc/issues>.
* Or send a pull request to <https://github.com/flr/doc/>
* For more information on the FLR Project for Quantitative Fisheries Science in R, visit the FLR webpage, <http://flr-project.org>.
## Software Versions
* `r version$version.string`
* FLCore: `r packageVersion('FLCore')`
* lattice: `r packageVersion('lattice')`
* **Compiled**: `r date()`
## License
This document is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0) license.
## Author information
**Paris VASILAKOPOULOS**. European Commission, DG Joint Research Centre, Directorate D - Sustainable Resources, Unit D.02 Water and Marine Resources, Via E. Fermi 2749, 21027 Ispra VA, Italy. <https://ec.europa.eu/jrc/>
**Arina MOTOVA**. SEAFISH, 18 Logie Mill, Logie Green Road, EH7 4HS, Edinburgh, United Kingdom. <http://www.seafish.org/>