आर में एक ही चार्ट में एकाधिक लाइनें (डेटा श्रृंखला) कैसे प्लॉट करें


यह ट्यूटोरियल बताता है कि आर में एक ही चार्ट में एकाधिक लाइनें (यानी डेटा श्रृंखला) कैसे प्लॉट करें।

ग्राफ़ में एकाधिक पंक्तियाँ प्लॉट करने के लिए, हम या तो आधार R का उपयोग कर सकते हैं या ggplot2 जैसा अधिक परिष्कृत पैकेज स्थापित कर सकते हैं।

बेसआर का उपयोग करना

बेस आर का उपयोग करके एक ही ग्राफ़ में एकाधिक पंक्तियों को कैसे प्लॉट किया जाए, इसके दो उदाहरण यहां दिए गए हैं।

उदाहरण 1: मैटप्लॉट का उपयोग करना

यदि आपके पास एक बड़े प्रारूप वाला डेटासेट है, तो ग्राफ़ में एकाधिक पंक्तियों को प्लॉट करने का एक आसान तरीका matplot का उपयोग करना है:

 #Create a fake dataset with 3 columns (ncol=3) composed of randomly generated
#numbers from a uniform distribution with minimum = 1 and maximum = 10
data <- matrix(runif(30,1,10), ncol=3)
data
        [,1] [,2] [,3]
#[1,] 5.371653 3.490919 3.953603
#[2,] 9.551883 2.681054 9.506765
#[3,] 3.525686 1.027758 8.059011
#[4,] 9.923080 1.337935 1.112361
#[5,] 7.273972 7.627546 1.174340
#[6,] 8.859109 3.778144 9.384526
#[7,] 9.614542 3.866029 7.301729
#[8,] 9.288085 5.804041 8.347907
#[9,] 1.696849 4.650687 7.220209
#[10,] 5.820941 4.799682 5.243663

#plot the three columns of the dataset as three lines and add a legend in
#the top right corner of the chart
matplot(data, type = "b",pch=1,col = 1:3)
legend("topright", legend = 1:3, col=1:3, pch=1)

यह कोड निम्नलिखित ग्राफ उत्पन्न करता है:

उदाहरण 2: बिंदुओं और रेखाओं का उपयोग करना

कई रेखाएँ खींचने का दूसरा तरीका अंतर्निहित आर फ़ंक्शंस पॉइंट() और लाइन्स() का उपयोग करके उन्हें एक बार में खींचना है। नीचे दिया गया कोड इस दृष्टिकोण का एक उदाहरण दिखाता है:

 #generate an x-axis along with three data series
x <- c(1,2,3,4,5,6)
y1 <- c(2,4,7,9,12,19)
y2 <- c(1,5,9,8,9,13)
y3 <- c(3,6,12,14,17,15)

#plot the first data series using plot()
plot(x, y1, type="o", col="blue", pch="o", ylab="y", lty=1)

#add second data series to the same chart using points() and lines()
points(x, y2, col="red", pch="*")
lines(x, y2, col="red",lty=2)

#add third data series to the same chart using points() and lines()
points(x, y3, col="dark red",pch="+")
lines(x, y3, col="dark red", lty=3)

#add a legend in top left corner of chart at (x, y) coordinates = (1, 19)
legend(1,19,legend=c("y1","y2","y3"), col=c("blue","red","black"),
                                   pch=c("o","*","+"),lty=c(1,2,3), ncol=1)

यह कोड निम्नलिखित ग्राफ उत्पन्न करता है:

Ggplot2 का उपयोग करना

यहां ggplot2 का उपयोग करके एक ही ग्राफ़ में एकाधिक पंक्तियों को प्लॉट करने का एक उदाहरण दिया गया है।

 #install (if not already installed) and load ggplot2 package
if(!require(ggplot2)){install.packages(' ggplot2 ')}

#generate fake dataset with three columns 'x', 'value', and 'variable'
data <- data. frame (x=rep(1:5, 3),
                   value=sample(1:100, 15), 
                   variable=rep(paste0(' series ', 1:3), each=5))

#view dataset
head(data)
  x value variable
1 1 93 series1
2 2 64 series1
3 3 36 series1
4 4 17 series1
5 5 95 series1
6 1 80 series2

#plot all three series on the same chart using geom_line()
ggplot(data = data, aes(x=x, y=value)) + geom_line(aes(color=variable))

यह निम्नलिखित ग्राफ उत्पन्न करता है:

अतिरिक्त संसाधन

निम्नलिखित ट्यूटोरियल बताते हैं कि ggplot2 में अन्य सामान्य प्लॉटिंग ऑपरेशन कैसे करें:

Ggplot2 में लेजेंड स्थिति कैसे बदलें
Ggplot2 में लेजेंड शीर्षक कैसे बदलें
Ggplot2 में मार्जिन कैसे बदलें

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

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