I am trying to calculate Pearson correlation between two variables using cor.test
in R and also represent the data using ggplot2
. While plotting the data I am using stat_cor
to add the correlation coefficient and p-value to the plot. I noticed that the p-values calculated by the two methods don't match. I tried changing the exact
parameter in cor.test
to TRUE/FALSE
as suggested here but it doesn't make any difference.
Here's my data:
data <- data.frame(A = c(7.2522, 7.8023, 7.4741, 8.1916, 7.7342,
7.9694, 9.2437, 8.7419, 7.9288, 6.1581, 8.9725, 6.6203, 8.392,
7.3426, 8.3315, 7.9668, 7.7307, 6.4736, 6.4173, 8.4583, 6.318,
8.0141, 7.1282, 7.6728, 6.6996, 7.2628, 7.8479, 7.2758, 7.6528,
7.8376, 7.6133, 8.059, 7.7067, 7.3983, 8.0433, 7.4078, 7.4004,
7.3723, 7.7947, 6.7346, 7.9064, 6.4544, 7.6623, 6.6703, 7.1375,
7.2981, 6.603, 6.7018, 7.2705, 7.8492, 7.4567, 6.9586, 7.0147,
6.5972, 8.0107, 6.4509, 8.3036, 8.5086, 8.4867, 7.1364, 7.7103,
8.4274, 7.8392, 7.6576, 7.0692, 7.3194, 7.3259, 8.1677, 6.5563,
7.2733, 7.8052, 7.2525, 7.3327, 7.6071, 8.0825, 8.1794, 7.7919,
7.2662, 7.4849, 7.5567, 7.138, 6.7526, 6.9958, 6.4038, 7.6324,
7.9362, 7.3181, 8.5966, 8.1202, 7.3034, 7.6495, 8.2328, 7.1565,
7.0588, 6.7469, 7.6978, 8.0019, 7.8915, 6.562, 6.431, 7.0615,
6.4301, 7.7736, 7.7938, 6.9058, 6.884, 7.1529, 7.3742, 7.955,
8.124, 7.7467, 6.6889, 7.7339, 7.3047, 7.468, 6.9155, 6.9024,
7.322, 5.582, 6.3142, 7.3418, 7.0034, 5.2863, 8.1416, 7.2394,
6.6843, 7.2772, 7.5488, 7.6269, 7.4939, 8.316, 8.2104, 6.7741,
7.1528, 7.3059, 7.088, 7.4578, 6.84, 7.4417, 7.7396, 7.5473,
6.7726, 7.5082, 7.4607, 7.2199, 7.8167, 6.6827, 7.3508, 7.5839,
7.4206, 6.9973, 6.0804, 5.9254, 7.9614, 7.089, 7.9938, 5.7987,
6.7792, 7.7962, 6.9315, 7.7676, 7.3525, 7.2498, 7.6716, 7.8441,
8.1848, 6.7142, 7.3317, 6.5885, 7.6079, 5.8022, 8.2209, 7.2613,
6.4929, 7.4398, 6.2312, 6.6842, 5.8285, 6.0335, 7.9888, 6.919,
6.84, 6.7942, 7.2229, 7.859, 6.643, 7.5612, 7.2445, 8.1893, 9.4298,
8.0789, 7.6878, 8.1157, 7.0334, 7.7589, 7.7174, 7.9144, 8.3985,
6.8021, 7.5551, 7.5816, 7.8185, 7.5726, 6.2718, 7.6299, 8.407,
8.468, 7.0832, 7.4891, 6.6967, 7.5969, 7.5611, 7.8248, 7.1217,
7.1926, 7.6722, 7.2303, 7.2931, 7.7666, 7.5655, 7.9799, 6.6139,
7.7393, 7.0227, 6.7773, 7.4542, 7.328, 7.5798, 7.0534, 6.6719,
7.706, 7.3695, 8.2583, 7.1586, 7.3594, 7.0566, 7.0591, 6.6744,
7.2765, 7.2147, 6.6491, 7.321, 7.1371, 7.5431, 7.6534, 6.3426,
8.0088, 7.3745, 8.2489, 6.2066, 6.3795, 7.5311, 7.4697, 7.316,
7.1668, 7.4657, 7.4575, 7.9665, 7.377, 6.4387, 6.6981, 7.5718,
7.7394, 7.5432, 7.2023, 7.1213, 7.0216, 7.2533, 8.5051, 7.1244,
5.9056, 6.4507, 7.0661, 6.8893, 7.0724, 6.3441, 6.2493, 6.6688,
7.6975, 6.4579, 6.4474, 7.4216, 6.7952, 6.8721, 7.6909, 6.9794,
7.2268, 7.1399, 7.648, 6.8031, 7.3428, 6.7601, 7.221, 6.5459,
6.5929, 7.8426, 7.4419, 7.2834, 6.4232, 7.603, 7.5984, 7.3467,
7.1805, 6.8795, 7.5939, 7.8312, 7.5377, 6.9176, 7.6245, 6.9563,
8.3461, 7.9613, 8.2359, 6.8757, 7.7049, 7.0913, 7.7383, 7.731,
6.6122, 7.2611, 6.709, 6.2961, 7.0936, 7.6514, 7.2314, 7.8052,
7.4067, 7.5492, 7.0291, 6.5189, 7.3176, 7.594, 7.4425, 7.7646,
7.661, 7.2465, 7.3388, 7.7331, 7.6216, 6.9283, 7.7867, 7.2409,
7.7583, 8.6002, 6.6402, 7.8067, 6.3818, 5.8414, 7.8545, 7.2889,
8.4528, 6.71, 6.8817, 6.8813, 6.8604, 7.0804, 8.0733, 6.9651,
7.3233, 8.6305, 7.0151, 7.9017, 7.3789, 7.3645, 7.5411, 7.7931,
7.3547, 7.27, 9.2145, 6.1515, 6.7585, 7.187, 6.4897, 7.8047,
7.2922, 7.3048, 7.795, 6.1363, 7.4396, 6.5582, 8.2793, 7.9183,
6.8788, 6.9511, 7.223, 7.3067, 7.3435, 7.1842, 7.8756, 7.5051,
7.5234, 7.8242, 7.2051, 8.0899, 6.736, 7.4785, 8.1954, 6.3797,
7.5678, 7.0623, 7.8564, 7.3025, 8.3457, 7.3207, 7.5724, 7.6164,
7.2052, 7.4789, 7.3641, 8.0805, 7.6767, 7.6576, 8.0185, 8.2083,
7.3076, 7.5717, 7.4735, 7.1779, 7.1149, 6.7887, 7.573, 7.3457,
8.166, 7.6059, 8.1016, 7.2192, 7.6511, 6.9305, 8.6951, 8.029,
7.7611, 6.9953, 7.1924, 7.0434, 6.0832, 8.0633, 7.4061, 7.8128,
6.5658, 7.2694, 7.7292, 7.8167, 8.3828, 7.4979, 6.5286, 7.5047,
6.589, 8.182, 7.5683, 7.1254, 7.5994, 7.3301, 6.8924, 7.8442,
8.1813, 8.425, 7.1243, 7.3724, 7.1877, 7.3758, 5.788, 6.8793,
6.5718, 7.2873, 6.7609, 5.9397, 8.6219, 7.8157, 8.2423, 8.819,
7.3811, 8.8174, 8.9046, 9.6859, 7.5542, 7.025, 8.3208, 7.4823,
7.7475, 7.8205, 7.1424, 7.5538, 7.3154, 6.5849, 6.9849, 7.4723,
7.576, 7.6117, 7.9603, 7.5923, 6.8219, 6.9969, 7.7362, 8.0479,
7.5154, 7.6614, 8.2747, 6.4431, 7.4613, 7.2575, 7.9111, 7.2848,
7.3871, 7.5717, 7.9613, 6.8965, 6.1998, 7.0426, 7.029, 7.5013,
6.5618, 8.5664, 7.8674, 7.4132, 6.6375, 7.6376, 6.4609, 7.3881,
8.0213, 7.271, 7.5777, 7.3261, 7.3792, 7.5643, 7.257, 7.579,
7.8608, 7.4433),
B = c(4.1531, 4.473, 4.0868, 7.019, 5.512, 6.3378,
7.3137, 5.5535, 5.3793, 4.2856, 7.2195, 4.1629, 6.421, 5.5921,
5.8306, 3.9775, 3.7057, 4.3848, 3.5933, 5.914, 3.9559, 4.6959,
4.658, 4.5399, 4.2504, 4.5165, 6.8185, 5.0541, 6.3259, 4.711,
4.7948, 4.3064, 4.0461, 4.5342, 4.4459, 4.2089, 4.9137, 4.3166,
4.5833, 4.6266, 3.7524, 5.0786, 3.8449, 4.2093, 4.5037, 4.1481,
3.8874, 4.7858, 4.372, 7.1941, 6.061, 3.9275, 4.0217, 4.3872,
4.4956, 3.7176, 6.1726, 4.3929, 4.6174, 4.9405, 4.6338, 5.7895,
4.6304, 4.3581, 4.1557, 4.1976, 4.2389, 4.7816, 3.8497, 4.2347,
3.9537, 5.3007, 4.6347, 4.6802, 4.56, 4.6006, 6.1878, 4.7903,
4.8226, 3.9921, 4.1353, 4.6995, 4.7458, 4.2452, 4.6175, 5.5851,
4.463, 5.5618, 4.9761, 4.1385, 4.3034, 4.8003, 4.5153, 4.7165,
4.2684, 3.8478, 6.1408, 5.2058, 4.0257, 4.4486, 4.0931, 4.8517,
4.1816, 3.9084, 4.2272, 4.0308, 4.1749, 4.5321, 4.2118, 4.3287,
4.5949, 3.8607, 4.3163, 4.8651, 5.4691, 4.942, 4.0849, 4.5952,
3.7627, 4.07, 3.8262, 4.4759, 4.0808, 6.2553, 4.5452, 4.5787,
4.638, 4.9173, 5.3909, 5.5766, 4.9781, 5.281, 3.796, 4.1087,
4.0798, 4.6196, 5.0838, 5.1086, 4.8097, 4.8642, 4.7388, 4.4419,
5.516, 4.004, 4.2272, 4.1642, 4.2766, 4.658, 4.9016, 5.1107,
4.4812, 4.7916, 5.316, 4.3096, 4.5413, 4.1618, 3.5831, 4.5853,
5.0735, 5.1118, 5.0023, 4.0226, 5.5281, 4.8239, 4.9209, 4.3086,
4.9147, 4.4973, 4.9311, 4.2116, 4.1961, 4.2031, 3.7416, 4.7418,
4.4523, 4.8948, 4.9159, 3.7951, 3.8687, 4.9886, 4.4293, 4.5493,
5.8463, 5.0328, 4.069, 3.8202, 4.771, 6.5082, 4.4151, 9.3598,
4.8401, 4.5967, 5.0077, 4.4491, 3.8378, 5.75, 4.3742, 5.0248,
4.1096, 4.7791, 4.5152, 4.9796, 4.2216, 3.5643, 6.1857, 4.7081,
4.3092, 4.7716, 3.792, 4.5419, 4.2147, 4.667, 4.5985, 4.5638,
5.3071, 4.0176, 4.3484, 4.6765, 4.5196, 5.7972, 3.8144, 4.0308,
4.1219, 4.8298, 4.2688, 3.969, 3.6499, 4.4563, 4.3834, 4.7537,
4.4135, 4.9686, 4.5407, 3.6465, 4.6515, 4.9418, 5.1322, 4.0716,
4.3139, 4.3717, 4.4364, 4.6193, 4.1596, 3.6182, 4.8053, 4.0661,
4.4509, 5.5379, 6.6676, 4.3699, 4.6352, 4.0169, 4.4435, 4.5853,
4.7659, 4.2801, 4.0129, 5.1164, 4.3666, 3.9635, 3.7079, 4.4013,
4.2831, 5.5454, 4.6384, 4.2392, 5.0026, 4.772, 4.9849, 4.2099,
5.2645, 4.282, 3.7575, 4.9239, 5.0565, 5.2606, 4.5413, 4.3794,
4.9442, 4.8502, 3.9911, 4.7226, 3.9206, 5.2833, 3.9008, 4.2133,
4.8546, 5.5596, 4.8249, 4.6887, 4.4949, 4.8237, 3.9534, 4.7268,
5.1498, 4.8719, 5.8118, 4.8643, 4.2022, 4.5727, 5.1051, 4.9264,
4.2292, 4.2158, 4.4555, 5.3254, 4.9455, 4.0878, 4.8803, 4.5054,
5.2805, 6.2107, 5.5979, 4.1935, 5.8767, 5.0418, 4.7015, 5.8639,
4.6239, 4.5248, 4.5226, 4.6365, 4.0215, 5.3803, 4.7874, 5.5076,
3.9888, 4.7067, 4.4915, 4.7789, 5.3626, 4.8719, 4.8392, 4.5378,
4.6962, 4.8514, 4.1571, 4.4177, 4.7204, 4.8253, 4.8181, 4.6064,
5.6122, 5.2701, 4.5037, 3.8618, 3.8559, 4.5169, 5.7317, 4.3055,
4.3507, 3.9255, 4.7639, 3.9088, 4.902, 4.3161, 4.1922, 5.077,
5.3417, 5.5658, 4.5431, 4.707, 4.4483, 5.1323, 5.6301, 4.4906,
4.9159, 3.8978, 4.7495, 4.7077, 4.1926, 4.7848, 4.249, 6.4578,
5.5694, 5.3648, 5.1605, 4.4243, 4.3347, 4.2386, 4.9246, 4.2992,
4.607, 4.5907, 4.7774, 4.4029, 4.8533, 4.2621, 4.2758, 5.2957,
5.9921, 4.301, 4.3313, 7.4858, 3.8896, 5.1341, 3.8771, 5.0641,
5.1623, 3.817, 4.6117, 4.4738, 6.0756, 4.5535, 5.0383, 4.2653,
4.4974, 5.0476, 4.7451, 4.5134, 4.4736, 5.0842, 4.7651, 4.6712,
4.146, 4.6541, 5.4357, 4.3218, 4.5683, 4.8845, 4.0452, 4.1599,
4.7943, 6.3047, 4.0191, 4.9948, 4.3568, 7.1291, 5.4856, 4.3794,
5.507, 5.3809, 5.062, 4.1565, 4.6244, 4.078, 4.7441, 5.3483,
4.2923, 4.5232, 5.426, 5.4448, 6.3514, 4.7087, 3.8357, 5.0651,
4.4781, 4.6767, 4.7547, 4.6188, 4.4234, 4.4113, 4.5579, 5.4852,
4.5013, 3.6953, 4.2295, 4.9703, 3.7676, 4.6445, 3.7959, 5.9265,
4.2295, 4.1308, 4.2517, 3.735, 8.549, 5.5903, 6.5792, 7.8535,
6.0347, 5.6661, 5.9168, 5.5812, 4.5546, 4.4168, 4.914, 4.6495,
4.6372, 5.9212, 4.4251, 5.0813, 4.5992, 4.1543, 4.6469, 4.4101,
5.348, 4.5765, 5.6937, 4.9091, 4.4577, 6.0764, 5.9279, 4.8404,
5.5468, 4.9039, 4.2217, 4.2115, 5.6648, 4.0095, 4.2602, 4.3607,
5.16, 4.3684, 5.0163, 4.4507, 4.8864, 4.3162, 4.1436, 3.8862,
3.5108, 5.3297, 4.2063, 4.339, 4.0927, 4.6954, 4.2145, 5.836,
4.194, 5.1575, 4.8366, 5.5887, 4.3689, 4.3174, 4.3054, 4.7569,
4.2927, 5.4407))
and the code:
# Correlation coefficient and p-value using cor.test
corr <- c(Correlation = cor.test(x = data$A,
y = data$B,
method = "pearson")[4]$estimate,
pvalue = cor.test(x = data$A,
y = data$B,
method = "pearson")[3]$p.value)
> corr
Correlation.cor pvalue
4.136836e-01 3.034155e-23
# Plot
library(ggplot2)
library(ggpubr)
formula <- y ~ x
ggplot(data = data,
aes(x = A,
y = B)) +
geom_point(alpha = 0.7) +
geom_smooth(method = "lm",
formula = formula,
se = FALSE) +
stat_cor(method = "pearson",
show.legend = FALSE,
cor.coef.name = "r")
Here are two options for manually printing the p-value from cor.test using annotate()
.
data <- data.frame(A = c(7.2522, 7.8023, 7.4741, 8.1916, 7.7342,
7.9694, 9.2437, 8.7419, 7.9288, 6.1581, 8.9725, 6.6203, 8.392,
7.3426, 8.3315, 7.9668, 7.7307, 6.4736, 6.4173, 8.4583, 6.318,
8.0141, 7.1282, 7.6728, 6.6996, 7.2628, 7.8479, 7.2758, 7.6528,
7.8376, 7.6133, 8.059, 7.7067, 7.3983, 8.0433, 7.4078, 7.4004,
7.3723, 7.7947, 6.7346, 7.9064, 6.4544, 7.6623, 6.6703, 7.1375,
7.2981, 6.603, 6.7018, 7.2705, 7.8492, 7.4567, 6.9586, 7.0147,
6.5972, 8.0107, 6.4509, 8.3036, 8.5086, 8.4867, 7.1364, 7.7103,
8.4274, 7.8392, 7.6576, 7.0692, 7.3194, 7.3259, 8.1677, 6.5563,
7.2733, 7.8052, 7.2525, 7.3327, 7.6071, 8.0825, 8.1794, 7.7919,
7.2662, 7.4849, 7.5567, 7.138, 6.7526, 6.9958, 6.4038, 7.6324,
7.9362, 7.3181, 8.5966, 8.1202, 7.3034, 7.6495, 8.2328, 7.1565,
7.0588, 6.7469, 7.6978, 8.0019, 7.8915, 6.562, 6.431, 7.0615,
6.4301, 7.7736, 7.7938, 6.9058, 6.884, 7.1529, 7.3742, 7.955,
8.124, 7.7467, 6.6889, 7.7339, 7.3047, 7.468, 6.9155, 6.9024,
7.322, 5.582, 6.3142, 7.3418, 7.0034, 5.2863, 8.1416, 7.2394,
6.6843, 7.2772, 7.5488, 7.6269, 7.4939, 8.316, 8.2104, 6.7741,
7.1528, 7.3059, 7.088, 7.4578, 6.84, 7.4417, 7.7396, 7.5473,
6.7726, 7.5082, 7.4607, 7.2199, 7.8167, 6.6827, 7.3508, 7.5839,
7.4206, 6.9973, 6.0804, 5.9254, 7.9614, 7.089, 7.9938, 5.7987,
6.7792, 7.7962, 6.9315, 7.7676, 7.3525, 7.2498, 7.6716, 7.8441,
8.1848, 6.7142, 7.3317, 6.5885, 7.6079, 5.8022, 8.2209, 7.2613,
6.4929, 7.4398, 6.2312, 6.6842, 5.8285, 6.0335, 7.9888, 6.919,
6.84, 6.7942, 7.2229, 7.859, 6.643, 7.5612, 7.2445, 8.1893, 9.4298,
8.0789, 7.6878, 8.1157, 7.0334, 7.7589, 7.7174, 7.9144, 8.3985,
6.8021, 7.5551, 7.5816, 7.8185, 7.5726, 6.2718, 7.6299, 8.407,
8.468, 7.0832, 7.4891, 6.6967, 7.5969, 7.5611, 7.8248, 7.1217,
7.1926, 7.6722, 7.2303, 7.2931, 7.7666, 7.5655, 7.9799, 6.6139,
7.7393, 7.0227, 6.7773, 7.4542, 7.328, 7.5798, 7.0534, 6.6719,
7.706, 7.3695, 8.2583, 7.1586, 7.3594, 7.0566, 7.0591, 6.6744,
7.2765, 7.2147, 6.6491, 7.321, 7.1371, 7.5431, 7.6534, 6.3426,
8.0088, 7.3745, 8.2489, 6.2066, 6.3795, 7.5311, 7.4697, 7.316,
7.1668, 7.4657, 7.4575, 7.9665, 7.377, 6.4387, 6.6981, 7.5718,
7.7394, 7.5432, 7.2023, 7.1213, 7.0216, 7.2533, 8.5051, 7.1244,
5.9056, 6.4507, 7.0661, 6.8893, 7.0724, 6.3441, 6.2493, 6.6688,
7.6975, 6.4579, 6.4474, 7.4216, 6.7952, 6.8721, 7.6909, 6.9794,
7.2268, 7.1399, 7.648, 6.8031, 7.3428, 6.7601, 7.221, 6.5459,
6.5929, 7.8426, 7.4419, 7.2834, 6.4232, 7.603, 7.5984, 7.3467,
7.1805, 6.8795, 7.5939, 7.8312, 7.5377, 6.9176, 7.6245, 6.9563,
8.3461, 7.9613, 8.2359, 6.8757, 7.7049, 7.0913, 7.7383, 7.731,
6.6122, 7.2611, 6.709, 6.2961, 7.0936, 7.6514, 7.2314, 7.8052,
7.4067, 7.5492, 7.0291, 6.5189, 7.3176, 7.594, 7.4425, 7.7646,
7.661, 7.2465, 7.3388, 7.7331, 7.6216, 6.9283, 7.7867, 7.2409,
7.7583, 8.6002, 6.6402, 7.8067, 6.3818, 5.8414, 7.8545, 7.2889,
8.4528, 6.71, 6.8817, 6.8813, 6.8604, 7.0804, 8.0733, 6.9651,
7.3233, 8.6305, 7.0151, 7.9017, 7.3789, 7.3645, 7.5411, 7.7931,
7.3547, 7.27, 9.2145, 6.1515, 6.7585, 7.187, 6.4897, 7.8047,
7.2922, 7.3048, 7.795, 6.1363, 7.4396, 6.5582, 8.2793, 7.9183,
6.8788, 6.9511, 7.223, 7.3067, 7.3435, 7.1842, 7.8756, 7.5051,
7.5234, 7.8242, 7.2051, 8.0899, 6.736, 7.4785, 8.1954, 6.3797,
7.5678, 7.0623, 7.8564, 7.3025, 8.3457, 7.3207, 7.5724, 7.6164,
7.2052, 7.4789, 7.3641, 8.0805, 7.6767, 7.6576, 8.0185, 8.2083,
7.3076, 7.5717, 7.4735, 7.1779, 7.1149, 6.7887, 7.573, 7.3457,
8.166, 7.6059, 8.1016, 7.2192, 7.6511, 6.9305, 8.6951, 8.029,
7.7611, 6.9953, 7.1924, 7.0434, 6.0832, 8.0633, 7.4061, 7.8128,
6.5658, 7.2694, 7.7292, 7.8167, 8.3828, 7.4979, 6.5286, 7.5047,
6.589, 8.182, 7.5683, 7.1254, 7.5994, 7.3301, 6.8924, 7.8442,
8.1813, 8.425, 7.1243, 7.3724, 7.1877, 7.3758, 5.788, 6.8793,
6.5718, 7.2873, 6.7609, 5.9397, 8.6219, 7.8157, 8.2423, 8.819,
7.3811, 8.8174, 8.9046, 9.6859, 7.5542, 7.025, 8.3208, 7.4823,
7.7475, 7.8205, 7.1424, 7.5538, 7.3154, 6.5849, 6.9849, 7.4723,
7.576, 7.6117, 7.9603, 7.5923, 6.8219, 6.9969, 7.7362, 8.0479,
7.5154, 7.6614, 8.2747, 6.4431, 7.4613, 7.2575, 7.9111, 7.2848,
7.3871, 7.5717, 7.9613, 6.8965, 6.1998, 7.0426, 7.029, 7.5013,
6.5618, 8.5664, 7.8674, 7.4132, 6.6375, 7.6376, 6.4609, 7.3881,
8.0213, 7.271, 7.5777, 7.3261, 7.3792, 7.5643, 7.257, 7.579,
7.8608, 7.4433),
B = c(4.1531, 4.473, 4.0868, 7.019, 5.512, 6.3378,
7.3137, 5.5535, 5.3793, 4.2856, 7.2195, 4.1629, 6.421, 5.5921,
5.8306, 3.9775, 3.7057, 4.3848, 3.5933, 5.914, 3.9559, 4.6959,
4.658, 4.5399, 4.2504, 4.5165, 6.8185, 5.0541, 6.3259, 4.711,
4.7948, 4.3064, 4.0461, 4.5342, 4.4459, 4.2089, 4.9137, 4.3166,
4.5833, 4.6266, 3.7524, 5.0786, 3.8449, 4.2093, 4.5037, 4.1481,
3.8874, 4.7858, 4.372, 7.1941, 6.061, 3.9275, 4.0217, 4.3872,
4.4956, 3.7176, 6.1726, 4.3929, 4.6174, 4.9405, 4.6338, 5.7895,
4.6304, 4.3581, 4.1557, 4.1976, 4.2389, 4.7816, 3.8497, 4.2347,
3.9537, 5.3007, 4.6347, 4.6802, 4.56, 4.6006, 6.1878, 4.7903,
4.8226, 3.9921, 4.1353, 4.6995, 4.7458, 4.2452, 4.6175, 5.5851,
4.463, 5.5618, 4.9761, 4.1385, 4.3034, 4.8003, 4.5153, 4.7165,
4.2684, 3.8478, 6.1408, 5.2058, 4.0257, 4.4486, 4.0931, 4.8517,
4.1816, 3.9084, 4.2272, 4.0308, 4.1749, 4.5321, 4.2118, 4.3287,
4.5949, 3.8607, 4.3163, 4.8651, 5.4691, 4.942, 4.0849, 4.5952,
3.7627, 4.07, 3.8262, 4.4759, 4.0808, 6.2553, 4.5452, 4.5787,
4.638, 4.9173, 5.3909, 5.5766, 4.9781, 5.281, 3.796, 4.1087,
4.0798, 4.6196, 5.0838, 5.1086, 4.8097, 4.8642, 4.7388, 4.4419,
5.516, 4.004, 4.2272, 4.1642, 4.2766, 4.658, 4.9016, 5.1107,
4.4812, 4.7916, 5.316, 4.3096, 4.5413, 4.1618, 3.5831, 4.5853,
5.0735, 5.1118, 5.0023, 4.0226, 5.5281, 4.8239, 4.9209, 4.3086,
4.9147, 4.4973, 4.9311, 4.2116, 4.1961, 4.2031, 3.7416, 4.7418,
4.4523, 4.8948, 4.9159, 3.7951, 3.8687, 4.9886, 4.4293, 4.5493,
5.8463, 5.0328, 4.069, 3.8202, 4.771, 6.5082, 4.4151, 9.3598,
4.8401, 4.5967, 5.0077, 4.4491, 3.8378, 5.75, 4.3742, 5.0248,
4.1096, 4.7791, 4.5152, 4.9796, 4.2216, 3.5643, 6.1857, 4.7081,
4.3092, 4.7716, 3.792, 4.5419, 4.2147, 4.667, 4.5985, 4.5638,
5.3071, 4.0176, 4.3484, 4.6765, 4.5196, 5.7972, 3.8144, 4.0308,
4.1219, 4.8298, 4.2688, 3.969, 3.6499, 4.4563, 4.3834, 4.7537,
4.4135, 4.9686, 4.5407, 3.6465, 4.6515, 4.9418, 5.1322, 4.0716,
4.3139, 4.3717, 4.4364, 4.6193, 4.1596, 3.6182, 4.8053, 4.0661,
4.4509, 5.5379, 6.6676, 4.3699, 4.6352, 4.0169, 4.4435, 4.5853,
4.7659, 4.2801, 4.0129, 5.1164, 4.3666, 3.9635, 3.7079, 4.4013,
4.2831, 5.5454, 4.6384, 4.2392, 5.0026, 4.772, 4.9849, 4.2099,
5.2645, 4.282, 3.7575, 4.9239, 5.0565, 5.2606, 4.5413, 4.3794,
4.9442, 4.8502, 3.9911, 4.7226, 3.9206, 5.2833, 3.9008, 4.2133,
4.8546, 5.5596, 4.8249, 4.6887, 4.4949, 4.8237, 3.9534, 4.7268,
5.1498, 4.8719, 5.8118, 4.8643, 4.2022, 4.5727, 5.1051, 4.9264,
4.2292, 4.2158, 4.4555, 5.3254, 4.9455, 4.0878, 4.8803, 4.5054,
5.2805, 6.2107, 5.5979, 4.1935, 5.8767, 5.0418, 4.7015, 5.8639,
4.6239, 4.5248, 4.5226, 4.6365, 4.0215, 5.3803, 4.7874, 5.5076,
3.9888, 4.7067, 4.4915, 4.7789, 5.3626, 4.8719, 4.8392, 4.5378,
4.6962, 4.8514, 4.1571, 4.4177, 4.7204, 4.8253, 4.8181, 4.6064,
5.6122, 5.2701, 4.5037, 3.8618, 3.8559, 4.5169, 5.7317, 4.3055,
4.3507, 3.9255, 4.7639, 3.9088, 4.902, 4.3161, 4.1922, 5.077,
5.3417, 5.5658, 4.5431, 4.707, 4.4483, 5.1323, 5.6301, 4.4906,
4.9159, 3.8978, 4.7495, 4.7077, 4.1926, 4.7848, 4.249, 6.4578,
5.5694, 5.3648, 5.1605, 4.4243, 4.3347, 4.2386, 4.9246, 4.2992,
4.607, 4.5907, 4.7774, 4.4029, 4.8533, 4.2621, 4.2758, 5.2957,
5.9921, 4.301, 4.3313, 7.4858, 3.8896, 5.1341, 3.8771, 5.0641,
5.1623, 3.817, 4.6117, 4.4738, 6.0756, 4.5535, 5.0383, 4.2653,
4.4974, 5.0476, 4.7451, 4.5134, 4.4736, 5.0842, 4.7651, 4.6712,
4.146, 4.6541, 5.4357, 4.3218, 4.5683, 4.8845, 4.0452, 4.1599,
4.7943, 6.3047, 4.0191, 4.9948, 4.3568, 7.1291, 5.4856, 4.3794,
5.507, 5.3809, 5.062, 4.1565, 4.6244, 4.078, 4.7441, 5.3483,
4.2923, 4.5232, 5.426, 5.4448, 6.3514, 4.7087, 3.8357, 5.0651,
4.4781, 4.6767, 4.7547, 4.6188, 4.4234, 4.4113, 4.5579, 5.4852,
4.5013, 3.6953, 4.2295, 4.9703, 3.7676, 4.6445, 3.7959, 5.9265,
4.2295, 4.1308, 4.2517, 3.735, 8.549, 5.5903, 6.5792, 7.8535,
6.0347, 5.6661, 5.9168, 5.5812, 4.5546, 4.4168, 4.914, 4.6495,
4.6372, 5.9212, 4.4251, 5.0813, 4.5992, 4.1543, 4.6469, 4.4101,
5.348, 4.5765, 5.6937, 4.9091, 4.4577, 6.0764, 5.9279, 4.8404,
5.5468, 4.9039, 4.2217, 4.2115, 5.6648, 4.0095, 4.2602, 4.3607,
5.16, 4.3684, 5.0163, 4.4507, 4.8864, 4.3162, 4.1436, 3.8862,
3.5108, 5.3297, 4.2063, 4.339, 4.0927, 4.6954, 4.2145, 5.836,
4.194, 5.1575, 4.8366, 5.5887, 4.3689, 4.3174, 4.3054, 4.7569,
4.2927, 5.4407))
# Correlation coefficient and p-value using cor.test
corr <- cor.test(x = data$A,
y = data$B,
method = "pearson")
corr_fit <- round(corr$estimate,2)
corr_p <- corr$p.value
# Plot
library(ggplot2)
# Option 1
ggplot(data = data, aes(x = A, y = B)) +
geom_point(alpha = 0.7) +
geom_smooth(method = "lm", se = FALSE) +
annotate('text', x = 5, y = 9, label = as.expression(bquote(r~'='~.(corr_fit)~','~p~'='~.(corr_p))),hjust = 0)
#> `geom_smooth()` using formula = 'y ~ x'
#> Warning in is.na(x): is.na() applied to non-(list or vector) of type
#> 'expression'
# Option 2
ggplot(data = data, aes(x = A, y = B)) +
geom_point(alpha = 0.7) +
geom_smooth(method = "lm", se = FALSE) +
annotate('text', x = 5, y = 9, label = c('r = 0.41, p < 0.001'), hjust = 0)
#> `geom_smooth()` using formula = 'y ~ x'
Created on 2024-05-15 with reprex v2.1.0