Вы хотите преобразовать данные из формата wide
в long
, используя, например, tidyr::gather()
.Вот решение с использованием пакетов в tidyverse
framework
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)
Создано в 2019-02-28 с помощью пакета Представление (v0.2.1.9000)