Ggplot2를 사용하여 r에서 히트 맵을 만드는 방법


이 튜토리얼에서는 ggplot2를 사용하여 R에서 히트 맵을 만드는 방법을 설명합니다.

예: R에서 히트맵 생성

히트맵을 생성하기 위해 내장된 R 데이터세트 mtcars를 사용합니다.

 #view first six rows of mtcars
head(mtcars)

# mpg cyl disp hp drat wt qsec vs am gear carb
#Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

현재 mtcars 는 와이드 형식이지만 히트맵을 생성하려면 긴 형식으로 혼합해야 합니다.

 #load reshape2 package to use melt() function
library(reshape2)

#melt mtcars into long format
melt_mtcars <- melt(mtcars)

#add column for car name
melt_mtcars$car <- rep(row.names(mtcars), 11)

#view first six rows of melt_mtcars
head(melt_mtcars)

# variable value char
#1 mpg 21.0 Mazda RX4
#2 mpg 21.0 Mazda RX4 Wag
#3 mpg 22.8 Datsun 710
#4 mpg 21.4 Hornet 4 Drive
#5 mpg 18.7 Hornet Sportabout
#6 mpg 18.1 Valiant

다음 코드를 사용하여 ggplot2에서 히트맵을 만들 수 있습니다.

 library(ggplot2)

ggplot(melt_mtcars, aes(variable, char)) +
  geom_tile(aes(fill = value), color = "white") +
  scale_fill_gradient(low = "white", high = "red")

안타깝게도 disp 의 값은 데이터 프레임 내 다른 모든 변수의 값보다 훨씬 크기 때문에 다른 변수의 색상 변화를 확인하기 어렵습니다.

이 문제를 해결하는 한 가지 방법은 scales() 패키지의 rescale() 함수와 plyr() 패키지의 ddply() 함수를 사용하여 각 변수의 값을 0에서 1로 다시 조정하는 것입니다.

 #load libraries
library(plyr)
library(scales)

#rescale values for all variables in melted data frame
melt_mtcars <- ddply(melt_mtcars, .(variable), transform, rescale = rescale(value))

#create heatmap using rescaled values
ggplot(melt_mtcars, aes(variable, char)) +
  geom_tile(aes(fill = rescale), color = "white") +
  scale_fill_gradient(low = "white", high = "red")

scale_fill_gradient() 인수에 사용된 색상을 변경하여 히트맵 색상을 변경할 수도 있습니다.

 #create heatmap using blue color scale
ggplot(melt_mtcars, aes(variable, char)) +
  geom_tile(aes(fill = rescale), color = "white") +
  scale_fill_gradient(low = "white", high = "steelblue")

현재 히트맵은 자동차 이름별로 분류되어 있습니다. 대신 다음 코드를 사용하여 mpg 와 같은 변수 중 하나의 값에 따라 히트맵을 정렬할 수 있습니다.

 #define car name as a new column, then order by mpg descending
mtcars$car <- row.names(mtcars)
mtcars$car <- with(mtcars, reorder(car, mpg))

#melt mtcars into long format
melt_mtcars <- melt(mtcars)

#rescale values for all variables in melted data frame
melt_mtcars <- ddply(melt_mtcars, .(variable), transform, rescale = rescale(value))

#create heatmap using rescaled values
ggplot(melt_mtcars, aes(variable, char)) +
  geom_tile(aes(fill = rescale), color = "white") +
  scale_fill_gradient(low = "white", high = "steelblue")

mpg 를 늘려 히트맵을 정렬하려면 reorder() 인수에 -mpg 를 사용하면 됩니다.

 #define car name as a new column, then order by mpg descending
mtcars$car <- row.names(mtcars)
mtcars$car <- with(mtcars, reorder(car, -mpg ))

#melt mtcars into long format
melt_mtcars <- melt(mtcars)

#rescale values for all variables in melted data frame
melt_mtcars <- ddply(melt_mtcars, .(variable), transform, rescale = rescale(value))

#create heatmap using rescaled values
ggplot(melt_mtcars, aes(variable, char)) +
  geom_tile(aes(fill = rescale), color = "white") +
  scale_fill_gradient(low = "white", high = "steelblue")

마지막으로 labs() 및 theme() 인수를 사용하여 표시되는 방식이 마음에 들지 않으면 x 및 y 축 레이블과 범례를 제거할 수 있습니다.

 #create heatmap with no axis labels or legend
ggplot(melt_mtcars, aes(variable, char)) +
  geom_tile(aes(fill = rescale), color = "white") +
  scale_fill_gradient(low = "white", high = "steelblue") +
  labs(x = "", y = "") +
  theme(legend.position = "none")

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다