i have a data frame with 4 variables :
library(tidyverse)
y1 = rpois(10,3)
y2 = rpois(10,5)
y3 = rpois(10,10)
y4 = rpois(10,20)
data = tibble(y1,y2,y3,y4)
Now i have a vector of interest say xx:
xx = rpois(10,4)
I want to calculate the correlation of vector xx with each column variable and doing so i have :
data %>%
dplyr::summarize(across(.cols = everything(), ~ cor(.x,xx, use = "pairwise.complete.obs")))
With result :
# A tibble: 1 x 4
y1 y2 y3 y4
<dbl> <dbl> <dbl> <dbl>
1 0.745 -0.600 -0.200 -0.386
But how i can use ggplot package in order to visualize these 4 correlation values? (heatmap or colourful scatterplot) Any help ?
CodePudding user response:
Something like this ??
library(tidyverse)
set.seed(123)
y1 = rpois(10,3)
y2 = rpois(10,5)
y3 = rpois(10,10)
y4 = rpois(10,20)
data = tibble(y1,y2,y3,y4)
xx = rpois(10,4)
cors <- data %>%
dplyr::summarize(across(.fns = ~ cor(.x,xx, use = "pairwise.complete.obs"))) %>%
pivot_longer(cols = everything()) %>%
mutate(
center_x = c(0.5, 1, 1.5, 2),
center_y = "a",
name = str_c(name, "\n", round(value, 3))
)
cors %>%
ggplot(aes(x = center_x, y = center_y))
geom_tile(aes(fill = value))
scale_fill_distiller()
geom_label(aes(label = name))
theme_void()
labs(title = "Correlation with xx")
CodePudding user response:
How about adding these two lines of code:
data=tibble(data,xx)
GGally::ggcorr(data, label=TRUE)
GGally package allows for pretty neat pairwise visualisations, maybe this is what you are looking for.