Ggplot2 ကို အသုံးပြု၍ r တွင် အပူမြေပုံဖန်တီးနည်း
ဤသင်ခန်းစာတွင် ggplot2 ကို အသုံးပြု၍ R တွင် အပူမြေပုံဖန်တီးနည်းကို ရှင်းပြထားသည်။
ဥပမာ- R ဖြင့် အပူမြေပုံဖန်တီးခြင်း။
အပူမြေပုံဖန်တီးရန် ကျွန်ုပ်တို့သည် တပ်ဆင်ထားသည့် R dataset 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 တွင် heatmap ကိုဖန်တီးရန် အောက်ပါကုဒ်ကို အသုံးပြုနိုင်သည်။
library(ggplot2) ggplot(melt_mtcars, aes(variable, char)) + geom_tile(aes(fill = value), color = "white") + scale_fill_gradient(low = "white", high = "red")
ကံမကောင်းစွာဖြင့်၊ disp ၏တန်ဖိုးများသည် data frame ရှိ အခြားသော variable များအားလုံး၏တန်ဖိုးများထက် များစွာကြီးမားသောကြောင့်၊ အခြား variable များ၏ အရောင်ကွဲလွဲမှုကို မြင်ရန်ခက်ခဲပါသည်။
ဤပြဿနာကိုဖြေရှင်းရန်နည်းလမ်းတစ်ခုမှာ 0 မှ 1 အတွင်းရှိ variable တစ်ခုစီ၏တန်ဖိုးများကို scale() ပက်ကေ့ဂျ်ရှိ scale( ) လုပ်ဆောင်ချက်နှင့် plyr() ပက်ကေ့ခ်ျရှိ ddply() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ ပြန်လည်ချိန်ညှိခြင်းဖြစ်သည်-
#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 ကဲ့သို့သော variable များထဲမှ တန်ဖိုးများအတိုင်း အပူမြေပုံကို မှာယူနိုင်သည်-
#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 ကို အသုံးပြုပါ။
#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")