Rich Shepard
2015-Jul-11 14:03 UTC
[R] Trellis Plots: translating lattice xyplot() to ggplot() [RESOLVED]
On Fri, 10 Jul 2015, Yihui Xie wrote:> Your LyX example has two problems:Yihui, et al.: All fixed now. After adding the specific libraries the chuck calling xyplot() to produce the graphic caused an error when I tried to generate a dvips preview: LaTeX Error: File 'figure/unnamed-chunk-7-1' not found. I could not locate the file with any of these extensions: .eps,.ps,.eps.gz,.ps.gz,.eps.Z Which is what sent me down the path of specifying pdf() and dev.off(). However, compiling with pdflatex produces the graphic in the document. All part of being brand new to using knitr. Thank you all very much, Rich
Yihui Xie
2015-Jul-11 19:01 UTC
[R] Trellis Plots: translating lattice xyplot() to ggplot() [RESOLVED]
I guess you didn't tell us you were compiling the document with dvips (BTW, I'm surprised that dvips is still alive today...), otherwise the solution would be simply to use the postscript device instead of the default pdf device (i.e. use the chunk option dev='postscript'). Hopefully you learned some lessons on what a reproducible example means. Regards, Yihui -- Yihui Xie <xieyihui at gmail.com> Web: http://yihui.name On Sat, Jul 11, 2015 at 9:03 AM, Rich Shepard <rshepard at appl-ecosys.com> wrote:> On Fri, 10 Jul 2015, Yihui Xie wrote: > >> Your LyX example has two problems: > > > Yihui, et al.: > > All fixed now. After adding the specific libraries the chuck calling > xyplot() to produce the graphic caused an error when I tried to generate a > dvips preview: > > LaTeX Error: File 'figure/unnamed-chunk-7-1' not found. > > I could not locate the file with any of these extensions: > .eps,.ps,.eps.gz,.ps.gz,.eps.Z > > Which is what sent me down the path of specifying pdf() and dev.off(). > > However, compiling with pdflatex produces the graphic in the document. All > part of being brand new to using knitr. > > Thank you all very much, > > Rich
Rich Shepard
2015-Jul-12 00:07 UTC
[R] Knitr/Lattice/Lyx [was: Trellis Plots: translating lattice xyplot() to ggplot()]
On Sat, 11 Jul 2015, Yihui Xie wrote:> I guess you didn't tell us you were compiling the document with dvipsThat's how I preview lyx docs for the past decade-and-a-half or so. The final .pdf is generated with pdflatex. There is now an issue when a second figure is added to the document: it won't compile. 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/. Here are the lyx messages when I tried compiling the document with pdflatex: 16:51:11.255: Exporting ... 16:51:11.259: (buffer-export pdf2) 16:51:11.278: Rscript --verbose --no-save --no-restore "/usr/share/lyx/scripts/lyxknitr.R" "/tmp/lyx_tmpdir.rGQZdOZ30938/lyx_tmpbuf3/""test.Rnw" "/tmp/lyx_tmpdir.rGQZdOZ30938/lyx_tmpbuf3/""test.tex" ISO-8859-15 "/home/rshepard/documents/white-papers/water-chem-anal/" 16:51:11.281: running 16:51:11.286: '/usr/lib/R/bin/R --slave --no-restore --no-save --no-restore --file=/usr/share/lyx/scripts/lyxknitr.R --args -- /tmp/lyx_tmpdir.rGQZdOZ30938/lyx_tmpbuf3/test.Rnw -- / /tmp/lyx_tmpdir.rGQZdOZ30938/lyx_tmpb 16:51:11.288: uf3/test.tex ISO-8859-15 /home/rshepard/documents/white-papers/water-chem-anal/' 16:51:11.292: 16:51:11.697: Loading required package: methods 16:51:11.709: Loading required package: survival 16:51:11.712: Loading required package: graphics 16:51:11.721: Loading required package: stats 16:51:11.921: 16:51:11.923: Attaching package: ?NADA? 16:51:11.925: 16:51:11.927: The following object is masked from ?package:stats?: 16:51:11.929: 16:51:11.932: cor 16:51:11.933: 16:51:11.969: 16:51:11.970: Attaching package: ?zoo? 16:51:11.972: 16:51:11.975: The following objects are masked from ?package:base?: 16:51:11.976: 16:51:11.979: as.Date, as.Date.numeric 16:51:11.981: 16:51:12.091: 16:51:12.092: 16:51:12.094: processing file: /tmp/lyx_tmpdir.rGQZdOZ30938/lyx_tmpbuf3/test.Rnw 16:51:12.112: | | | 0% | 16:51:12.113: |.... | 6% 16:51:12.116: ordinary text without R code 16:51:12.118: 16:51:12.120: | |........ | 12% 16:51:12.123: label: unnamed-chunk-1 (with options) 16:51:12.126: List of 1 16:51:12.128: $ echo: logi FALSE 16:51:12.130: 16:51:12.204: | |........... | 18% 16:51:12.205: ordinary text without R code 16:51:12.207: 16:51:12.210: | |............... | 24% 16:51:12.213: label: unnamed-chunk-2 16:51:12.239: | |................... | 29% 16:51:12.240: ordinary text without R code 16:51:12.242: 16:51:12.244: | |....................... | 35% 16:51:12.247: label: unnamed-chunk-3 16:51:12.293: | |........................... | 41% 16:51:12.295: ordinary text without R code 16:51:12.297: 16:51:12.299: | |............................... | 47% 16:51:12.302: label: unnamed-chunk-4 16:51:12.344: | |.................................. | 53% 16:51:12.346: ordinary text without R code 16:51:12.348: 16:51:12.350: | |...................................... | 59% 16:51:12.353: label: unnamed-chunk-5 16:51:12.432: | |.......................................... | 65% 16:51:12.434: ordinary text without R code 16:51:12.436: 16:51:12.438: | |.............................................. | 71% 16:51:12.441: label: unnamed-chunk-6 16:51:12.484: | |.................................................. | 76% 16:51:12.486: ordinary text without R code 16:51:12.488: 16:51:12.490: | |...................................................... | 82% 16:51:12.493: label: unnamed-chunk-7 16:51:12.837: | |......................................................... | 88% 16:51:12.839: ordinary text without R code 16:51:12.841: 16:51:12.843: | |............................................................. | 94% 16:51:12.846: label: unnamed-chunk-8 16:51:12.849: Quitting from lines 101-102 (/tmp/lyx_tmpdir.rGQZdOZ30938/lyx_tmpbuf3/test.Rnw) 16:51:12.851: Error in parse(text = x, srcfile = src) : 16:51:12.853: <text>:2:0: unexpected end of input 16:51:12.855: 1: xyplot(value ~ sampdate | variable, data=carlin.2.melt, rm.na = T, ylab = 'Measured Value', xlab = 'Date' 16:51:12.857: ^ 16:51:12.859: Calls: knit ... <Anonymous> -> parse_all -> parse_all.character -> parse 16:51:12.861: 16:51:12.862: Execution halted Systemcall.cpp (292): Systemcall: 'Rscript --verbose --no-save --no-restore "/usr/share/lyx/scripts/lyxknitr.R" "/tmp/lyx_tmpdir.rGQZdOZ30938/lyx_tmpbuf3/""test.Rnw" "/tmp/lyx_tmpdir.rGQZdOZ30938/lyx_tmpbuf3/""test.tex" ISO-8859-15 "/home/rshepard/documents/white-papers/water-chem-anal/"' finished with exit code 1 Error: Cannot convert file ---------------------------------------- An error occurred while running: Rscript --verbose --no-save --no-restore "/usr/share/lyx/scripts/lyxknitr.R" "/tmp/lyx_tmpdir.rGQZdOZ30938/lyx_tmpbuf3/""test.Rnw" "/tmp/lyx_tmpdir.rGQZdOZ30938/lyx_tmpbuf3/""test.tex" ISO-8859-15 "/home/rshepard/documents/white-papers/water-chem-anal/" 16:51:16.136: Error while exporting format: PDF (pdflatex) There is no log file in the tempbuf. Note the references to unnamed-chunks. Each chunk is labeled with a number when opened in lyx; the two figures each have a distinct label. I don't see the specific error generated by adding a second figure to the document. If there are more files needed to reproduce this situation, let me know and I'll provide it. 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 -------------- next part -------------- \batchmode \makeatletter \def\input at path{{/home/rshepard/documents/white-papers/water-chem-anal//}} \makeatother \documentclass[letterpaper,twoside,english,abstract=on]{scrreprt} \usepackage{mathpazo} \usepackage[T1]{fontenc} \usepackage[latin9]{inputenc} \makeatletter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands. \pdfpageheight\paperheight \pdfpagewidth\paperwidth %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands. \usepackage[natbibapa]{apacite} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands. \date{} \usepackage{textcomp,url,multicol} %\setkomafont{sectioning}{\rmfamily} \makeatother \usepackage{babel} \begin{document} <<echo=FALSE>>load('.RData') @ Some text in here; separating knitr chunks. <<>>carlin <- read.csv("./carlin.csv", header = TRUE, sep = ",", stringsAsFactors = F) @ Next paragraph of text. <<>>carlin$sampdate <- as.Date(carlin$sampdate) str(carlin) @ Another paragraph or two of text. <<>>summary(carlin) @ Plots of these distributions are the next step ... <<>>carlin.1 <- subset(carlin, select = siteid:CO3) carlin.2 <- subset(carlin, select = c(siteid, sampdate, Alk:Ca)) carlin.3 <- subset(carlin, select = c(siteid, sampdate, Mg:Cr)) carlin.4 <- subset(carlin, select = c(siteid, sampdate, Co:Hg)) @ To plot each chemical constituent's concentration as a function of collection date the data format needs to be reshaped from wide to long: <<>>library(reshape2) carlin.1.melt <- melt(carlin.1, na.rm = F, id.vars = c('siteid', 'sampdate')) carlin.2.melt <- melt(carlin.2, na.rm = F, id.vars = c('siteid', 'sampdate')) carlin.3.melt <- melt(carlin.3, na.rm = F, id.vars = c('siteid', 'sampdate')) carlin.4.melt <- melt(carlin.4, na.rm = F, id.vars = c('siteid', 'sampdate')) @ 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 \ref{fig: scatter-plot group 1} shows that most chemical constitent concentrations are low and have low variability. \begin{figure} \begin{centering} <<>>library(lattice) xyplot(value ~ sampdate | variable, data=carlin.1.melt, rm.na = T, ylab = 'Measured Value', xlab = 'Date') @ \par\end{centering} \protect\caption{\label{fig: scatter-plot group 1}Variability of measured values in group 1 constituents over the period of record at the Carlin station.} \end{figure} 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. The second group of constituents (Figure \ref{fig: scatter-plot group 2}) \begin{figure} \begin{centering} <<>>xyplot(value ~ sampdate | variable, data=carlin.2.melt, rm.na = T, ylab = 'Measured Value', xlab = 'Date' @ \par\end{centering} \protect\caption{\label{fig: scatter-plot group 2}Variability of measured values in group 2 constituents over the period of record at the Carlin station.} \end{figure} 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{document}