Ggplot2 में मार्जिन कैसे बदलें (उदाहरण के साथ)
आप किसी प्लॉट के मार्जिन क्षेत्रों को संशोधित करने के लिए ggplot2 में थीम() तर्क का उपयोग कर सकते हैं:
ggplot(df, aes(x=x)) + geom_histogram() + theme(plot. margin =unit(c(5,1,1,1), ' cm '))
ध्यान रखें कि प्लॉट मार्जिन का क्रम इस प्रकार है:
- इकाई(सी(ऊपर, दाएँ, नीचे, बाएँ), इकाइयाँ)
निम्नलिखित उदाहरण दिखाते हैं कि व्यवहार में ggplot2 प्लॉट के मार्जिन क्षेत्रों को कैसे बदला जाए।
उदाहरण 1: एक मूल कथानक बनाएँ
निम्नलिखित कोड दिखाता है कि मार्जिन क्षेत्रों को निर्दिष्ट किए बिना ggplot2 में एक मूल प्लॉट कैसे बनाया जाए:
library (ggplot2)
#make this example reproducible
set. seeds (0)
#create data
df <- data. frame (x=rnorm(n= 5000 ))
#create histogram using ggplot2
ggplot(df, aes(x=x)) +
geom_histogram() +
ggtitle(' Title of Histogram ') +
theme(plot. background =element_rect(fill=' #e3fbff '))
ध्यान दें कि प्लॉट में प्रत्येक तरफ न्यूनतम मार्जिन है।
उदाहरण 2: प्लॉट मार्जिन बदलें
निम्नलिखित कोड दिखाता है कि प्लॉट के ऊपर और नीचे महत्वपूर्ण मार्जिन कैसे जोड़ें:
library (ggplot2)
#make this example reproducible
set. seeds (0)
#create data
df <- data. frame (x=rnorm(n= 5000 ))
#create histogram with significant margins on top and bottom
ggplot(df, aes(x=x)) +
geom_histogram() +
ggtitle(' Title of Histogram ') +
theme(plot. margin =unit(c(5,1,5,1), ' cm '),
plot. background =element_rect(fill=' #e3fbff '))
ध्यान दें कि प्लॉट के ऊपर और नीचे काफी जगह है।
और निम्नलिखित कोड दिखाता है कि प्लॉट के बाएँ और दाएँ महत्वपूर्ण मार्जिन कैसे जोड़ें:
library (ggplot2)
#make this example reproducible
set. seeds (0)
#create data
df <- data. frame (x=rnorm(n= 5000 ))
#create histogram with significant margins on left and right
ggplot(df, aes(x=x)) +
geom_histogram() +
ggtitle(' Title of Histogram ') +
theme(plot. margin =unit(c(1,5,1,5), ' cm '),
plot. background =element_rect(fill=' #e3fbff '))
ध्यान दें कि प्लॉट के बायीं और दायीं ओर काफी जगह है।
अतिरिक्त संसाधन
निम्नलिखित ट्यूटोरियल बताते हैं कि ggplot2 में अन्य सामान्य ऑपरेशन कैसे करें:
Ggplot2 में फ़ॉन्ट आकार कैसे बदलें
Ggplot2 में अक्ष लेबल को कैसे घुमाएँ
Ggplot2 में एक लीजेंड को कैसे हटाएं
Ggplot2 में अक्ष लेबल कैसे हटाएं