简体   繁体   English

当数据具有两个级别时,如何在散点图矩阵的下部面板中写出Pearson相关系数?

[英]How to write the Pearson correlation coefficient in the lower panel of a scatterplot matrix when data has 2 levels?

I would like to generate a matrix of scatterplots from the following data frame. 我想从以下数据帧生成散点图矩阵。

# Generate some fake data
set.seed(123)
fakeData <- rnorm(10)
df <- data.frame(Type=c(rep("A", 5), rep("B", 5)), 
                 Syst=fakeData, Bio=2*fakeData, Blr=fakeData^2)

If I use the pairs function, I get scatterplots both below and above the diagonal of my scatterplot matrix. 如果我使用结pairs函数,则会在散点图矩阵的对角线以下和上方得到散点图。

I do want to keep the scatterplots in the upper panel, however, I would like to "plot" the correlation coefficient of my data in the lower panel. 我确实希望将散点图保留在上部面板中,但是,我想在下部面板中“绘制”数据的相关系数。

I have looked for an answer online, and despite finding some good explanation, I have had no success so far. 我一直在网上寻找答案,尽管找到了很好的解释,但到目前为止我还没有成功。 Like here , and here , here too , and here as well . 就像这里这里这里也是这里 While elucidating, these examples don't go over cases when there are data with different levels in the data frame. 在阐明时,这些示例不会覆盖数据帧中存在不同级别数据的情况。

As my data indicate, there are two levels in my data frame, "A" and "B". 如我的数据所示,我的数据框中有两个级别:“ A”和“ B”。 Hence, I'd like to have two correlation coefficient in each "box" of my lower panel, one for the data whose level is A and another for the data whose level is B. For instance, in plotting pairs(df[2:4]), I'd like to see these two coefficients in the first box of the second line (lower panel) of my matrix. 因此,我想在下部面板的每个“框中”有两个相关系数,一个用于级别为A的数据,另一个用于级别为B的数据。例如,在绘制pair(df [2: 4]),我想在矩阵第二行(下部面板)的第一框中看到这两个系数。

This line of code 这行代码

pairs(df[2:4], main="", pch=21, bg=c("red","blue"), lower.panel=NULL)

will plot the scatterplot matrix on the upper panel. 将在上方面板上绘制散点图矩阵。 By assign color options to bg , I can differentiate between A and B data points. 通过为bg分配颜色选项,我可以区分A和B数据点。 Ideally, my Pearson correlation coefficient will be plotted in the same as their respective data were. 理想情况下,我的Pearson相关系数将以与它们各自数据相同的方式绘制。


Attempt # 1 - I took the commented function below and changed a bit so as to accommodate the changes needed for the desired result. 尝试#1-我采用了下面的注释功能,并进行了一些更改,以便适应所需的更改以获得所需的结果。

# 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 <- paste(prefix, txt, sep="")
#   if(missing(cex.cor)) cex.cor <- 2
#   text(0.5, 0.5, txt, cex = cex.cor)
# }

I know my data frame "df" has 10 rows. 我知道我的数据框“ df”有10行。 Suppose I want to print the correlation of only the data whose level is A in the lower panel. 假设我只想在下面的面板中打印级别为A的数据的相关性。 I thought of changing x and y dimensions to restrict both variables to take only level-A data. 我考虑过更改x和y尺寸,以限制两个变量仅获取A级数据。

panel.cor <- function(x, y, digits=2, prefix="", cex.cor, ...)
{
  x <- x[1:5,1:3]
  y <- y[1:5,1:3]
  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 <- paste(prefix, txt, sep="")
  if(missing(cex.cor)) cex.cor <- 2
  text(0.5, 0.5, txt, cex = cex.cor)
}

Unfortunately, this didn't work either. 不幸的是,这也不起作用。 I get an error message that says incorrect number of dimensions 我收到一条错误消息,指出incorrect number of dimensions

The function ggscatmat from the GGally library will do the trick. 该功能ggscatmatGGally库会做的伎俩。

For example, for the generated data, a satisfactory scatterplot matrix will be plotted with 例如,对于生成的数据,将绘制一个令人满意的散点图矩阵,其中

ggscatmat(df, columns = 2:4, color = "Type", alpha = 0.25) 

Further ggplot specifications, as scale_color_... and theme , will work as well. 进一步的ggplot规范(如scale_color_...theme )也将起作用。 Of course, as with any package function, one may need to tweak with it a bit in order to get the desired result. 当然,与任何包功能一样,可能需要对其进行一些微调才能获得所需的结果。 However, this function is an excellent start. 但是,此功能是一个很好的开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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