Ggplot2 का उपयोग करके r में हीट मैप कैसे बनाएं


यह ट्यूटोरियल बताता है कि ggplot2 का उपयोग करके 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

वर्तमान में एमटीकार्स एक विस्तृत प्रारूप में है, लेकिन हीटमैप बनाने के लिए हमें इसे एक लंबे प्रारूप में मिश्रित करने की आवश्यकता है।

 #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")

दुर्भाग्य से, क्योंकि डिस्प का मान डेटा फ़्रेम में अन्य सभी वेरिएबल्स के मानों से बहुत बड़ा है, इसलिए अन्य वेरिएबल्स के रंग भिन्नता को देखना मुश्किल है।

इस समस्या को हल करने का एक तरीका स्केल्स() पैकेज में रीस्केल() फ़ंक्शन और 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")

हम स्केल_फिल_ग्रेडिएंट() तर्क में उपयोग किए गए रंगों को बदलकर हीटमैप के रंग भी बदल सकते हैं:

 #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() और थीम() तर्कों का उपयोग करने का तरीका पसंद नहीं है तो हम 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")

एक टिप्पणी जोड़ने

आपका ईमेल पता प्रकाशित नहीं किया जाएगा. आवश्यक फ़ील्ड चिह्नित हैं *