[R] 回覆︰ adding a line across plots in xy plot with panel.abline

Duncan Mackay dulcalma at bigpond.com
Mon Sep 14 17:48:38 CEST 2015


forgot to send to list

 

From: Duncan Mackay [mailto:dulcalma at bigpond.com] 
Sent: Tuesday, 15 September 2015 01:47
To: 'Christine Lee'
Subject: RE: 回覆︰ [R] adding a line across plots in xy plot with panel.abline

 

Hi

 

If your panel function was like this (just adding to plot the points with the argument panel.xyplot  

 

        panel= function(Date1,DI){

                 panel.xyplot(Date1,DI)

                 panel.abline(h=1,lty=2,lwd=3)

        }

 

and this would cause an error see ? panel.xyplot

you can test it by replacing the lines  and see what happens

 

Now with the panel function like this 

        panel = function(x, y, ...){

        

          panel.xyplot(x,y, ...)

          panel.abline(h=1,lty=2,lwd=3)

        }

 

it will “carry” the parameters in par.settings to the functions within the panel function.

One advantage of this is that you do not have to finish panel.xplot with what you want eg

 

panel.xyplot(x,y,  pch = 16, col = c(1,2), cex = 2)

 

In some contexts you can get away with just

   panel = function(...){

        

but in this case there is an error message in each panel saying y cannot be found

 

I cannot think of a good example at the moment run

 

demo(lattice::intervals )

and look at the code output

 

see also ? panel.superpose

 

 

Regards

 

Duncan

 

 

 

From: Christine Lee [mailto:leptostracan at yahoo.com] 
Sent: Monday, 14 September 2015 19:30
To: Duncan Mackay; R
Subject: 回覆︰ [R] adding a line across plots in xy plot with panel.abline

 

Thanks, Duncan,

 

I am confused by the function(x,y, ...) and panel.xyplot(x,y,...).  Is this a must to put in "..."? I knew this may be a stupid question.  I am sorry.

 

Christine

 

 

Duncan Mackay <dulcalma at bigpond.com> 於 2015年09月14日 (週一) 3:56 PM 寫道﹕

 

Hi 

As Bert has intimated there was a misspelling in panel.abline

To get points as well you need the appropriate arguments for a panel
function otherwise you would only get a line.
You only had a function for  a line in panel function so adding panel.xyplot
will give you points as well.


xyplot(DI~Date1|Station, data=Raw,
        groups = culr,
        par.settings = list(strip.background = list(col = "transparent"),
                            superpose.symbol = list(cex = rep(2, 2),
                                                    col=c("grey","black"),
                                                    pch = rep(16,2))),
        type="p",
        xlab=list("Month",cex=1.5),
        ylab=list("Dispersion index",cex=1.5),
        index.cond=list(c(1,2,3,4)),
        auto.key = T,
        layout=c(4,1),
        panel = function(x, y, ...){
        
          panel.xyplot(x,y, ...)
          panel.abline(h=1,lty=2,lwd=3)
        }

)

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Christine
Lee via R-help
Sent: Monday, 14 September 2015 13:43
To: r-help at r-project.org
Subject: [R] adding a line across plots in xy plot with panel.abline

Dear all,

I want to draw a line at DI=1 across all four graphs in the xy plot, I have
used panel.abline, but I failed to do so, does any one has an idea of what
has went wrong?

structure(list(Date = structure(c(6L, 7L, 2L, 4L, 13L, 17L, 5L, 
12L, 4L, 11L, 14L, 9L, 7L, 2L, 4L, 13L, 10L, 17L, 5L, 12L, 8L, 
4L, 11L, 14L, 9L, 16L, 15L, 3L, 10L, 1L, 17L, 5L, 12L, 8L, 4L, 
11L, 14L, 12L, 8L, 4L, 11L, 14L), .Label = c("1/10", "1/11", 
"11/11", "12/11", "13/10", "19/9", "2/10", "2/11", "20/9", "23/9", 
"26/11", "29/10", "29/11", "30/11", "31/10", "4/10", "6/10"), class =
"factor"), 
    Year = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
    2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("Y2002", "Y2014"), class = "factor"), 
    Station = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
    4L, 4L, 4L), .Label = c("E", "F", "H", "I"), class = "factor"), 
    DI = c(13.4, 7, 12.6, 12.6, 5.2, 2.2, 1, 1, 1.2, 0.8, 2.3, 
    3.8, 5.4, 4.6, 5, 3.2, 3.1, 8.7, 2.1, 2.7, 4.9, 4, 2.2, 5.3, 
    5.6, 4.8, 4, 8.6, 1.9, 2.9, 5.9, 2.6, 8.9, 4, 13.5, 15.3, 
    12.8, 3.4, 4.7, 1.7, 0.9, 1.7), Date1 = structure(c(16697, 
    16710, 16740, 16751, 16768, 16714, 16721, 16737, 16751, 16765, 
    16769, 16698, 16710, 16740, 16751, 16768, 16701, 16714, 16721, 
    16737, 16741, 16751, 16765, 16769, 16698, 16712, 16739, 16750, 
    16701, 16709, 16714, 16721, 16737, 16741, 16751, 16765, 16769, 
    16737, 16741, 16751, 16765, 16769), class = "Date")), .Names = c("Date",

"Year", "Station", "DI", "Date1"), row.names = c(NA, -42L), class =
"data.frame")
Raw$Date1<-as.Date(Raw$Date,"%d/%m")
culr<-ifelse(Raw$Year=="Y2002","Year 2002","Year 2014")
library(lattice)
xyplot(DI~Date1|Station, data=Raw,
        groups = culr,
        par.settings = list(strip.background = list(col = "transparent"),
                            superpose.symbol = list(cex = rep(2, 2),
                                                    col=c("grey","black"),
                                                    pch = rep(16,2))),
        type="p",
        xlab=list("Month",cex=1.5),
        ylab=list("Dispersion index",cex=1.5),  
        index.cond=list(c(1,2,3,4)),
        auto.key = T,
        layout=c(4,1),
        panel=function(Date1,DI){
        panel.labline(h=1,lty=2,lwd=3)
        })

Many thanks.

Regards,
Christine



______________________________________________
R-help at r-project.org 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.




	[[alternative HTML version deleted]]



More information about the R-help mailing list