1

I have a classical linear model y ~ x1 + x2. How can I plot a heatmap of this model? Normally I print a heatmap from data in this way:

library(pheatmap)

data <- data.frame(y=c(1,2,3), x1=c(1.5,2,2.5), x2=c(3,10,12))
pheatmap(cor(data), scale="none")

But in this case I want to see the effect of y on x1 and x2. So I need a heatmap with a covariate for y. Something like this:

pheatmap(cor(select(data,-c(y))), scale="none", covariate=y)

But i didn't find a correct syntax for y as covariate. Please help me.

user3351676
  • 91
  • 1
  • 1
  • 5

1 Answers1

1

How about something like this (where y is qsec, x1 is hp and x2 is wt:

library(tidyr)
library(ggplot2)
data(mtcars)

mod <- lm(qsec ~ hp + wt, data=mtcars)

hp_s <- seq(min(mtcars$hp), max(mtcars$hp), length=25)
wt_s <- seq(min(mtcars$wt), max(mtcars$wt), length=25)

eg <- expand_grid(hp = hp_s, wt=wt_s)
eg$fit <- predict(mod, newdata=eg)

ggplot(eg, aes(x=hp, y=wt, fill=fit)) + 
  geom_tile() + 
  scale_fill_viridis_c() + 
  theme_classic()

Created on 2022-11-27 by the reprex package (v2.0.1)

DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25
  • Thanks a lot. But I want to show the data with a heatmap - more precisely with the pheatmap function. I think it is possible with the parameters annotation_col and annotation_row but actually I don't know how. – user3351676 Nov 29 '22 at 14:32
  • @user3351676 what do you want the two axes of the heat map to be and to what quantity do you want the coolers to correspond? – DaveArmstrong Nov 29 '22 at 17:54