[英]R: pairs plot of one variable with the rest of the variables
我想用我的“ True”變量對與其余所有變量(人變量)生成一個相關圖。 我很確定這已經提出來了,但是我發現的解決方案對我不起作用。
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)
我從哪里得到錯誤:錯誤:列y
必須是一維原子向量或一個列表。
我知道一種解決方案是寫出y中的所有變量-因為我的真實數據有15列,所以我試圖避免這種情況。
我也不完全確定ggplot中的“值”,“變量”指的是什么。 我在演示代碼時看到了很多。
任何建議表示贊賞!
例如,您想使用tidyr::gather()
將數據wide
格式轉換為long
格式。 這是在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)
由reprex軟件包 (v0.2.1.9000)創建於2019-02-28
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.