Home > OS >  change scales of specific facets in ggplots to avoid clutter
change scales of specific facets in ggplots to avoid clutter

Time:02-23

I want to manually change the scales of some specific facets in ggplot. I tried setting scales and space = "free" but I am not getting the desired behavior. I also want to make the graph appear bigger, I realize when I use "theme(aspect.ratio = 5/5)" it shrinks the plot

I want to manually change the scales of some specific facets in ggplot. I tried setting scales and space = "free" but I am not getting the desired behavior. I also want to make the graph appear bigger, I realize when I use "theme(aspect.ratio = 5/5)" it shrinks the plot

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

Index.labs <- c("RT (s)", "MT (s)", "RSP (s)")
names(Index.labs) <- c("1", "2", "3")

Group.labs <- c("Visible", "Remembered")
names(Group.labs) <- c("Grp_1", "Grp_2")

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, labeller = labeller(Index = Index.labs, Group = Group.labs)) 
  geom_text(aes(x=1.5,y=3,label=paste("r=",Cor)),size = 4.5,data=Cors)   theme_bw()   theme(
    axis.text.x = element_text(size = 12,face="bold"),
    axis.text.y = element_text(size = 12, face = "bold"),
    axis.title.y = element_text(vjust= 1.8, size = 20),
    axis.title.x = element_text(vjust= -0.5, size = 18),
    axis.title = element_text(face = "bold"))                                                                  # Change font size
  theme(strip.text.x = element_text(size = 12, face = "bold"))  
  theme(strip.text.y = element_text(size = 12, face = "bold"))   
  theme(aspect.ratio = 5/5)   geom_abline(intercept =0 , slope = 1)

CodePudding user response:

There are at least two complicating factors here.

  1. With facet_grid, scales = "free" is somewhat limited: plots in the same column will still all have the same x axis, and plots in the same row will still all have the same y axis. One solution would be to use facet_wrap, which allows all scales to be independent from one another.

  2. Setting geom_text to x=1.5,y=3 means that the axes will always extend at least this far, even if the range of the data is smaller -- e.g. with your RT data, where the max y value is ~0.6. A solution is to set the label position for each facet in your data, as a proportion of the range for that facet.

library(tidyverse)

Index.labs <- c("RT (s)", "MT (s)", "RSP (s)")
names(Index.labs) <- c("1", "2", "3")

Group.labs <- c("Visible", "Remembered")
names(Group.labs) <- c("Grp_1", "Grp_2")

Cors <- dataLong %>% 
  group_by(Group, Index) %>% 
  summarize(
    text_x = max(Hand) * .1,    # plot text at 10% of x axis
    text_y = max(Eye) * .9,     # & 90% of y axis
    Cor=round(cor(Eye, Hand), 3)
  )

ggplot(dataLong, aes(x = Hand, y = Eye))  
  geom_point()  
  facet_wrap(                   # facet_wrap instead of facet_grid
    vars(Index, Group), 
    ncol = 2,
    scales = "free",
    labeller = labeller(Index = Index.labs, Group = Group.labs)
  ) 
  geom_text(
    aes(x = text_x, y = text_y, label = paste("r =", Cor)), # replace 1.5 & 3 w/ 
    size = 4.5,                                             # text_x & text_y
    hjust = 0,
    data = Cors
  )   
  theme_bw()   
  theme(
    axis.text = element_text(size = 12, face = "bold"),
    axis.title.y = element_text(vjust= 1.8, size = 20),
    axis.title.x = element_text(vjust= -0.5, size = 18),
    axis.title = element_text(face = "bold"),
    strip.text = element_text(size = 12, face = "bold"),
    aspect.ratio = .8     # adjust aspect ratio to allow a bit more width
  )   
  geom_abline()      # intercept=0, slope=1 are defaults, don't need to specify

You might want even finer-grained control -- e.g., it might be nice to set the axis limits equal within each Index but let them vary across indices. At this point, it's easier to make multiple plots with different specs, then join them together using cowplot or patchwork.

CodePudding user response:

Here's an alternative approach, using cowplot to combine multiple plots. This can take a bit more effort, but gives you greater control. In this example, the final plot has the same axis ranges within each level of Index, but lets these vary between Indexes.

library(tidyverse)
library(cowplot)

# since we'll have to generate three plots, we'll put our `ggplot` spec 
# into a function
plot_row <- function(dataLong, Cors) {
  text_x <- max(dataLong$Hand) * .1    # set text position as proportion of 
  text_y <- max(dataLong$Eye) * .9     # subplot range
  p <- ggplot(dataLong, aes(x = Hand, y = Eye))  
    geom_point()  
    facet_grid(
      Index ~ Group, 
      labeller = labeller(Index = Index.labs, Group = Group.labs)
    )  
    geom_text(
      aes(x = text_x, y = text_y, label = paste("r =", Cor)),
      size = 4.5,
      hjust = 0,
      data = Cors
    )   
    coord_fixed(xlim = c(0, NA), ylim = c(0, NA))  
    theme_bw()   
    theme(
      axis.text = element_text(size = 12, face = "bold"),
      axis.title = element_blank(),
      strip.text.y = element_text(size = 12, face = "bold"),
      plot.margin = margin(0,0,0,0)
    )   
    geom_abline()
  # only include column facet labels for the top row
  if ("3" %in% dataLong$Index) {
    p   theme(strip.text.x = element_text(size = 12, face = "bold"))
  } else {
    p   theme(strip.text.x = element_blank())
  }
}

# unchanged from original code
Index.labs <- c("RT (s)", "MT (s)", "RSP (s)")
names(Index.labs) <- c("1", "2", "3")

Group.labs <- c("Visible", "Remembered")
names(Group.labs) <- c("Grp_1", "Grp_2")

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

# split data and Cors into three separate dfs each -- one for each level of
# Index -- and pass them to the plotting function
dataLongList <- split(dataLong, dataLong$Index)
CorsList <- split(Cors, Cors$Index)
plot_rows <- map2(dataLongList, CorsList, plot_row)

# use cowplot::plot_grid to assemble the plots, and cowplot::add_sub to 
# add axis labels
p <- plot_grid(plotlist = rev(plot_rows), ncol = 1, align = "vh") %>% 
  add_sub("Hand", fontface = "bold", size = 18) %>% 
  add_sub("Eye", fontface = "bold", size = 18, x = .1, y = 6.5, angle = 90)

# NB, placing the axis labels using `add_sub` is the most finicky part -- the 
# right values of x, y, hjust, and vjust will depend on your plot dimensions and
# margins, and often take trial and error to figure out.
  • Related