R में facet_wrap का उपयोग कैसे करें (उदाहरण के साथ)
Facet_wrap() फ़ंक्शन का उपयोग ggplot2 में मल्टी-पैनल प्लॉट बनाने के लिए किया जा सकता है।
यह फ़ंक्शन निम्नलिखित मूल सिंटैक्स का उपयोग करता है:
library (ggplot2) ggplot(df, aes (x_var, y_var)) + geom_point() + facet_wrap(vars(category_var))
निम्नलिखित उदाहरण दिखाते हैं कि आर में अंतर्निहित एमपीजी डेटासेट के साथ इस फ़ंक्शन का उपयोग कैसे करें:
#view first six rows of mpg dataset
head(mpg)
manufacturer model displ year cyl trans drv cty hwy fl class
audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
audi a4 2.0 2008 4 auto(front) f 21 30 p compact
audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
audi a4 2.8 1999 6 manual(m5) f 18 26 p compact
उदाहरण 1:facet_wrap() मूल कार्य
निम्नलिखित कोड दिखाता है कि x-अक्ष चर के रूप में displ , y-अक्ष चर के रूप में hwy , और समूहीकरण चर के रूप में class का उपयोग करके ggplot2 में एकाधिक स्कैटरप्लॉट कैसे बनाएं:
ggplot(mpg, aes (displ, hwy)) +
geom_point() +
facet_wrap(vars(class))
उदाहरण 2: कस्टम लेबल का उपयोग करें
निम्नलिखित कोड दिखाता है कि प्लॉट शीर्षकों के लिए कस्टम लेबल के साथ facet_wrap() फ़ंक्शन का उपयोग कैसे करें:
#define custom labels
plot_names <- c('2seater' = "2 Seater",
'compact' = "Compact Vehicle",
'midsize' = "Midsize Vehicle",
'minivan' = "Minivan",
'pickup' = "Pickup Truck",
'subcompact' = "Subcompact Vehicle",
'suv' = "Sport Utility Vehicle")
#use facet_wrap with custom plot labels
ggplot(mpg, aes (displ, hwy)) +
geom_point() +
facet_wrap(vars(class), labeller = as_labeller (plot_names))
उदाहरण 3: कस्टम पैमानों का उपयोग करें
निम्नलिखित कोड दिखाता है कि प्रत्येक व्यक्तिगत प्लॉट के लिए कस्टम स्केल के साथ facet_wrap() फ़ंक्शन का उपयोग कैसे करें:
#use facet_wrap with custom scales
ggplot(mpg, aes (displ, hwy)) +
geom_point() +
facet_wrap(vars(class), scales=' free ')
उदाहरण 4: एक कस्टम कमांड का उपयोग करें
निम्नलिखित कोड दिखाता है कि अलग-अलग प्लॉट के लिए कस्टम ऑर्डरिंग के साथ facet_wrap() फ़ंक्शन का उपयोग कैसे करें:
#define order for plots
mpg <- within(mpg, class <- factor(class, levels=c(' compact ', ' 2seater ', ' suv ',
' subcompact ', ' pickup ',
' minivan ', ' midsize ')))
#use facet_wrap with custom order
ggplot(mpg, aes (displ, hwy)) +
geom_point() +
facet_wrap(vars(class))
ध्यान दें कि प्लॉट हमारे द्वारा निर्दिष्ट सटीक क्रम में दिखाई देते हैं।
अतिरिक्त संसाधन
Ggplot2 में फ़ॉन्ट आकार कैसे बदलें
Ggplot2 में एक लीजेंड को कैसे हटाएं
Ggplot2 में अक्ष लेबल को कैसे घुमाएँ