根据相关系数/lm 线重新排序多个散点图中的变量面板(在 r 中使用 ggplot)

Reordering panels of variables in multiple scatterplot based on correlation coefficient / lm line (using ggplot in r)

我正在尝试为每个变量创建一个包含多个面板的散点图。我想根据它们的 lm 线或相关系数(例如,从负到正)对面板重新排序。

因此在下面的示例中,我希望第一行包含 amdratvs 的地块,最后一行是 wt

library(tidyr)
library(ggplot2)

corr_eqn = function(x, y, digits = 2) {
  corr_coef = round(cor(x, y), digits = digits)
  paste("italix(r) == ", corr_coef)
}

mtcars_gath <- mtcars %>%
  gather(-mpg, key = "var", value = "value") 

labels = data.frame(x = 300, y = 10, label = corr_eqn(mtcars_gath$value, mtcars_gath$mpg))

mtcars_plot = mtcars %>%
  gather(-mpg, key = "var", value = "value") %>%
  ggplot(aes(x = value, y = mpg)) +
  geom_point() +
  facet_wrap(~ var) +
  theme_bw()+
  geom_smooth(method = "lm", se = FALSE, color = "black", fullrange = TRUE)+
  geom_text(data = labels, aes(x = x, y = y, label = label), parse = TRUE)
mtcars_plot 

此外,我无法为每个图添加相关系数(如本例所示)。

非常感谢任何提示:)

为了更改面板的顺序,您需要更改分面变量 var 的因子水平。

首先按照您希望绘制的顺序获取您的唯一 var 值:

library(tidyverse)

mtcars_gath <- mtcars %>%
  gather(-mpg, key = "var", value = "value") 

# calculate the correlation coefficients between each var and mpg
cor_df <- data.frame(var = unique(mtcars_gath$var),
                cor = sapply(unique(mtcars_gath$var), function(x) cor.test(mtcars[,x],mtcars$mpg)$estimate)
                ) 
# get your vector of var in the order you want for plotting
levs <- arrange(cor_df,desc(cor))$var

将您的 var 列更新为具有正确顺序水平的因子,这些将用作绘图面板的顺序。

mtcars_plot <- mtcars_gath %>%
  mutate(var = factor(var, levels=levs)) %>%
  ggplot(aes(x = value, y = mpg)) +
  geom_point() +
  facet_wrap(~ var) +
  theme_bw()+
  geom_smooth(method = "lm", se = FALSE, color = "black", fullrange = F)

mtcars_plot

按照@Chris 的回答,如果你还想添加标签,你需要使用与 factor 相同的技巧:

mtcast_gat <- mtcars %>%
  gather(-mpg, key = "var", value = "value")

#Corr function:
    corr_eqn = function(x, y, digits = 2) {
      corr_coef = round(cor(x, y), digits = digits)
      paste("italic(r) == ", corr_coef)
    }

labels <- mtcast_gat %>% 
  group_by(var) %>% # group by variable
  summarise(label = corr_eqn(value, mpg), # get the formatted corr
            cor = round(cor(value, mpg), 2)) %>% # get the numeric corr
  mutate(x = 300, y = -2000) # we add labels x,y


level <- arrange(labels,desc(cor))$var # level arrangement

labels <- labels %>% 
  mutate(var = factor(var, levels=levs)) # arrangement for labels

然后我们可以绘制:

mtcars %>%
  gather(-mpg, key = "var", value = "value") %>%
  mutate(var = factor(var, levels=levs)) %>% # correct arrangement
  arrange(var) %>% 
  ggplot(aes(x = value, y = mpg)) +
  geom_point() +
  facet_wrap(~ var) +
  geom_smooth(method = "lm", se = FALSE, color = "black", fullrange = TRUE) +
  geom_text(data = labels, aes(x = x, y = y, label = label), parse = TRUE) + # add labels
  theme_bw()