简体   繁体   中英

Simple way for adding multiple R^2-values in a plotmatrix

So I have a simple multi-plot/plotmatrix of the following form:

DATA_SE <- read.table("DEWRATES_SE_15-17.txt", sep = "\t", dec = ".", header = T)
multiplot_SE <- pairs(~SE_21+SE_25+SE_26, data = DATA_SE, main = "Tauraten_Selhausen")
multiplot_SE  

在此处输入图片说明

Is there any way to add r-squared-values (for a simple lm-modell) to each one of my plots?

Thanks!

Update:

Is there a way to set a fixed limit for the x- and y-axis of my plot-panels? I just need to set the them all at the same value (even for x- and y)!

You can do something like this (since you don't provide sample data I'm using the iris dataset to demonstrate):

panel.rsquared <- function(x, y) {
    fit <- lm(y ~ x)
    usr <- par("usr")
    on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    text(0.5, 0.5, sprintf(
        "R squared = %4.3f\n Adj. R squared = %4.3f",
        summary(fit)$r.squared,
        summary(fit)$adj.r.squared))
}   

pairs(iris[, -ncol(iris)], upper.panel = panel.rsquared)

在此处输入图片说明


Update

In response to your comment, you can define any upper/lower panel function to meet your needs.

For example, you could do something like I'm showing below. Mind you, this is not very useful, as it will be difficult (impossible) to avoid overlapping text and points. That's the whole idea (and strength) of pairs when configuring the upper panel to show annotation/text and the lower panel to show the plots. That way you avoid redundancies (in your original post plots are repeated and are therefore redundant).

Anyway, for what it's worth:

panel.plot_withrsquared <- function(x, y) {
    points(x, y)
    fit <- lm(y ~ x)
    usr <- par("usr")
    on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    text(0.1, 0.8,
        sprintf("R squared = %4.3f",summary(fit)$r.squared),
        adj = 0, cex = 0.8)
    }

pairs(
    iris[, -ncol(iris)],
    upper.panel = panel.rsquared,
    lower.panel = panel.plot_withrsquared)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM