how to pass arguments to panel.cor in pairs()

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

how to pass arguments to panel.cor in pairs()

Jinsong Zhao-2
Hi there,

Here is the panel.cor function from ?pairs:

     panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...)
     {
         usr <- par("usr"); on.exit(par(usr))
         par(usr = c(0, 1, 0, 1))
         r <- abs(cor(x, y))
         txt <- format(c(r, 0.123456789), digits = digits)[1]
         txt <- paste0(prefix, txt)
         if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
         text(0.5, 0.5, txt, cex = cex.cor * r)
     }

in the following code, pairs use the panel.cor defined above:

     pairs(USJudgeRatings, lower.panel = panel.smooth, upper.panel = panel.cor,
           gap=0, row1attop=FALSE)

I try to change prefix = "" to prefix = "r = ", something like:

     pairs(USJudgeRatings, lower.panel = panel.smooth, upper.panel = panel.cor, prefix = "r = ",
           gap=0, row1attop=FALSE)

I got lots of warnings.

How to change prefix to something else, but not change the defined panel.cor? Thanks!

Best,
Jinsong

        [[alternative HTML version deleted]]

______________________________________________
[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: how to pass arguments to panel.cor in pairs()

Duncan Murdoch-2
On 09/10/2018 10:53 AM, Jinsong Zhao wrote:

> Hi there,
>
> Here is the panel.cor function from ?pairs:
>
>       panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...)
>       {
>           usr <- par("usr"); on.exit(par(usr))
>           par(usr = c(0, 1, 0, 1))
>           r <- abs(cor(x, y))
>           txt <- format(c(r, 0.123456789), digits = digits)[1]
>           txt <- paste0(prefix, txt)
>           if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
>           text(0.5, 0.5, txt, cex = cex.cor * r)
>       }
>
> in the following code, pairs use the panel.cor defined above:
>
>       pairs(USJudgeRatings, lower.panel = panel.smooth, upper.panel = panel.cor,
>             gap=0, row1attop=FALSE)
>
> I try to change prefix = "" to prefix = "r = ", something like:
>
>       pairs(USJudgeRatings, lower.panel = panel.smooth, upper.panel = panel.cor, prefix = "r = ",
>             gap=0, row1attop=FALSE)
>
> I got lots of warnings.
>
> How to change prefix to something else, but not change the defined panel.cor? Thanks!

Since panel.cor is not a base function, just a function defined in the
example, you could just modify it.

If you want the regular one sometimes and a custom one other times,
you could make your own panel function:

   panel.cor2 <- function(...) panel.cor(..., prefix = "r = ")

Then use


   pairs(USJudgeRatings, lower.panel = panel.smooth, upper.panel =
panel.cor2, gap=0, row1attop=FALSE)

Duncan Murdoch

______________________________________________
[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: how to pass arguments to panel.cor in pairs()

Jinsong Zhao-2
On 2018/10/9 23:22, Duncan Murdoch wrote:

> On 09/10/2018 10:53 AM, Jinsong Zhao wrote:
>> Hi there,
>>
>> Here is the panel.cor function from ?pairs:
>>
>>       panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...)
>>       {
>>           usr <- par("usr"); on.exit(par(usr))
>>           par(usr = c(0, 1, 0, 1))
>>           r <- abs(cor(x, y))
>>           txt <- format(c(r, 0.123456789), digits = digits)[1]
>>           txt <- paste0(prefix, txt)
>>           if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
>>           text(0.5, 0.5, txt, cex = cex.cor * r)
>>       }
>>
>> in the following code, pairs use the panel.cor defined above:
>>
>>       pairs(USJudgeRatings, lower.panel = panel.smooth, upper.panel =
>> panel.cor,
>>             gap=0, row1attop=FALSE)
>>
>> I try to change prefix = "" to prefix = "r = ", something like:
>>
>>       pairs(USJudgeRatings, lower.panel = panel.smooth, upper.panel =
>> panel.cor, prefix = "r = ",
>>             gap=0, row1attop=FALSE)
>>
>> I got lots of warnings.
>>
>> How to change prefix to something else, but not change the defined
>> panel.cor? Thanks!
>
> Since panel.cor is not a base function, just a function defined in the
> example, you could just modify it.
>
> If you want the regular one sometimes and a custom one other times,
> you could make your own panel function:
>
>    panel.cor2 <- function(...) panel.cor(..., prefix = "r = ")
>
> Then use
>
>
>    pairs(USJudgeRatings, lower.panel = panel.smooth, upper.panel =
> panel.cor2, gap=0, row1attop=FALSE)
>

That is what I want to know. Thanks a lot.

Best,
Jinsong

______________________________________________
[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.