繁体   English   中英

Plot R 中由两组分面的变量之间的相关性

[英]Plot correlation between variables separated by two groups with facets in R

我想要 plot 相关对(Eye1、Hand1、Eye2、Hand2、Eye3、Hand3)和 facet/ 按组和(1 和 2)和工作区分开。 在下面的 plot 中,我想要 3 条相关线(每条代表一个工作空间)。 在此处输入图像描述

data10 <- structure(list(Group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), Workspace = c(1, 1, 1, 1, 
2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3), 
    Eye1 = c(4, 8, 5, 6, 3, 5, 4, 4, 5, 5, 7, 6, 4, 8, 4, 7, 
    5, 6, 4, 4, 6, 5, 4, 5), Hand1 = c(5, 6, 4, 4, 6, 5, 4, 7, 
    7, 5, 5, 5, 6, 4, 5, 4, 4, 5, 6, 4, 4, 3, 4, 5), Eye2 = c(3, 
    7, 7, 4, 4, 5, 6, 4, 4, 3, 4, 3, 6, 7, 4, 6, 4, 6, 7, 4, 
    3, 3, 4, 6), Hand2 = c(4, 6, 7, 4, 3, 3, 7, 5, 6, 6, 6, 4, 
    7, 4, 4, 7, 4, 4, 4, 6, 5, 7, 5, 5), Eye3 = c(4, 4, 5, 4, 
    5, 2, 5, 7, 4, 4, 4, 6, 5, 7, 4, 6, 5, 5, 3, 2, 3, 5, 6, 
    5), Hand3 = c(5, 5, 3, 2, 3, 5, 3, 5, 4, 6, 6, 4, 4, 4, 6, 
    4, 6, 3, 5, 4, 4, 5, 5, 7)), row.names = c(NA, -24L), spec = structure(list(
    cols = list(Group = structure(list(), class = c("collector_double", 
    "collector")), Workspace = structure(list(), class = c("collector_double", 
    "collector")), Eye1 = structure(list(), class = c("collector_double", 
    "collector")), Hand1 = structure(list(), class = c("collector_double", 
    "collector")), Eye2 = structure(list(), class = c("collector_double", 
    "collector")), Hand2 = structure(list(), class = c("collector_double", 
    "collector")), Eye3 = structure(list(), class = c("collector_double", 
    "collector")), Hand3 = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))
dataLong <- data10 %>% pivot_longer(cols = -Group,
                                   names_to=c(".value","Index"),
                                   names_pattern = "(Eye|Hand)(\\d)") %>% 
  mutate(Group=paste0("Grp_",Group))

Cors <- dataLong %>% group_by(Group,Index) %>% summarize(Cor=round(cor(Eye,Hand),3))

ggplot(dataLong,aes(x=Hand,y=Eye))+geom_point()+
  facet_grid(Index~Group)+
  geom_text(aes(x=4,y=4,label=paste("r=",Cor)),data=Cors)

你的代码很乱。

我的第一个想法是使用geom_smooth()但很难告诉它你想要汇集什么而不是。 基本上,我的想法是先计算线性 model 以获得截距和斜率,然后通过指定组(在此示例中为Index )使用geom_abline()计算 plot 它们。

首先,从每组的lm()中提取估计值。

est <- by(dataLong, dataLong$Index, FUN = function(x) (lm(Eye~Hand, data=x)))
est <- sapply(est, coef)

其次,为方便起见创建一个包含这些估计及其组的数据集。

estimate <- data.frame(Index = 1:3,
                  intercept = est[1,],
                  slope = est[2,])

geom_abline(data = estimate, aes(intercept = intercept, slope = slope, group = Index))中使用新数据框并指定截距、斜率及其组。

ggplot(dataLong,aes(x=Hand,y=Eye))+
  geom_point() +
  geom_abline(data = estimate, aes(intercept = intercept, slope = slope, group = Index)) +
  facet_grid(Index~Group)+
  geom_text(aes(x=4,y=4,label=paste("r=",Cor)),data=Cors)

在此处输入图像描述

暂无
暂无

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

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