[R] Plot two values on same axes
r@turner @end|ng |rom @uck|@nd@@c@nz
Fri Sep 27 01:12:18 CEST 2019
On 27/09/19 10:27 AM, Rich Shepard wrote:
> I want to plot maximum and minimum water temperatures on the same axes and
> thought I had the correct syntax:
> watertemp <- read.table("../../data/hydro/water-temp.dat", header =
> TRUE, sep =",")
> watertemp$sampdate <- as.Date(as.character(watertemp$sampdate))
> watertempsum <- summary(watertemp)
> maxwatemp <- xyplot(maxtemp ~ sampdate, data=watertemp, col="red",
> type="h", main="USGS Foss Gauge Water Temperatures, 1974-1981",
> ylab="Temperature (C)", xlab="Date", scales=list(tck=c(1,0)))
> minwatemp <- xyplot(mintemp ~ sampdate, data=watertemp, col="blue",
> However, R plots only minwatemp and displays this:
> Warning message:
> In par(new = TRUE) : calling par(new=TRUE) with no plot
> Despite my searching for the reason I don't see what syntax error I made.
> The two questions I ask of you:
> 1. Where have I gone wrong in this script?
> 2. With 2,492 daily observations in the source file (including 242 NAs for
> maxtemp and 243 NAs for mintemp), what would be a more appropriate plot to
> show both sets of data?
You are using xyplot() from the lattice package (which you did not
mention). Your par(new=TRUE) syntax would probably work with base R
graphics (although it would be suboptimal; one would normally use
points() to add a second graph on the same figure). I would hazard that
it won't work at all with lattice graphics.
Moreover you seem to be trying to mix lattice graphics and base R
graphics in a higgledy-piggledy fashion: first you call xyplot() and
then you call plot().
To do what you want with lattice graphics I think you need to learn
about and apply *panels* --- in particular panel.points(). Using panels
appropriately is a bit tricky, I find, and requires some study.
You may better off using base R graphics, unless there are
considerations which demand the use of lattice. Something like this, maybe:
plot(maxtemp ~ sampdate, data=watertemp, type="h", col="red",
points(maxtemp ~ sampdate, data=watertemp, type="h",col="blue")
(Not tested since your example is not reproducible.)
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
More information about the R-help