Plot two values on same axes

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Plot two values on same axes

Rich Shepard
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)
print(watertempsum)
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", type="h")
plot(maxwatemp)
par(new=TRUE)
plot(minwatemp)

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?

Thanks in advance,

Rich

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Plot two values on same axes

David Winsemius
Instead of trying to mix lattice and base functions, you might try using the formula:

maxtemp+mintemp ~ sampdate

And then: col= c(“red”, “blue”)

Sent from my iPhone, so make sure those quotes are ordinary double quotes.


David


> On Sep 27, 2019, at 6:27 AM, Rich Shepard <[hidden email]> 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)
> print(watertempsum)
> 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", type="h")
> plot(maxwatemp)
> par(new=TRUE)
> plot(minwatemp)
>
> 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?
>
> Thanks in advance,
>
> Rich
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Plot two values on same axes

Rolf Turner
In reply to this post by Rich Shepard

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)
> print(watertempsum)
> 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",
> type="h")
> plot(maxwatemp)
> par(new=TRUE)
> plot(minwatemp)
>
> 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",
                          <annotation arguments>)
points(maxtemp ~ sampdate, data=watertemp, type="h",col="blue")

(Not tested since your example is not reproducible.)

cheers,

Rolf Turner

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Re: Plot two values on same axes

Rolf Turner
In reply to this post by David Winsemius

On 27/09/19 11:08 AM, David Winsemius wrote:

> Instead of trying to mix lattice and base functions, you might try using the formula:
>
> maxtemp+mintemp ~ sampdate
>
> And then: col= c(“red”, “blue”)
>
> Sent from my iPhone, so make sure those quotes are ordinary double quotes.

Ah-ha!  I've learned something about lattice that I had not previously
taken on board.  Thanks David!

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Plot two values on same axes

Rich Shepard
In reply to this post by David Winsemius
On Fri, 27 Sep 2019, David Winsemius wrote:

> Instead of trying to mix lattice and base functions, you might try using
> the formula:
>
> maxtemp+mintemp ~ sampdate
>
> And then: col= c(“red”, “blue”)

David,

I certainly will. I've not needed R for projects for many months and when I
looked through previous scripts and the lattice book I failed to find what I
needed. So what I tried was a solution to a different but similar issue on
stackexchange.

Many thanks,

Rich

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Plot two values on same axes

Rich Shepard
In reply to this post by Rolf Turner
On Fri, 27 Sep 2019, Rolf Turner wrote:

> 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",
>                         <annotation arguments>)
> points(maxtemp ~ sampdate, data=watertemp, type="h",col="blue")

Rolf,

Thanks very much.

Regards,

Rich

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.