简体   繁体   English

R:将一个变量与其余变量配对绘制

[英]R: pairs plot of one variable with the rest of the variables

I would like to generate a correlation plot with my "True" variable pairs with all of the rest (People variables). 我想用我的“ True”变量对与其余所有变量(人变量)生成一个相关图。 I am pretty sure this has been brought up somewhere but solutions I have found do not work for me. 我很确定这已经提出来了,但是我发现的解决方案对我不起作用。

library(ggplot2)
set.seed(0)

dt = data.frame(matrix(rnorm(120, 100, 5), ncol = 6) )
colnames(dt) = c('Salary', paste0('People', 1:5))
ggplot(dt, aes(x=Salary, y=value)) +
  geom_point() + 
  facet_grid(.~Salary)

Where I got error: Error: Column y must be a 1d atomic vector or a list. 我从哪里得到错误:错误:列y必须是一维原子向量或一个列表。

I know one of the solutions is writing out all of the variables in y - which I am trying to avoid because my true data has 15 columns. 我知道一种解决方案是写出y中的所有变量-因为我的真实数据有15列,所以我试图避免这种情况。

Also I am not entirely sure what do the "value", "variables" refer to in the ggplot. 我也不完全确定ggplot中的“​​值”,“变量”指的是什么。 I saw them a lot in demonstrating codes. 我在演示代码时看到了很多。

Any suggestion is appreciated! 任何建议表示赞赏!

You want to convert your data from wide to long format using tidyr::gather() for example. 例如,您想使用tidyr::gather()将数据wide格式转换为long格式。 Here is a solution using packages in the tidyverse framework 这是在tidyverse框架中使用包的解决方案

library(tidyr)
library(ggplot2)
theme_set(theme_bw(base_size = 14))

set.seed(0)
dt = data.frame(matrix(rnorm(120, 100, 5), ncol = 6) )
colnames(dt) = c('Salary', paste0('People', 1:5))

### convert data frame from wide to long format
dt_long <- gather(dt, key, value, -Salary)
head(dt_long)
#>      Salary     key     value
#> 1 106.31477 People1  98.87866
#> 2  98.36883 People1 101.88698
#> 3 106.64900 People1 100.66668
#> 4 106.36215 People1 104.02095
#> 5 102.07321 People1  99.71447
#> 6  92.30025 People1 102.51804

### plot
ggplot(dt_long, aes(x = Salary, y = value)) +
  geom_point() +
  facet_grid(. ~ key) 

### if you want to add regression lines
library(ggpmisc)

# define regression formula
formula1 <- y ~ x

ggplot(dt_long, aes(x = Salary, y = value)) +
  geom_point() +
  facet_grid(. ~ key) +
  geom_smooth(method = 'lm', se = TRUE) +
  stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~")), 
               label.x.npc = "left", label.y.npc = "top",
               formula = formula1, parse = TRUE, size = 3) +
  coord_equal()

### if you also want ggpairs() from the GGally package
library(GGally)
ggpairs(dt)

Created on 2019-02-28 by the reprex package (v0.2.1.9000) reprex软件包 (v0.2.1.9000)创建于2019-02-28

You need to stack() your data first, probably that's what you have "seen". 您需要首先对数据进行stack() ,这可能就是您所看到的。

dt <- setNames(stack(dt), c("value", "Salary"))

library(ggplot2)
ggplot(dt, aes(x=Salary, y=value)) +
  geom_point() + 
  facet_grid(.~Salary)

Yields 产量

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在R中的一个绘图上绘制2对变量? - How Do I plot 2 pairs of variables on one plot in R? 用于将对()变量映射到对角线中的公共垂直轴变量的函数 - Function to plot pairs() variables against common vertical axis variable in the diagonal 如何在R中绘制一列与其余列 - How to plot one column vs the rest in R 如何在 R 中的一个 plot 中打印不同的变量 - How to print different variables in one plot in R 一个条中的多个变量 R 中的 plot - Multiple variables in one bar plot in R 在R中绘制多个箱形图(几个数字变量与一个类别变量) - Plot multiple box-plots (several numeric variables vs. one categorical variable) in R 如何根据R中的因变量绘制两个自变量,其中一个是前N个计数 - How to plot two independent variables with one being a top N count based on the dependent variable in R 分位数-分位数图:比较多个建模变量与一个守恒变量ggplot2 R - Quantile-Quantile plot: compare multiple modelled variables vs one oberved variable ggplot2 R 合并合并10个变量并将它们组合成一个变量 - Merge 10 variables in pairs and make them into one variable R:重复 for 循环,该循环使用数据集中的两个变量作为数据集中其余的变量对 - R: repeat a for loop which uses two variables in a dataset for the rest of the pairs of variables in the dataset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM