[R] Knitr/Lattice/Lyx [was: Trellis Plots: translating lattice xyplot() to ggplot()]
Rich Shepard
rshepard at appl-ecosys.com
Sun Jul 12 02:21:39 CEST 2015
On Sat, 11 Jul 2015, Rich Shepard wrote:
> Attached are the .RData file from the cwd, the test.lyx file (lyx-2.1.3),
> and the two test files from /tmp/.../lyx_tmpbuf3/.
The .RData and .lyx files were stripped off. I'll try sending them again.
Rich
-------------- next part --------------
#LyX 2.1 created this file. For more info see http://www.lyx.org/
\lyxformat 474
\begin_document
\begin_header
\textclass scrreprt
\begin_preamble
\date{}
\usepackage{textcomp,url,multicol}
%\setkomafont{sectioning}{\rmfamily}
\end_preamble
\options abstract=on
\use_default_options false
\begin_modules
natbibapa
knitr
\end_modules
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman palatino
\font_sans default
\font_typewriter default
\font_math auto
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command bibtex
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize letterpaper
\use_geometry false
\use_package amsmath 1
\use_package amssymb 1
\use_package cancel 1
\use_package esint 0
\use_package mathdots 1
\use_package mathtools 1
\use_package mhchem 1
\use_package stackrel 1
\use_package stmaryrd 1
\use_package undertilde 1
\cite_engine natbib
\cite_engine_type authoryear
\biblio_style humannat
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\justification true
\use_refstyle 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 2
\tocdepth 2
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 2
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header
\begin_body
\begin_layout Standard
\begin_inset Flex Chunk
status open
\begin_layout Plain Layout
load('.RData')
\begin_inset Argument 1
status open
\begin_layout Plain Layout
echo=FALSE
\end_layout
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Some text in here; separating knitr chunks.
\end_layout
\begin_layout Standard
\begin_inset Flex Chunk
status open
\begin_layout Plain Layout
carlin <- read.csv("./carlin.csv", header = TRUE, sep = ",", stringsAsFactors
= F)
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Next paragraph of text.
\end_layout
\begin_layout Standard
\begin_inset Flex Chunk
status collapsed
\begin_layout Plain Layout
carlin$sampdate <- as.Date(carlin$sampdate)
\end_layout
\begin_layout Plain Layout
str(carlin)
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Another paragraph or two of text.
\end_layout
\begin_layout Standard
\begin_inset Flex Chunk
status open
\begin_layout Plain Layout
summary(carlin)
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Plots of these distributions are the next step ...
\end_layout
\begin_layout Standard
\begin_inset Flex Chunk
status collapsed
\begin_layout Plain Layout
carlin.1 <- subset(carlin, select = siteid:CO3)
\end_layout
\begin_layout Plain Layout
carlin.2 <- subset(carlin, select = c(siteid, sampdate, Alk:Ca))
\end_layout
\begin_layout Plain Layout
carlin.3 <- subset(carlin, select = c(siteid, sampdate, Mg:Cr))
\end_layout
\begin_layout Plain Layout
carlin.4 <- subset(carlin, select = c(siteid, sampdate, Co:Hg))
\end_layout
\end_inset
\end_layout
\begin_layout Standard
To plot each chemical constituent's concentration as a function of collection
date the data format needs to be reshaped from wide to long:
\end_layout
\begin_layout Standard
\begin_inset Flex Chunk
status collapsed
\begin_layout Plain Layout
library(reshape2)
\end_layout
\begin_layout Plain Layout
carlin.1.melt <- melt(carlin.1, na.rm = F, id.vars = c('siteid', 'sampdate'))
\end_layout
\begin_layout Plain Layout
carlin.2.melt <- melt(carlin.2, na.rm = F, id.vars = c('siteid', 'sampdate'))
\end_layout
\begin_layout Plain Layout
carlin.3.melt <- melt(carlin.3, na.rm = F, id.vars = c('siteid', 'sampdate'))
\end_layout
\begin_layout Plain Layout
carlin.4.melt <- melt(carlin.4, na.rm = F, id.vars = c('siteid', 'sampdate'))
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The scatter (X-Y) plots use only measured data; missing data are not included.
These plots show the measured values over time for each measured constituent.
For example, Figure
\begin_inset CommandInset ref
LatexCommand ref
reference "fig: scatter-plot group 1"
\end_inset
shows that most chemical constitent concentrations are low and have low
variability.
\begin_inset Float figure
wide false
sideways false
status collapsed
\begin_layout Plain Layout
\align center
\begin_inset Flex Chunk
status collapsed
\begin_layout Plain Layout
library(lattice)
\end_layout
\begin_layout Plain Layout
xyplot(value ~ sampdate | variable, data=carlin.1.melt, rm.na = T, ylab = 'Measured
Value', xlab = 'Date')
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption Standard
\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "fig: scatter-plot group 1"
\end_inset
Variability of measured values in group 1 constituents over the period of
record at the Carlin station.
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
Because river discharge (in cubic feet per second) spiked at about 8,000
cfs in the mis-1980s the Y-axis for all panels in the figure have the save
range.
This compresses low values yet any single constituent can be plotted by
itself and the details of its variability over time easily seen.
\end_layout
\begin_layout Standard
The second group of constituents (Figure
\begin_inset CommandInset ref
LatexCommand ref
reference "fig: scatter-plot group 2"
\end_inset
)
\begin_inset Float figure
wide false
sideways false
status collapsed
\begin_layout Plain Layout
\align center
\begin_inset Flex Chunk
status collapsed
\begin_layout Plain Layout
xyplot(value ~ sampdate | variable, data=carlin.2.melt, rm.na = T, ylab = 'Measured
Value', xlab = 'Date'
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
\begin_inset Caption Standard
\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "fig: scatter-plot group 2"
\end_inset
Variability of measured values in group 2 constituents over the period of
record at the Carlin station.
\end_layout
\end_inset
\end_layout
\end_inset
reveals that nutrients (the nitrogen compounds, phosphate, and organic
carbon) are all consistenly low with low variability while total dissolved
solids (TDS) concentrations are very highly variable both within years
(as shown by the different concentrations at the same date) and across
the period of observations.
Alkalinity and hardness are also quite variable at both time scales.
\end_layout
\end_body
\end_document
More information about the R-help
mailing list