'Plot correlation between variables separated by two groups with facets in R

I want to plot correlation pairs (Eye1,Hand1,Eye2,Hand2,Eye3,Hand3) and facet/ separate by groups and (1 and 2) and workspace. In the plot below, I want 3 correlation lines (each representing a workspace).enter image description here

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)


Solution 1:[1]

Your code is very messy.

My first thought was to use geom_smooth() but it is quite hard to tell it what you want to pool together and what not. Basically, my idea is to compute the linear model to get the intercept and slope first, and then plot them with geom_abline() by specifying the group (Index in this example).

First, extract estimates from lm() for each group.

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

Second, create a data set for convenience containing these estimate and their group.

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

Use the new data frame in geom_abline(data = estimate, aes(intercept = intercept, slope = slope, group = Index)) and specify the intercept, slope and their group.

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)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1