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).

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)

Try to add this to the ggplot command

+ geom_smooth(formula = y ~ x, method = lm)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.