मुख्य सामग्री पर जाएँ

R उपयोगकर्ताओं के प्लॉट्स से जुड़े 15 आम सवाल

There are different types of R plots, ranging from the basic graph types to complex types of graphs. Here we discover how to create these.
· 15 मि॰ पढ़ें

AI के साथ खोजें

DataCamp की R कोर्स लाइब्रेरी बैनर देखें

R आपको विभिन्न प्रकार के प्लॉट्स बनाने देता है, बुनियादी ग्राफ़ प्रकारों जैसे density plots, dot plots, boxplots और scatter plots से लेकर अधिक सांख्यिकीय रूप से जटिल ग्राफ़ जैसे probability plots तक।

इसके अलावा, यह आपको बहुत कम कस्टमाइज़ेशन के साथ बुनियादी ग्राफ़ बनाने से लेकर इंटरएक्टिव ग्राफ़िक्स के साथ पूर्ण कस्टमाइज़ेशन में उन्नत ग्राफ़्स प्लॉट करने तक जाने देता है। फिर भी, हमें हमेशा वह परिणाम नहीं मिलता जो हम अपने R प्लॉट्स के लिए चाहते हैं: Stack Overflow हमारे प्लॉट्स से जुड़े सवालों से भरा हुआ है, और कई सवाल बार-बार आते हैं।

इसीलिए DataCamp ने सभी अक्सर पूछे जाने वाले सवालों और उनके सर्वाधिक रेटेड जवाबों को एक ब्लॉग पोस्ट में एकत्र करने का फ़ैसला किया।

R में प्लॉट बनाना अभ्यास करने के लिए, R में डेटा विज़ुअलाइज़ेशन पर यह कोर्स आज़माएँ।

15 R प्लॉट्स से जुड़े सवाल

1. खाली R प्लॉट कैसे बनाएं

नया प्लॉट फ़्रेम खोलें

You can open an empty plot frame and activate the graphics device in R as follows:
plot.new()

# Or try out the following command 
# frame()

नोट कि plot.new() और frame() फ़ंक्शनों से कोई अक्ष, लेबल या आउटलाइन के बिना नया प्लॉट फ़्रेम बनता है। यह दर्शाता है कि एक नया प्लॉट बनाया जाना है: यदि कोई ग्राफ़िक्स विंडो खुली नहीं है तो नई विंडो खुलेगी, अन्यथा मौजूदा विंडो नए प्लॉट के लिए तैयार की जाएगी। आप इन फ़ंक्शनों के बारे में यहाँ पढ़ सकते हैं।

  • x11() X Window System (संस्करण 11) के लिए R में नई विंडो डिवाइस भी खोल सकता है!
  • quartz() OS X सिस्टम के लिए ग्राफ़िक्स डिवाइस ड्राइवर शुरू करता है।

ग्राफ़िक्स विंडो के माप सेट करें

आप अभी बनाए गए खाली प्लॉट की क्षैतिज और ऊर्ध्वाधर सीमाएँ सेट करने के लिए plot.window() फ़ंक्शन भी उपयोग कर सकते हैं:

plot.new()
pWidth = 3
pHeight = 2
plot.window(c(0,pWidth),
            c(0,pHeight))

वास्तव में खाली प्लॉट बनाएं

आप plot() फ़ंक्शन से खाली प्लॉट बना सकते हैं। साथ ही, mtext() और title() फ़ंक्शनों से अक्ष, अक्ष लेबल और एक शीर्षक जोड़कर खाली प्लॉट को भरें।

# Draw an empty plot
plot(5, 
     5, 
     type="n", 
     axes=FALSE, 
     ann=FALSE, 
     xlim=c(0, 10), 
     ylim = c(0,10))

# Add text to the x-axis
mtext("x-axis", 
      side=1)

# Add text to the y-axis
mtext("y-axis",
      side=2) 

# Add a title
title("An R Plot")

आप प्लॉट के लिए निर्देशांक (5,5) देते हैं, लेकिन type आर्ग्यूमेंट को "n" पर सेट करके वास्तव में कुछ भी प्लॉट नहीं कराते।

टिप: इस आर्ग्यूमेंट को "p" या "b" पर सेट कर देखें कि क्या होता है!

इसके अलावा, आप प्लॉट को annotate नहीं करते, लेकिन x- और y-अक्ष पर 0 से 10 तक सीमाएँ ज़रूर लगाते हैं।

अंत में, आप box() फ़ंक्शन से प्लॉट के चारों ओर बॉक्स बना सकते हैं और points() फ़ंक्शन से कुछ बिंदु जोड़ सकते हैं:

# Draw an empty plot
plot(5, 
     5, 
     type="n", 
     axes=FALSE, 
     ann=FALSE, 
     xlim=c(0, 10), 
     ylim = c(0,10))

# Add text to the x-axis
mtext("x-axis", 
      side=1)

# Add text to the y-axis
mtext("y-axis",
      side=2) 

# Add a title
title("An R Plot")

#Draw a box
box()

#Put (red) point in the plot at (5,5)
points(5,
       5, 
       col="red") 
points(5, 
       7, 
       col="orange", 
       pch=3, 
       cex=2)
points(c(0, 0, 1), 
       c(2, 4, 6), 
       col="green", 
       pch=4)

नोट कि आप एक साथ कई बिंदु प्लॉट करने के लिए x- और y-निर्देशांकों को वेक्टर में रख सकते हैं। pch आर्ग्यूमेंट से आप प्रतीक चुनते हैं, जबकि cex का मान यह दर्शाता है कि डिफ़ॉल्ट की तुलना में प्लॉट किया गया टेक्स्ट और प्रतीक कितने स्केल हों।

टिप: किस नंबर से कौन-सा प्रतीक जुड़ा है यह देखने के लिए यहाँ क्लिक करें।

2. R प्लॉट्स के अक्ष लेबल और शीर्षक कैसे सेट करें

R प्लॉट्स के अक्ष Stack Overflow पर सबसे लोकप्रिय विषयों में से एक हैं; इस विषय से जुड़े सवाल काफ़ी विविध हैं।

पढ़ते रहें और जानें कि DataCamp ने किन-किन प्रकार के सवालों को आम पाया!

अक्षों का नाम दें (सुपरस्क्रिप्ट या सबस्क्रिप्ट के साथ) और R प्लॉट को शीर्षक दें

आप आसानी से अक्षों को नाम दे सकते हैं और शीर्षक लगाकर अपने R प्लॉट को अपने दर्शकों के लिए अधिक विशिष्ट और समझने योग्य बना सकते हैं।

यह main (मुख्य शीर्षक), sub (उपशीर्षक), xlab (x-अक्ष का लेबल) और ylab (y-अक्ष का लेबल) आर्ग्यूमेंट्स जोड़कर किया जा सकता है।

नोट कि यदि आप शीर्षक में सुपरस्क्रिप्ट या सबस्क्रिप्ट चाहते हैं, तो आप इन्हें आसानी से plot() के main आर्ग्यूमेंट में जोड़ सकते हैं। सुनिश्चित करें कि आप expression() का प्रयोग करें और सुपरस्क्रिप्ट के लिए ^ तथा सबस्क्रिप्ट के लिए चौकोर ब्रैकेट का उपयोग करें।

नीचे दिए DataCamp Light ब्लॉक में आज़माएँ:

# The data
x <- seq(0,2*pi,0.1)
y <- sin(x)

# Plot the data
plot(x,
     y,
     main=expression("main title"^2), 
     sub=expression("sub-title"[2]), 
     xlab="x-axis label", 
     ylab="y-axis label")

आप अक्ष लेबल्स में ग्रीक अक्षर भी सुनिश्चित कर सकते हैं, जहाँ expression() और paste() फ़ंक्शनों का संयोजन किया जाता है। निम्न कोड चलाएँ:

# The data
x <- seq(0,2*pi,0.1)
y <- sin(x)

# Plot the data
plot(x,
     y,
     xlab = expression(paste("Greek letter ", phi)),
     ylab = expression(paste("Greek letter ",mu)))

अक्ष लेबल्स का स्वरूप समायोजित करें

To adjust the appearance of the x-and y-axis labels, you can use the arguments col.lab and cex.lab. The first argument is used to change the color of the x-and y-axis labels, while the second argument is used to determine the size of the x-and y-axis labels, relative to the (default) setting of cex.
# The data
x <- seq(0,2*pi,0.1)
y <- sin(x)

# Plot the data
plot(x,
     y,
     main=expression("main title"^2), 
     sub=expression("sub-title"[2]), 
     xlab="x-axis label", 
     ylab="y-axis label",
     col.lab="blue",
     cex.lab=0.75)

इन आर्ग्यूमेंट्स पर अधिक जानकारी के लिए इस पेज पर जाएँ।

प्लॉट के अक्ष लेबल और एनोटेशन हटाएँ

यदि आप किसी प्लॉट के अक्ष मान हटाना चाहते हैं, तो पहले xaxt और yaxt आर्ग्यूमेंट्स जोड़ें और उन्हें "n" पर सेट करें। ये आर्ग्यूमेंट्स x-अक्ष के प्रकार को दर्शाते हैं। यदि आप इसमें "n" डालते हैं, जैसा कि नीचे कमांड में है, तो अक्ष का प्लॉटिंग दब जाएगा।

ध्यान दें कि xaxt और yaxt में कोई अन्य कैरेक्टर देने पर x और y अक्ष प्लॉट होंगे।

Next, you can add the annotation argument ann and set it to FALSE to make sure that any axis labels are removed.
# The data
x <- seq(0,2*pi,0.1)
y <- sin(x)

# Plot the data
plot(x,
     y,
     xaxt="n",
     yaxt="n",
     ann=FALSE)

टिप: जो जानकारी आप ढूँढ रहे हैं वह नहीं मिली? इस पेज पर जाएँ।

प्लॉट के अक्ष लेबल घुमाएँ

You can add the las argument to the axis() function to rotate the numbers that correspond to each axis:
# The data
x <- seq(0,2*pi,0.1)
y <- sin(x)

# Plot the data, no axes
plot(x,
     y, 
     axes=FALSE)

# Draw a box
box()

# Add (modified) axes to the plot
axis(2, 
     las=2)
axis(1, 
     las=0)

नोट कि इसके लिए आपको plot() में एक आर्ग्यूमेंट जोड़ना पड़ता है जो कहता है कि अभी अक्ष प्लॉट नहीं होंगे: यह काम बाद में आने वाले दो axis() फ़ंक्शनों का है।

las आर्ग्यूमेंट के चार मान हो सकते हैं। विकल्प के अनुसार लेबल का प्लेसमेंट बदलेगा: 0 पर लेबल हमेशा अक्ष के समांतर होगा (डिफ़ॉल्ट); 1 पर लेबल क्षैतिज होगा। 2 पर वह अक्ष के लंबवत होगा और 3 पर वह लंबवत रूप में रखा जाएगा।

और भी है। अगर आप axis() फ़ंक्शन की और संभावनाएँ जानना चाहते हैं, तो पढ़ते रहें!

अपने R प्लॉट के अक्ष लेबल्स को इधर-उधर करें

तो, आप अक्ष लेबल्स को इधर-उधर करना चाहते हैं?

कोई दिक्कत नहीं, आप यह axis() फ़ंक्शन से कर सकते हैं; जैसा कि आपने पहले भी देखा, यह फ़ंक्शन आपको पहले यह बताने देता है कि आप अक्ष कहाँ खींचना चाहते हैं। मान लें कि आप x-अक्ष को प्लॉट क्षेत्र के ऊपर और y-अक्ष को दाएँ तरफ़ खींचना चाहते हैं।

याद रखें कि यदि आप axis() में 1 या 2 देते हैं, तो अक्ष क्रमशः नीचे और बाएँ खींचा जाएगा। इसका उदाहरण ऊपर के कोड में देखें!

This means that you will want to pass 3 and 4 to the axis() function:
# The data
x<-seq(0,2*pi,0.1)
y<-sin(x)

# Plot the data, but don't plot axes or annotations
plot(x,
     y,
     axes=FALSE, 
     ann=FALSE)

# Draw the x-axis above the plot area
axis(3)

# Draw the y-axis to the right of the plot area
axis(4)

# Draw a box
box()

जैसा कि आप देख सकते हैं, पहले आप मूल रूप से x और y प्लॉट करते हैं, लेकिन अक्ष और एनोटेशन छोड़ देते हैं। फिर, जिन अक्षों की ज़रूरत है, उन्हें प्लॉट के सापेक्ष स्थान बताकर जोड़ते हैं।

axis() फ़ंक्शन की लचीलेपन से आप और भी कर सकते हैं! अगला सवाल देखें कि इस बुनियादी R फ़ंक्शन से और क्या हल किया जा सकता है।

टिप: आख़िरी सवाल पर जाएँ और देखें कि hjust और vjust से अक्ष लेबल्स में टेक्स्ट को कैसे खिसकाएँ!

3. अपने R प्लॉट के टिक मार्क्स जोड़ें और उनकी दूरी कैसे बदलें

अपने R प्लॉट के टिक मार्क्स की दूरी कैसे बदलें

टिक मार्क्स R पर छोड़ देना कई बार खीझ पैदा कर सकता है और समय आ सकता है जब आप इन्हें समायोजित करना चाहेंगे।

1. अपने प्लॉट के टिक मार्क्स निर्धारित करने के लिए axis() फ़ंक्शन का उपयोग

Consider the following piece of code:
# Define the position of tick marks
v1 <- c(0,pi/2,pi,3*pi/2,2*pi)

# Define the labels of tick marks
v2 <- c("0","Pi/2","Pi","3*Pi/2","2*Pi")

# The data
x <- seq(0,2*pi,0.1)
y <- sin(x)

# Plot the data
plot(x,
     y,
     xaxt = "n")

# Add an axis to the plot 
axis(side = 1, 
     at = v1, 
     labels = v2,
     tck=-.05)

जैसा कि आप देख सकते हैं, आप पहले टिक मार्क्स की स्थिति और उनके लेबल परिभाषित करते हैं। फिर आप अपना प्लॉट बनाते हैं और x-अक्ष के प्रकार को "n" के रूप में निर्दिष्ट करते हैं, जिससे अक्ष का प्लॉटिंग दब जाता है।

इसके बाद, असली काम शुरू होता है:

  • axis() फ़ंक्शन आपको यह बताने देता है कि प्लॉट के किस तरफ़ अक्ष बनना है। यहाँ आर्ग्यूमेंट 1 है, मतलब अक्ष नीचे बनेगा। 2 होने पर बाएँ, और 3 या 4 होने पर क्रमशः ऊपर या दाएँ अक्ष बनेगा;
  • at आर्ग्यूमेंट से आप बताते हैं कि टिक मार्क्स किन बिंदुओं पर बनें। यहाँ आप V1 में परिभाषित पोज़िशन्स का उपयोग करते हैं;
  • इसी तरह, जिन labels का आप उपयोग करते हैं वे V2 में निर्दिष्ट हैं;
  • आप tck से टिक की दिशा समायोजित करते हैं: नकारात्मक मान देने पर टिक अक्ष के नीचे दिखाई देंगे।

टिप: tck को धनात्मक मान देकर देखें क्या होता है!

You can further specify the size of the ticks through tcl and the appearance of the tick labels is controlled with cex.axis, col.axis and font.axis.
# Define the position of tick marks
v1 <- c(0,pi/2,pi,3*pi/2,2*pi)

# Define the labels of tick marks
v2 <- c("0","Pi/2","Pi","3*Pi/2","2*Pi")

# The data
x <- seq(0,2*pi,0.1)
y <- sin(x)

# Plot the data
plot(x,
     y,
     xaxt = "n")

# Add axis to the plot 
axis(side = 1, 
     at = v1, 
     labels = v2,
     tck=-.1,
     tcl = -0.5,
     cex.axis=1.05,
     col.axis="blue",
     font.axis=5)

2. टिक मार्क्स निर्धारित करने के लिए अन्य फ़ंक्शनों का उपयोग

आप par() और plot() फ़ंक्शन से भी टिक मार्क्स की पोज़िशन और उनके बीच के इंटरवल्स की संख्या निर्धारित कर सकते हैं।

नोट कि तब आप xaxp आर्ग्यूमेंट का उपयोग करते हैं जहाँ आप x-अक्ष के सिरों पर टिक मार्क्स की स्थिति और उनके बीच इंटरवल्स की संख्या पास करते हैं:
# The data
x <- seq(0,2*pi,0.1)
y <- sin(x)

# Plot the data
plot(x,
     y,
     xaxp = c(0, 2*pi, 5))

आप लगभग वही परिणाम पाने के लिए निम्न कोड भी चला सकते हैं:

# The data 
x <- seq(0,2*pi,0.1)
y <- sin(x)

# Plot the data 
plot(x, 
     y, 
     xaxt="n")

# Use `par()` to set graphical `xaxp` parameter
par(xaxp= c(0, 2*pi, 2)) 

# Add an axis to the plot 
axis(1)

नोट कि यह तभी काम करेगा जब आप लॉगरिदमिक स्केल का उपयोग न करें। यदि लॉग कोऑर्डिनेट्स true हैं, यानी par(xlog=T), तो xaxp को दिए गए तीन मानों का अर्थ अलग होता है: छोटे रेंज के लिए n नकारात्मक होता है। अन्यथा, n 1:3 में होता है जो केस नंबर बताता है, और x1 तथा x2 यूज़र कोऑर्डिनेट्स के भीतर 10 की सबसे छोटी और सबसे बड़ी घात हैं, 10 ^ par(“usr”)[1:2]।

An example:
n <- 1

# The data
x <- seq(0, 10000, 1)
y <- exp(n)/(exp(n)+x)

# Set graphical parameters `xlog` and `xaxp`
par(xlog=TRUE, 
    xaxp= c(1, 4, 3))

# Pot the data 
plot(x, 
     y, 
     log="x") 

इस उदाहरण में, आप par() फ़ंक्शन का उपयोग करते हैं: xlog को TRUE करते हैं और xaxp आर्ग्यूमेंट जोड़ते हैं ताकि चरम टिक मार्क्स के निर्देशांक और उनके बीच इंटरवल्स की संख्या दें। यहाँ, न्यूनतम मान 1, अधिकतम 4 और टिक के बीच इंटरवल्स की संख्या 3 सेट की जाती है।

इसके बाद, आप x और y प्लॉट करते हैं और log आर्ग्यूमेंट जोड़ते हैं ताकि x- या y-अक्ष या दोनों को लॉग स्केल पर प्लॉट करें। इसके लिए आप "x", "y", और "xy" मान पास कर सकते हैं।

An example with both axes in logarithmic scale is:
n <- 1

# The data
x <- seq(0, 20, 1)
y <- exp(x)/(x)

# Set graphical parameters `xlog` and `yaxp`
par(xlog=TRUE, 
    xaxp= c(1, 4, 3))
# Set graphical parameters `ylog` and `yaxp`
par(ylog=TRUE, 
    yaxp= c(1, 11, 2)) 

# Plot the data 
plot(x, 
     y, 
     log="xy")

R प्लॉट में minor tick marks कैसे जोड़ें

You can quickly add minor tick marks to your plot with the minor.tick() function from the Hmisc package:
# Import `Hmisc` library
library(Hmisc)

# Make a new plot
plot.new()

# Set minor tick mark
minor.tick(nx = 1.5, 
           ny = 2, 
           tick.ratio=0.75)
  • nx आर्ग्यूमेंट आपको बताने देता है कि major tick marks के बीच के हिस्से को आप कितने इंटरवल्स में बाँटना चाहते हैं। यदि आप 1 पास करते हैं, तो minor tick marks दब जाएँगे;
  • ny y-अक्ष के लिए nx जैसा ही है;
  • tick.ratio minor टिक की लंबाई और major टिक की लंबाई का अनुपात दर्शाता है। बाद वाले की लंबाई par(tck=x) से ली जाती है।

4. दो अलग-अलग X या Y-अक्ष कैसे बनाएं

The first option is to create a first plot and to execute the par() function with the new argument put to TRUE to prevent R from clearing the graphics device:
# Set a seed 
set.seed(101)

# The data
x <- 1:10
y <- rnorm(10)
z <- runif(10, min=1000, max=10000)

# Plot the data
plot(x, y) 

# Don't clean the frame before drawing
par(new = TRUE)

फिर, आप plot() से दूसरा प्लॉट बनाते हैं। डेटा पास करें, और यह सुनिश्चित करें कि type, axes, bty, xlab और ylab जैसे कुछ आर्ग्यूमेंट्स समायोजित हों:

  • type: लाइनों के साथ प्लॉट करने के लिए l पास कर सकते हैं;
  • axes को FALSE पर सेट किया गया है ताकि कोई अक्ष न प्लॉट हो;
  • bty प्लॉट के चारों ओर बॉक्स बनना दबाता है;
  • xlab खाली स्ट्रिंग पर है, यानी x-अक्ष पर लेबल नहीं, और
  • ylab भी खाली है, इसलिए y-अक्ष पर भी लेबल नहीं।

नीचे दिए गए DataCamp Light ब्लॉक में सब देखें। ध्यान दें कि ऊपर का कोड पहले से आपके कार्य परिवेश में लोड है। दाएँ ओर R Console में जाँच सकते हैं:

plot(x, 
     z, 
     type = "l", 
     axes = FALSE,
     bty = "n", 
     xlab = "", 
     ylab = "")

अगला चरण है अक्ष जोड़ना और क्रमशः axis() और mtext() से प्लॉट के मार्जिन में टेक्स्ट लिखना।

आप दाईं ओर नया अक्ष axis() में side आर्ग्यूमेंट को 4 देकर जोड़ सकते हैं। अगला, at आर्ग्यूमेंट बताता है कि टिक-मार्क कहाँ बनें। यहाँ आप pretty() फ़ंक्शन से z के मानों की रेंज को कवर करने वाले n+1 समान दूरी पर “राउंड” मान बनाते हैं। यह सुनिश्चित करता है कि आपके दूसरे प्लॉट के y-अक्ष वाले नंबर, जिसे आपने z नाम दिया है, वही हों।

यह सब नीचे आज़माएँ:

axis(side=4, 
     at = pretty(range(z)))
mtext("z", 
      side=4, 
      line=3)

नोट कि side आर्ग्यूमेंट के मान 1 (नीचे), 2 (बाएँ), 3 (ऊपर) और 4 (दाएँ) हो सकते हैं। line आर्ग्यूमेंट बताता है कि टेक्स्ट किस मार्जिन लाइन पर शुरू होता है।

आपके कोड का अंतिम परिणाम कुछ ऐसा दिखेगा:

set.seed(101)
x <- 1:10
y <- rnorm(10)
z <- runif(10, min=1000, max=10000)
plot(x, y) 
par(new = TRUE)
plot(x, 
     z, 
     type = "l", 
     axes = FALSE, 
     bty = "n", 
     xlab = "", 
     ylab = "")
axis(side=4, 
     at = pretty(range(z)))
mtext("z", 
      side=4, 
      line=3)

अभ्यास के लिए, दो अलग-अलग x-अक्षों के साथ एक R प्लॉट बनाने की कोशिश करें! नीचे दिए DataCamp Light ब्लॉक में करें:

plot.new()
set.seed(101)
x <- 1:10
y <- rnorm(10)
z <- runif(10, min=1000, max=10000) 
par(mar = c(5, 4, 4, 4) + 0.3)
plot(x, y)
par(new = TRUE)
plot(z, y, type = "l", axes = FALSE, bty = "n", xlab = "", ylab = "")
axis(side=3, at = pretty(range(z)))
mtext("z", side=3, line=3)

ध्यान दें, भले ही आप यह सब बेसिक plot(), axis() और mtext() से मैन्युअली कर सकते हैं, कुछ पैकेज इसे ऑटोमेट कर देते हैं: plotrix पैकेज का twoord.plot() और latticeExtra का doubleYScale() इसी काम के लिए काफ़ी उपयोगी और तेज़ हैं:

# Import `latticeExtra`
library(latticeExtra)

# The data
chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), header = TRUE)
Age <- chol$AGE
Chol <- chol$CHOL
Smoke <- chol$SMOKE
State <- chol$MORT

# Create a bivariate scatterplot `a` 
a <- xyplot(Chol ~ Age|State)

# Create a bivariate scatterplot `b` 
b <- xyplot(Smoke ~ Age|State)

# Draw the two plots with different y scales 
doubleYScale(a, b, style1 = 0, style2 = 3, add.ylab2 = TRUE, columns=3)

नोट कि ऊपर का उदाहरण इस डेटा सेट से बनाया गया है। यदि डेटा इम्पोर्ट या read.table() को लेकर कोई शंका है, तो DataCamp का R में डेटा इम्पोर्ट ट्यूटोरियल देखें।

5. R प्लॉट की legend कैसे जोड़ें या बदलें

बेसिक R से R प्लॉट की legend जोड़ना और बदलना

You can easily add a legend to your R plot with the legend() function:
# The data
x <- seq(0,pi,0.1)
y1 <- cos(x)
y2 <- sin(x)

# Plot 
plot(c(0,3), c(0,3), type="n", xlab="x", ylab="y")

# Draw lines
lines(x, y1, col="red", lwd=2)
lines(x, y2, col="blue", lwd=2)

# Add legend to plot 
legend("topright", 
       inset=.05, 
       cex = 1, 
       title="Legend", 
       c("Cosinus","Sinus"), 
       horiz=TRUE, 
       lty=c(1,1), 
       lwd=c(2,2), 
       col=c("red","blue"), 
       bg="grey96")

ध्यान दें कि legend() के दस्तावेज़ में वर्णित pt.cex और title.cex आर्ग्यूमेंट्स वास्तव में काम नहीं करते। कुछ उपाय हैं:

  1. legend() में text.font आर्ग्यूमेंट जोड़कर legend के शीर्षक या लेबल्स का फ़ॉन्ट बदलें:
# The data
x <- seq(0,pi,0.1)
y1 <- cos(x)
y2 <- sin(x)

# Plot 
plot(c(0,3), c(0,3), type="n", xlab="x", ylab="y")

# Draw lines
lines(x, y1, col="red", lwd=2)
lines(x, y2, col="blue", lwd=2)

# Add legend
legend("topright", 
       inset=.05, 
       cex = 1, 
       title="Legend", 
       c("Cosinus","Sinus"), 
       horiz=TRUE, 
       lty=c(1,1), 
       lwd=c(2,2), 
       col=c("red","blue"), 
       bg="grey96",
       text.font=3)
  1. अलग-अलग cex मानों के साथ legend दो बार ड्रॉ करें
# The data
x <- seq(0,pi,0.1)
y1 <- cos(x)
y2 <- sin(x)

# Plot
plot(c(0,3), c(0,3), type="n", xlab="x", ylab="y")

# Draw lines
lines(x, y1, col="red", lwd=2)
lines(x, y2, col="blue", lwd=2)

# Add subtitles for legend
legend("topright",
       inset=.05,
       cex = 0.75,
       c("Cosinus","Sinus"),
       title="",
       horiz=TRUE,
       lty=c(1,1), 
       lwd=c(2,2), 
       col=c("red","blue"))

# Add title to legend 
legend(2.05, 2.97, 
       inset=.05,
       c("",""),
       title="Legend",
       cex=1.15, 
       bty="n")

टिप: यदि आप जानना चाहते हैं कि R में कौन-कौन से रंग उपलब्ध हैं, तो यह उपयोगी PDF देखें।

ggplot2 में R प्लॉट की legend और लेबल कैसे जोड़ें और बदलें

Adding a legend to your ggplot2 plot is fairly easy. You can just execute the following:
# Load in `ggplot2`
library(ggplot2)

# Read in the data
chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), 
                   header = TRUE)

# Plot the data 
ggplot(chol, aes(x=chol$WEIGHT, 
                 y=chol$HEIGHT)) + 
  geom_point(aes(colour = factor(chol$MORT), 
                 shape=chol$SMOKE)) + 
  xlab("Weight") + 
  ylab("Height") 

और यह आपको एक डिफ़ॉल्ट legend देता है। लेकिन अधिकतर मामलों में, आप legend की सूरत और भी समायोजित करना चाहेंगे।

ggplot2 में legend शीर्षक और लेबल बदलने के दो तरीक़े हैं:

  1. स्केल को अलग शीर्षक और लेबल उपयोग करने के लिए कहें
If you have specified arguments such as colour or shape, or other aesthetics, you need to change the names and labels through the scale_color_discrete() and scale_shape_discrete() functions, respectively:
# Load in `ggplot2`
library(ggplot2)

# Read in data
chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), 
                   header = TRUE)

# Plot the data
ggplot(chol, aes(x=chol$WEIGHT, 
                 y=chol$HEIGHT)) + 
  geom_point(aes(colour = factor(chol$MORT), 
                 shape = chol$SMOKE)) + 
  xlab("Weight") + 
  ylab("Height") + 
  theme(legend.position=c(1,0.5),
        legend.justification=c(1,1)) + 
  scale_color_discrete(name ="Condition", 
                       labels=c("Alive", "Dead")) +
  scale_shape_discrete(name="Smoker", 
                       labels=c("Non-smoker", "Sigare", "Pipe" ))

ध्यान दें कि DataCamp Light के Plot टैब में प्लॉट थोड़ा दबी हुई नज़र आता है, लेकिन RStudio में कोड चलाने पर यह समस्या नहीं होगी।

ध्यान दें कि यदि आप geom_point() और labels आर्ग्यूमेंट्स में shape जोड़ते हैं, तो आप दो legends बना सकते हैं!

यदि आप legend को प्लॉट के नीचे ले जाना चाहते हैं, तो legend.position को "bottom" सेट करें। वहीं legend.justification आर्ग्यूमेंट आपको प्लॉट के अंदर legend की पोज़िशनिंग देता है।

टिप: ggplot को अन्य नाम और लेबल इस्तेमाल करने के लिए बताए जाने योग्य विभिन्न स्केल्स यहाँ देखें।

  1. डेटा फ़्रेम को ऐसे बदलें कि फ़ैक्टर मनचाहे रूप में हों
For example:
# Read in data
chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), header = TRUE)

# Manipulate the data
levels(chol$SMOKE)[levels(chol$SMOKE)=="Non-smoker"] <- "Non-smoker"
levels(chol$SMOKE)[levels(chol$SMOKE)=="Sigare"] <- "Sigare"
levels(chol$SMOKE)[levels(chol$SMOKE)=="Pipe"] <- "Pipe"
names(chol)[names(chol)=="SMOKE"]  <- "SMOKER"

# Inspect result 
head(chol)

इसके बाद आप नए फ़ैक्टर नामों का उपयोग ggplot2 में अपने प्लॉट के लिए कर सकते हैं, जिससे प्लॉटिंग कोड में अतिरिक्त लाइनों से नाम और लेबल बदलने की “झंझट” से बच जाएँगे।

टिप: ggplot2 की पूरी चीट शीट के लिए यहाँ जाएँ।

6. अपने R प्लॉट में ग्रिड कैसे बनाएं

बेसिक R के साथ अपने प्लॉट में ग्रिड बनाना

For some purposes, you might find it necessary to include a grid in your plot. You can easily add a grid to your plot by using the grid() function:
# Data
x <- c(1,2,3,4,5)
y <- 2*x

# Plot
plot(x,y)

# Grid
grid(10,10)

ggplot2 के साथ R प्लॉट में ग्रिड बनाना

नीचे दिया कोड ब्लॉक दिखाता है कि ggplot2 पैकेज से आप अपने प्लॉट में ग्रिड कैसे सुनिश्चित कर सकते हैं। ध्यान दें कि chol डेटा पहले से लोड है और ऊपर बताए गए डेटा मैनिपुलेशन स्टेप्स लागू हैं।

यदि आपको यक़ीन नहीं कि डेटा कैसा दिखता है, तो DataCamp Light ब्लॉक के R Console में डेटा फ़्रेम की शुरुआती पंक्तियाँ प्रिंट करें:

# Load in `ggplot2`
library(ggplot2)

# Plot data 
ggplot(chol, aes(x=chol$WEIGHT, y=chol$HEIGHT)) + 
  geom_point(aes(colour = factor(chol$MORT), shape = chol$SMOKE)) + 
  xlab("Weight") + 
  ylab("Height") + 
  theme(panel.grid.major = element_line(colour = "grey40"),
        panel.grid.minor = element_line(colour = "grey40"))

टिप: यदि आप minor grid lines नहीं चाहते, तो panel.grid.minor में element_blank() पास कर दें।

यदि आप बैकग्राउंड को किसी रंग से भरना चाहते हैं, तो अपने कोड में panel.background = element_rect(fill = "navy") जोड़ें। नीचे दिए DataCamp Light ब्लॉक में देखें। chol डेटा पहले जैसा ही लोड और थोड़ा मैनिपुलेट किया गया है। ख़ुद आज़माएँ!

library(ggplot2)
ggplot(chol, aes(x=chol$WEIGHT, y=chol$HEIGHT)) + 
  geom_point(aes(colour = factor(chol$MORT), shape = chol$SMOKER)) + 
  xlab("Weight") + 
  ylab("Height") + 
  scale_color_discrete(name ="Condition", labels=c("Alive", "Dead")) +
  scale_shape_discrete(name="Smoker", labels=c("Non-smoker", "Sigare", "Pipe" )) +
  theme(legend.position=c(1,0.5),
        legend.justification=c(1,1),
        panel.grid.major = element_line(colour = "grey40"),
        panel.grid.minor = element_line(colour = "grey40"),
        panel.background = element_rect(fill = "navy")
        )

7. PNG को बैकग्राउंड के रूप में लेकर प्लॉट कैसे बनाएं

आप png पैकेज की मदद से .png को बैकग्राउंड बनाकर तेज़ी से प्लॉट बना सकते हैं। यदि ज़रूरत हो तो पैकेज इंस्टॉल करें, library() से कार्यस्थान में सक्रिय करें और प्लॉटिंग शुरू करें!

सबसे पहले, आपको इमेज लोड करनी होगी। तस्वीर का पथ देने के लिए readPNG() फ़ंक्शन का उपयोग करें!

टिप: आप जहाँ आपका working directory सेट है, उसे जाँच और बदल सकते हैं; नीचे दिए कमांड चलाएँ।

यदि आपकी तस्वीर working directory में सेव है, तो आप पूरा पथ देने के बजाय सीधे readPNG("picture.png") लिख सकते हैं।

अगला, आप प्लॉट क्षेत्र सेट करते हैं और rasterImage() में ग्राफ़िकल पैरामीटर्स सेट करने के लिए par() कॉल करते हैं। आप usr आर्ग्यूमेंट से प्लॉटिंग क्षेत्र के यूज़र कोऑर्डिनेट्स के चरम बताते हैं। इस मामले में, आप 1, 3, 2 और 4 रखते हैं।

इसके बाद, आप ग्रिड बनाते हैं और कुछ लाइनें जोड़ते हैं:

install.packages("png")
library(png)
image <- readPNG("<path to your picture>")

getwd()
setwd("<path to a folder>")

plot(1:2, type='n', main="Plotting Over an Image", xlab="x", ylab="y")

lim <- par()

rasterImage(image, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])

grid()
lines(c(1, 1.2, 1.4, 1.6, 1.8, 2.0), c(1, 1.3, 1.7, 1.6, 1.7, 1.0), type="b", lwd=5, col="red")

यदि आप DataCamp लोगो का उपयोग करें, तो परिणाम कुछ ऐसा मिल सकता है:

library(png)
image <- readPNG("datacamp.png")
plot(1:2, type="n", main="Plotting Over an Image", xlab="x", ylab="y", asp=1)
lim <- par()
rasterImage(image, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
lines(c(1, 1.2, 1.4, 1.6, 1.8, 2.0), c(1.5, 1.3, 1.7, 1.6, 1.7, 1.0), type="b", lwd=5, col="red")

DataCamp लोगो पर प्लॉटिंग

ध्यान दें कि readPNG() को इनपुट में .png फ़ाइल देनी होती है और यदि आप चाहें, तो jpg इमेज के साथ भी यही प्रक्रिया अपना सकते हैं। बेशक, तब आपको jpeg लाइब्रेरी लोड करनी होगी!

# Download an image
download.file("https://holtzyan.files.wordpress.com/2015/10/cropped-p1090518.jpg" , destfile="my_image.jpg")

# Load `jpeg` 
library(jpeg)

# Read in the jpg file 
my_image=readJPEG("my_image.jpg")

# Set up a plot area with no plot
plot(1:2, type='n', main="", xlab="x", ylab="y")

# Get the plot information
lim <- par()
rasterImage(my_image, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
grid()

#Add your plot
lines(c(1, 1.2, 1.4, 1.6, 1.8, 2.0), c(1, 1.3, 1.7, 1.6, 1.7, 1.0), type="b", lwd=5, col="white")

ध्यान दें, इस उदाहरण में आप पहले वेब से एक इमेज डाउनलोड करते हैं और फिर उसे प्लॉट में उपयोग करते हैं, लेकिन आप किसी इमेज को सेव करके उसके फ़ाइल पाथ को readJPEG() में पास कर के भी उपयोग कर सकते हैं।

यह उदाहरण R Graph Gallery से लिया गया है, जो R डेटा विज़ुअलाइज़ेशन के लिए भरपूर प्रेरणा देता है—जहाँ कोड और रेंडरिंग एक ही जगह पर मिलते हैं! यदि आप प्रेरणा या अपने ग्राफ़्स के लिए ज़रूरी कोड जल्दी ढूँढना चाहते हैं, तो यह आदर्श है!

8. R प्लॉट में बिंदुओं का आकार कैसे समायोजित करें

बेसिक R में बिंदुओं का आकार समायोजित करना

To adjust the size of the points with basic R, you might just simply use the cex argument:
x <- c(1,2,3,4,5)
y <- c(6,7,8,9,10)

plot(x,y,cex=2,col="red")
याद रखें, हालाँकि, R आपको symbols() फ़ंक्शन से प्रतीकों पर और अधिक नियंत्रण देता है:
df <- data.frame(x1=1:10, 
                 x2=sample(10:99, 10), 
                 x3=10:1)
symbols(x=df$x1, 
        y=df$x2, 
        circles=df$x3, 
        inches=1/3, 
        ann=F, 
        bg="steelblue2", 
        fg=NULL)

इस प्लॉट के वृत्तों को df$x3 के मान radius के रूप में मिलते हैं, जबकि inches आर्ग्यूमेंट प्रतीकों के आकार को नियंत्रित करता है। जब यह आर्ग्यूमेंट धनात्मक संख्या लेता है, तो प्रतीकों को इस तरह स्केल किया जाता है कि सबसे बड़ा आयाम इंच में इस साइज का हो।

ggplot2 के साथ अपने R प्लॉट में बिंदुओं का आकार समायोजित करना

In this case, you will want to adjust the size of the points in your scatterplot. You can do this with the size argument. Note that the chol data has been loaded in for you:
library(ggplot2)

ggplot(chol, 
       aes(x=chol$WEIGHT, y=chol$HEIGHT), 
       size = 2) + 
  geom_point()

ध्यान दें कि आप size आर्ग्यूमेंट को geom_point() में भी पास कर सकते हैं:

ggplot(chol, 
       aes(x=chol$WEIGHT, y=chol$HEIGHT)) + 
  geom_point(size = 2)

आपको वही परिणाम मिलेगा, लेकिन मेरी बात पर भरोसा न करें! DataCamp Light ब्लॉक के अंतिम हिस्से के कोड को इस ब्लॉक से बदलकर देखें।

9. अपने R डेटा पर स्मूथ कर्व कैसे फिट करें

loess() फ़ंक्शन शायद इस तरह के सवालों के लिए हर R प्रोग्रामर का पसंदीदा हल है। यह वास्तव में “एक या अधिक संख्यात्मक प्रेडिक्टर्स द्वारा निर्धारित बहुपद सतह को स्थानीय फिटिंग के साथ फिट करता है।”

संक्षेप में, आपके पास आपका डेटा होता है और आप loess() का उपयोग करते हैं जिसमें आप y और x को संबद्ध करते हैं। इससे आप संख्यात्मक रेस्पॉन्स और एक से चार संख्यात्मक प्रेडिक्टर्स बताते हैं।

Next, you plot x and y and you plot lines in the original plot where you predicted the values of lo:
x <- 1:10
y <- c(2,4,6,8,7,12,14,16,18,20)

#Estimations between data
lo <- loess(y~x)

plot(x,y)

lines(predict(lo))

10. R प्लॉट में error bars कैसे जोड़ें

बेसिक R से error bars बनाना

बुरी ख़बर: R सीधे error bars नहीं खींचता। अच्छी ख़बर: आप बिना अतिरिक्त पैकेज इंस्टॉल किए फिर भी error bars खींच सकते हैं!

पहले, आपको अपने डेटा फ़्रेम के कुछ सारांश आँकड़े निकालने के लिए निम्न फ़ंक्शन परिभाषित करना होगा:

summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                      conf.interval=.95, .drop=TRUE) {
    library(plyr)

    # New version of length which can handle NA's: if na.rm==T, don't count them
    length2 <- function (x, na.rm=FALSE) {
        if (na.rm) sum(!is.na(x))
        else       length(x)
    }

    # This does the summary. For each group's data frame, return a vector with
    # N, mean, and sd
    datac <- ddply(data, groupvars, .drop=.drop,
      .fun = function(xx, col) {
        c(N    = length2(xx[[col]], na.rm=na.rm),
          mean = mean   (xx[[col]], na.rm=na.rm),
          sd   = sd     (xx[[col]], na.rm=na.rm)
        )
      },
      measurevar
    )

    # Rename the "mean" column    
    datac <- rename(datac, c("mean" = measurevar))

    datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean

    # Confidence interval multiplier for standard error
    # Calculate t-statistic for confidence interval: 
    # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
    ciMult <- qt(conf.interval/2 + .5, datac$N-1)
    datac$ci <- datac$se * ciMult

    return(datac)
}

ध्यान दें कि यह फ़ंक्शन परिभाषा इस स्रोत से आई है और Rmisc लाइब्रेरी से भी उपलब्ध है।

इसके बाद, आप इसका उपयोग chol डेटा (जो पहले से लोड है) के कुछ आँकड़े निकालने के लिए कर सकते हैं। फिर परिणामी संख्याओं से अपना प्लॉट बना सकते हैं।

अभ्यास शुरू करने से पहले डेटा ज़रूर जाँचें!

#Calculate some statistics for the `chol` dataset
cholc <- summarySE(chol, 
                   measurevar="CHOL", 
                   groupvars=c("MORT",
                               "SMOKER"))
#Plot the data
plot(cholc$N, 
     cholc$CHOL,
     ylim=range(c(cholc$CHOL-cholc$sd, 
                  cholc$CHOL+cholc$sd)),
     pch=19, 
     xlab="Cholesterol Measurements", 
     ylab="Cholesterol Mean +/- SD",
     main="Scatterplot With sd Error Bars"
)

#Draw arrows of a "special" type
arrows(cholc$N, 
       cholc$CHOL-cholc$sd, 
       cholc$N, 
       cholc$CHOL+cholc$sd, 
       length=0.05, 
       angle=90, 
       code=3)

यदि आप arrows() के सभी आर्ग्यूमेंट्स पर पढ़ना चाहते हैं, तो यहाँ जाएँ।

ggplot2 के साथ error bars बनाना

मीन के standard error दर्शाने वाले error bars

पहले summarySE() फ़ंक्शन (पैकेज Rmisc) या ऊपर परिभाषित फ़ंक्शन से अपने डेटा का सारांश निकालें। फिर, प्राप्त डेटा फ़्रेम से कुछ वेरिएबल्स प्लॉट करें और साथ ही उनके error bars भी बनाएं, जैसे कि मीन का standard error।

For convenience, the data as well as the function have been loaded in for you:

विषय