R Language ये ज्यादातर graphics के बारे में प्रसिद्ध है | bar graph ये इसका बहुत ही अच्छा उदाहरण है |
bar graph ये rectangular bars में data का वर्णन करता है | R में bar graph को horizontal या vertical bars में represent किया जा सकता है |
R में bar graph के लिए barplot() का इस्तेमाल किया जाता है |
Note : barplot() ये function vector में negative numbers भी लेता है |
Syntax for R Bar Chart(barplot())
barplot(vector/matrix, names.arg, xlab, ylab, main, horiz, col, border, beside, xlim, ylim)
Parameter for barplot() Function in R
vector/matrix : यहाँ पर numeric vector या matrix दिया जाता है |
names.arg : Optional. यहाँ पर हर bar को नाम देने के लिए vector दिया जाता है |
xlab : Optional.ये x-axis के लिए label होता है |
ylab : Optional.ये y-axis के लिए label होता है |
main : Optional.ये bar graph के लिए title होता है |
horiz : Optional.अगर bar horizontal चाहते हो तो यहाँ पर 'TRUE' दिया जाता है |
col : Optional.यहाँ पर bars के लिए color name दिया जाता है |
border : Optional.यहाँ पर bars के लिए border color name दिया जाता है |
beside : Optional. इसकी value अगर TRUE दी जाती है तो grouped bar chart display होता है, अगर FALSE होता है तो stacked bar chart display होता है |
xlim : Optional. यहाँ पर x-axis की limit को बढाया या घटाया जाता है | default ये limit bars के अनुसार set हो जाती है |
ylim : Optional. यहाँ पर y-axis की limit को बढाया या घटाया जाता है | default ये limit bars के अनुसार set हो जाती है |
Simple Example for barplot() Function in R
Output:> vec = c(25,10,15,35,5) > barplot(vec)

Add Title and Labels on Bar in R
Example पर bar chart को x-label, y-label और title दिया गया है |
Output:> vec = c(25,10,15,35,5) > barplot(vec, xlab='Student Name', ylab='Marks', main='Student Marks', col='red')

Named Bar in R
Example पर हर column/bar को seperate नाम दिया गया है |
Output:> vec = c(25,10,15,35,5) > stud_name = c('Rakesh','Ramesh','Raj','Vaibhav', 'Hrishikesh') > barplot(vec, names.arg = stud_name, xlab='Student Name', ylab='Marks', main='Student Marks', col='purple')

Bar With coloured border in R
यहाँ पर bars को coloured border दी गयी है |
Output:> barplot(vec, names.arg = stud_name, xlab='Student Name', ylab='Marks', main='Student Marks', col='white', border='red')

Create Stacked Bar Chart using Matrix in R
Example पर stacked bar chart को create किया गया है | इस bar chart में legend का होना बहुत ही जरुरी होता है इसके साथ-साथ matrix का इस्तेमाल किया जाता है | legend को create करने के लिए legend() function का इस्तेमाल किया जाता है |
Output:col = c("red","green","blue") stud_name = c("Rakesh","Mukesh","Raghav","Ramesh") sub = c("Physics","Chemistry","Maths") marks = matrix(c(32,28,45,26,41,35,49,42,29,35,44,49),nrow = 3,ncol = 4,byrow = TRUE) barplot(marks, main="Student Marks", names.arg=stud_name, xlab="Student Name", ylab="Marks", col = col) # legend for stacked bar chart. legend("topleft", sub, fill = col)

Create Grouped Bar Chart using Matrix in R
Example पर beside इस barplot() function के parameter की मदद से grouped bar chart को display किया गया है |
Output:col = c("red","green","blue") stud_name = c("Rakesh","Mukesh","Raghav","Ramesh") sub = c("Physics","Chemistry","Maths") marks = matrix(c(32,28,45,26,30,35,49,42,29,35,44,49),nrow = 3,ncol = 4,byrow = TRUE) barplot(marks, main="Student Marks", names.arg=stud_name, xlab="Student Name", ylab="Marks", col = col, beside=TRUE) # legend for grouped bar chart. legend("topleft", sub, fill = col)

Increase y-axis limit using ylim parameter
Example पर y-axis की limit को बढाया गया है | इस y-axis की limit को बढाने के लिए barplot() के ylim इस parameter का इस्तेमाल किया जाता है | जरुरत पर xlim(x-axis limit) को भी set किया जा सकता है |
col = c("red","green","blue")
stud_name = c("Rakesh","Mukesh","Raghav","Ramesh")
sub = c("Physics","Chemistry","Maths")
marks = matrix(c(49,28,45,26,45,35,25,30,29,35,44,30),nrow = 3,ncol = 4,byrow = TRUE)
barplot(marks, main="Student Marks", ylim=c(0,65), names.arg=stud_name, xlab="Student Name", ylab="Marks", col = col, beside=TRUE)
# legend for stacked bar chart.
legend(x="topleft", sub, fill = col)
Output:
legend() function के बारे में अधिक जानने के लिए यहाँ पर दबाये |