# Plot correlation between variables by group with possible facet in R

I want to do one of two things:

• either plot the correlation between a pair of variables (Eye1 and Hand 1) and facet/ separate by groups (1 and 2).

• Or get correlation pairs (Eye1,Hand1,Eye2,Hand2,Eye3,Hand3) and facet/ separate by groups (1 and 2)

``````data10 <- structure(list(Group = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2),
Eye1 = c(5, 5, 8, 5, 7, 6, 6, 8, 5, 6, 7, 5), Hand1 = c(6,
6, 7, 6, 6, 7, 7, 6, 7, 6, 6, 7), Eye2 = c(5, 5, 8, 5, 7,
5, 6, 8, 5, 6, 7, 5), Hand2 = c(6, 6, 2, 6, 6, 3, 7, 6, 4,
6, 6, 7), Eye3 = c(2, 5, 8, 3, 7, 6, 6, 5, 5, 2, 7, 5), Hand3 = c(6,
6, 7, 6, 6, 7, 7, 6, 7, 6, 6, 7)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), spec = structure(list(
cols = list(Group = 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")), skip = 1), class = "col_spec"))

``````
``````library(ggplot)

p <- ggplot(data10, aes(Eye1, Hand1)) + geom_point()

cors <- ddply(data10, c("1", "2"), summarise, cor = round(cor(Eye1, Hand1), 2))

p + facet_grid(1 ~ 2) +
geom_text(data=data10, aes(label=paste("r=", cor, sep="")), x=30, y=4)
``````
1 Like

Are you looking for something like this?

``````data10 <- structure(list(Group = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2),
Eye1 = c(5, 5, 8, 5, 7, 6, 6, 8, 5, 6, 7, 5),
Hand1 = c(6, 6, 7, 6, 6, 7, 7, 6, 7, 6, 6, 7),
Eye2 = c(5, 5, 8, 5, 7, 5, 6, 8, 5, 6, 7, 5),
Hand2 = c(6, 6, 2, 6, 6, 3, 7, 6, 4, 6, 6, 7),
Eye3 = c(2, 5, 8, 3, 7, 6, 6, 5, 5, 2, 7, 5),
Hand3 = c(6, 6, 7, 6, 6, 7, 7, 6, 7, 6, 6, 7)),
class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L),
spec = structure(list(cols = list(Group = 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")), skip = 1), class = "col_spec"))
library(dplyr)
library(tidyr)
library(ggplot2)

dataLong <- data10 |> pivot_longer(cols = -Group,
names_to=c(".value","Index"),
names_pattern = "(Eye|Hand)(\\d)") |>
mutate(Group=paste0("Grp_",Group))

dataLong
#> # A tibble: 36 x 4
#>    Group Index   Eye  Hand
#>    <chr> <chr> <dbl> <dbl>
#>  1 Grp_1 1         5     6
#>  2 Grp_1 2         5     6
#>  3 Grp_1 3         2     6
#>  4 Grp_1 1         5     6
#>  5 Grp_1 2         5     6
#>  6 Grp_1 3         5     6
#>  7 Grp_1 1         8     7
#>  8 Grp_1 2         8     2
#>  9 Grp_1 3         8     7
#> 10 Grp_1 1         5     6
#> # ... with 26 more rows
Cors <- dataLong |> group_by(Group,Index) |> summarize(Cor=round(cor(Eye,Hand),3))
#> `summarise()` has grouped output by 'Group'. You can override using the `.groups` argument.
Cors
#> # A tibble: 6 x 3
#> # Groups:   Group [2]
#>   Group Index    Cor
#>   <chr> <chr>  <dbl>
#> 1 Grp_1 1      0.612
#> 2 Grp_1 2     -0.506
#> 3 Grp_1 3      0.613
#> 4 Grp_2 1     -0.781
#> 5 Grp_2 2      0.156
#> 6 Grp_2 3      0.218
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)
``````

Created on 2022-02-17 by the reprex package (v2.0.1)

1 Like

I am using a longer data set but it seems to be giving me just a single value for the correlation

``````data10 <- structure(list(Group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2), Eye1 = c(0.1625, 0.6145, 0.1335, 0.083, 0.1165,
0.2585, 0.2085, 0.549, 0.142, 0.1585, 0.1835, 0.2585, 0.1835,
0.334, 0.1505, 0.166, 0.1085, 0.25, 0.1505, 0.452, 0.117, 0.183,
0.1745, 0.3335, 0.158, 0.3, 0.071, 0.133, 0.15, 0.3085, 0.133,
0.3415, 0.125, 0.25, 0.158, 0.3965, 0.158, 0.167, 0.1, 0.117,
0.1745, 0.1335, 0.134, 0.25, 0.1335, 0.125, 0.1, 0.175, 0.1835,
0.133, 0.1, 0.125, 0.167, 0.184, 0.1415, 0.4805, 0.1335, 0.1085,
0.133, 0.133, 0.167, 0.1335, 0.116, 0.134, 0.1505, 0.1415, 0.092,
0.367, 0.166, 0.1, 0.167, 0.15, 0.142, 0.2, 0.1505, 0.168, 0.116,
0.2, 0.3585, 0.1835, 0.1165, 0.1585, 0.175, 0.258, 0.154, 0.167,
0.167, 0.136, 0.1, 0.2085, 0.3445, 0.192, 0.116, 0.116, 0.166,
0.2, 0.129, 0.2, 0.1165, 0.1165, 0.117, 0.242, 0.327, 0.192,
0.1, 0.167, 0.154, 0.242, 0.125, 0.133, 0.1495, 0.1165, 0.117,
0.117, 0.0915, 0.1585, 0.117, 0.108, 0.117, 0.1165, 0.1715, 0.167,
0.133, 0.116, 0.133, 0.133, 0.083, 0.1865, 0.133, 0.1165, 0.117,
0.116, 0.1, 0.134, 0.1165, 0.117, 0.134, 0.125, 0.083, 0.225,
0.108, 0.125, 0.117, 0.1165), Hand1 = c(0.369, 0.75, 0.35, 0.35,
0.2165, 0.3835, 0.517, 0.433, 0.283, 0.367, 0.475, 0.4, 0.4125,
0.4005, 0.35, 0.4, 0.2165, 0.392, 0.5085, 0.433, 0.258, 0.3665,
0.4165, 0.383, 0.329, 0.667, 0.3665, 0.3165, 0.275, 0.466, 0.6415,
0.4585, 0.2835, 0.433, 0.433, 0.4245, 0.358, 0.4, 0.3, 0.367,
0.3, 0.267, 0.434, 0.45, 0.519, 0.3335, 0.3, 0.742, 0.3335, 0.3,
0.333, 0.325, 0.335, 0.3, 0.4165, 0.522, 0.3755, 0.3745, 0.275,
0.4, 0.3, 0.35, 0.284, 0.4, 0.35, 0.325, 0.4335, 0.564, 0.466,
0.3415, 0.333, 0.567, 0.3665, 0.2845, 0.3, 0.3995, 0.2585, 0.358,
0.4415, 0.4, 0.2745, 0.366, 0.475, 0.558, 0.3195, 0.3495, 0.333,
0.583, 0.267, 0.3335, 0.6475, 0.367, 0.283, 0.391, 0.467, 0.4835,
0.354, 0.383, 0.3, 0.4, 0.25, 0.3165, 0.526, 0.392, 0.2745, 0.3745,
0.508, 0.475, 0.329, 0.3075, 0.3, 0.3, 0.292, 0.283, 0.3165,
0.2415, 0.3915, 0.3335, 0.3255, 0.333, 0.3835, 0.3, 0.3, 0.292,
0.2665, 0.3, 0.342, 0.275, 0.4, 0.2655, 0.3085, 0.334, 0.341,
0.267, 0.3195, 0.317, 0.283, 0.3165, 0.4, 0.25, 0.3415, 0.3165,
0.316, 0.3335), Eye2 = c(0.152, 1.401, 1.111, 2.372, 0.2005,
0.4805, 1.663, 1.97, 0.719, 1.3735, 1.283, 0.795, 0.1625, 1.406,
1.0035, 2.323, 0.167, 0.2725, 1.453, 1.265, 0.343, 1.055, 1.199,
0.3825, 0.923, 2.033, 0.9435, 1.76, 0.2335, 0.473, 2.543, 2.2415,
0.7535, 1.691, 1.437, 1.2865, 0.314, 0.566, 0.2665, 0.166, 0.5945,
0.514, 0.333, 0.498, 3.879, 0.309, 0.344, 2.2515, 0.4295, 0.366,
0.333, 0.167, 0.167, 0.333, 0.333, 0.346, 3.161, 0.3, 0.434,
0.383, 0.4, 1.035, 0.334, 0.15, 0.6835, 1.417, 1.99, 0.646, 4.727,
0.89, 0.804, 1.695, 0.254, 0.445, 0.3665, 1.353, 0.1665, 0.2745,
0.5535, 0.2985, 0.166, 1.127, 1.238, 1.068, 0.1805, 0.8915, 0.635,
1.631, 0.2165, 0.252, 0.1825, 0.304, 0.1775, 1.005, 1.207, 0.6085,
0.1765, 0.661, 0.4775, 1.431, 0.1835, 0.3365, 1.236, 0.324, 0.1585,
1.425, 1.6555, 0.646, 0.2, 0.596, 0.267, 0.175, 0.272, 0.308,
0.167, 0.167, 3.0555, 0.3, 0.1745, 0.3005, 0.167, 0.361, 0.267,
0.183, 0.246, 0.166, 0.1925, 0.168, 2.668, 0.3315, 0.15, 0.167,
0.2835, 0.367, 0.5105, 0.192, 0.15, 0.4185, 0.25, 0.215, 3.036,
0.3085, 0.15, 0.254), Hand2 = c(1.4485, 2.3315, 1.8555, 2.788,
0.9965, 1.133, 2.817, 3.1015, 1.3025, 2.401, 1.45, 1.5335, 1.25,
1.483, 1.5815, 2.879, 1.0765, 1.198, 2.475, 3.027, 1.308, 1.69,
1.6245, 1.609, 1.771, 2.406, 2.3945, 2.574, 1.0995, 1.274, 3.189,
3.2325, 1.6815, 2.112, 1.992, 2.074, 1.555, 2.196, 1.476, 1.319,
2.3245, 1.513, 2.13, 1.533, 4.0535, 1.393, 1.404, 2.479, 1.2575,
1.627, 1.627, 1.2035, 1.972, 1.627, 2.0135, 1.723, 3.549, 1.3065,
1.509, 1.896, 1.337, 1.9335, 1.7195, 1.614, 1.8595, 2.093, 2.756,
1.784, 4.445, 1.954, 1.7855, 2.418, 1.73, 1.301, 1.437, 2.0635,
0.85, 0.958, 1.903, 1.7835, 1.0625, 1.831, 1.581, 1.569, 1.479,
1.427, 1.166, 2.1075, 0.812, 0.9445, 2.0145, 1.621, 0.954, 1.461,
1.742, 1.6455, 1.74, 1.35, 1.365, 2.157, 0.786, 1.0795, 2.419,
1.695, 0.979, 2.011, 2.128, 1.893, 1.2055, 1.6185, 0.999, 1.453,
1.1085, 1.151, 1.3145, 1.1525, 3.35, 0.95, 1.0535, 1.376, 1.0295,
1.237, 1.209, 1.0765, 0.99, 1.368, 1.0525, 1.293, 2.899, 0.8785,
0.892, 0.946, 1.236, 2.03, 1.279, 1.583, 1.4125, 1.5555, 1.245,
1.2275, 2.8515, 1.0295, 1.132, 1.2335), Eye3 = c(0.329, 2.3125,
1.294, 2.554, 0.3085, 0.7555, 1.913, 2.336, 0.869, 1.6545, 1.45,
1.2595, 0.3625, 1.811, 1.304, 2.656, 0.292, 0.5305, 1.537, 1.947,
0.653, 1.424, 1.3655, 0.619, 1.08, 2.337, 1.0105, 1.947, 0.5005,
0.7905, 2.626, 2.331, 0.862, 2.008, 1.554, 1.7715, 0.453, 0.666,
0.422, 0.284, 0.753, 0.6645, 0.467, 0.767, 4.0125, 0.4515, 0.439,
2.6085, 0.596, 0.5, 0.433, 0.3085, 0.35, 0.55, 0.4665, 0.785,
3.2945, 0.41, 0.584, 0.516, 0.533, 1.1575, 0.4665, 0.3, 0.842,
1.575, 2.3285, 1.03, 5.045, 0.9645, 0.999, 1.845, 0.408, 0.6785,
0.5, 1.588, 0.2665, 0.425, 1.24, 0.5605, 0.2855, 1.2575, 1.356,
1.4355, 0.31, 1.075, 0.901, 1.763, 0.3, 0.46, 1.11, 0.475, 0.3,
1.1245, 1.374, 0.9005, 0.3015, 0.911, 0.7605, 1.584, 0.3085,
0.573, 1.515, 0.541, 0.2505, 1.68, 1.8555, 1.165, 0.341, 0.7125,
0.4505, 0.297, 0.422, 0.3835, 0.267, 0.334, 3.164, 0.384, 0.342,
0.4165, 0.35, 0.494, 0.466, 0.275, 0.379, 0.317, 0.275, 0.45,
2.818, 0.4465, 0.3, 0.3, 0.3795, 0.533, 0.6145, 0.276, 0.3825,
0.5685, 0.35, 0.3985, 3.1365, 0.4335, 0.267, 0.359), Hand3 = c(1.8075,
2.896, 2.222, 3.056, 1.23, 1.483, 3.3425, 3.4345, 1.5895, 2.801,
1.965, 1.911, 1.658, 2.3415, 1.8805, 3.301, 1.2935, 1.588, 3.147,
3.461, 1.508, 2.107, 2.041, 2.026, 2.096, 2.915, 2.8, 2.907,
1.335, 1.7655, 3.894, 3.7165, 1.9735, 2.654, 2.309, 2.541, 1.947,
2.596, 1.8, 1.636, 2.5915, 1.763, 2.48, 1.955, 4.6585, 1.7415,
1.6775, 3.199, 1.628, 1.994, 1.998, 1.5895, 2.301, 1.903, 2.4555,
2.1685, 3.928, 1.6565, 1.743, 2.296, 1.57, 2.3835, 1.9995, 1.984,
2.693, 2.418, 3.2325, 2.348, 5.045, 2.304, 2.136, 3.051, 2.0245,
1.5885, 1.687, 2.3985, 1.1, 1.381, 2.413, 2.1765, 1.335, 2.184,
2.014, 2.2635, 1.743, 1.7055, 1.466, 2.54, 1.0605, 1.255, 2.588,
2.021, 1.237, 1.871, 2.074, 2.179, 2.002, 1.752, 1.665, 2.612,
1.04, 1.4045, 2.892, 2.1105, 1.254, 2.3935, 2.748, 2.4005, 1.522,
1.976, 1.363, 1.7445, 1.392, 1.476, 1.6235, 1.414, 3.6665, 1.242,
1.362, 1.718, 1.413, 1.704, 1.542, 1.3845, 1.2895, 1.585, 1.394,
1.588, 3.203, 1.102, 1.225, 1.28, 1.5285, 2.297, 1.532, 1.883,
1.679, 1.822, 1.645, 1.512, 3.245, 1.338, 1.465, 1.5585)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -144L), spec = structure(list(
cols = list(Group = 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")), skip = 1), class = "col_spec"))
``````

I used the larger data set you posted and got the following:

``````library(dplyr)
library(tidyr)
library(ggplot2)
dataLong <- data10 |> pivot_longer(cols = -Group,
names_to=c(".value","Index"),
names_pattern = "(Eye|Hand)(\\d)") |>
mutate(Group=paste0("Grp_",Group))
dataLong
# A tibble: 432 x 4
Group Index   Eye  Hand
<chr> <chr> <dbl> <dbl>
1 Grp_1 1     0.162 0.369
2 Grp_1 2     0.152 1.45
3 Grp_1 3     0.329 1.81
4 Grp_1 1     0.614 0.75
5 Grp_1 2     1.40  2.33
6 Grp_1 3     2.31  2.90
7 Grp_1 1     0.134 0.35
8 Grp_1 2     1.11  1.86
9 Grp_1 3     1.29  2.22
10 Grp_1 1     0.083 0.35
# ... with 422 more rows

Cors <- dataLong |> group_by(Group,Index) |> summarize(Cor=round(cor(Eye,Hand),3))
`summarise()` has grouped output by 'Group'. You can override using the `.groups` argument.
Cors
# A tibble: 6 x 3
# Groups:   Group [2]
Group Index   Cor
<chr> <chr> <dbl>
1 Grp_1 1     0.554
2 Grp_1 2     0.836
3 Grp_1 3     0.876
4 Grp_2 1     0.396
5 Grp_2 2     0.917
6 Grp_2 3     0.919
``````

I find it strange I am using exactly the same code and still getting just one value for Cors
Cors
Cor
1 0.843

Please show the actual show the actual code you are running. The best way to do that is with a Reproducible Example (reprex). Here is a tutorial on how to make one.

1 Like

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)

The code you posted worked for me after I fixed the second part of the names_pattern. It is \d in what you posted but it should be \\d. I assume that was caused by the forum software and your R code is correct.
All I can suggest is that you close and restart RStudio, load just the libraries needed for this code and try again.

2 Likes

The code works on the cloud but not on my desktop r studio

If the failure is that the Cors data frame only contains a single value when you run the code locally, that suggests that the group_by() function is not working. I have no idea why that would be. Does this code work on your local system?

``````library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#>     filter, lag
#> The following objects are masked from 'package:base':
#>
#>     intersect, setdiff, setequal, union
mtcars |> group_by(carb) |> summarize(avg = mean(mpg))
#> # A tibble: 6 x 2
#>    carb   avg
#>   <dbl> <dbl>
#> 1     1  25.3
#> 2     2  22.4
#> 3     3  16.3
#> 4     4  15.8
#> 5     6  19.7
#> 6     8  15
``````

Created on 2022-02-17 by the reprex package (v2.0.1)

1 Like

It is working now. I think the problem were some packages masking others

1 Like

This topic was automatically closed 7 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.