How to compare and visualize the ratio of one variable compared to many

I want to use R to visualize and calculate the correlation of one data variable [1] to many other data variables [2:96]

I already know which packages, such as psych and PerformanceAnalytics, have the Pairs function.

Ideally, I would like to draw the same conclusion as for paired outputs, but only for correlations between the data [1] and each of the data [2:96], and not for each of the data elements [1:96] with itself that would take up too much space. Any ideas on this would be appreciated.

+4
source share
4 answers

, . , cor(data)[,1] 1 .

+1

corrr focus() , ggplot2 . , get/plot mpg mtcars:

library(corrr)
library(ggplot2)

x <- mtcars %>% 
  correlate() %>% 
  focus(mpg)
x
#> # A tibble: 10 x 2
#>    rowname        mpg
#>      <chr>      <dbl>
#> 1      cyl -0.8521620
#> 2     disp -0.8475514
#> 3       hp -0.7761684
#> 4     drat  0.6811719
#> 5       wt -0.8676594
#> 6     qsec  0.4186840
#> 7       vs  0.6640389
#> 8       am  0.5998324
#> 9     gear  0.4802848
#> 10    carb -0.5509251

x %>% 
  mutate(rowname = factor(rowname, levels = rowname[order(mpg)])) %>%  # Order by correlation strength
  ggplot(aes(x = rowname, y = mpg)) +
    geom_bar(stat = "identity") +
    ylab("Correlation with mpg") +
    xlab("Variable")

enter image description here

+3

mtcars corrplot{}:

install.packages("corrplot")
library(corrplot)
mcor <- cor(x = mtcars$mpg, y = mtcars[2:11], use="complete.obs")
corrplot(mcor, tl.srt = 25)

: corrplot, : https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html

+1

To get scatter plots with loess lines, you can combine the package tidyrwith ggplot2. Here is an example of scatterplots mpgwith all other variables in a dataset mtcars:

library(tidyr)
library(ggplot2)

mtcars %>%
  gather(-mpg, key = "var", value = "value") %>% 
  ggplot(aes(x = value, y = mpg)) +
    facet_wrap(~ var, scales = "free") +
    geom_point() +
    stat_smooth()

enter image description here

For more information on how this works, see https://drsimonj.svbtle.com/quick-plot-of-all-variables

+1
source

Source: https://habr.com/ru/post/1649538/


All Articles