0% found this document useful (0 votes)
47 views3 pages

2 R-Graphics

This document provides examples of creating bar charts and pie charts in R. It demonstrates how to create bar charts from numeric vectors and matrices, including adding titles, labels, and arranging bars beside each other or in stacks. It also shows how to create a pie chart from a vector of values and labels, and how to set the chart title and slice colors. Categorical data can be visualized using bar charts or pie charts showing counts or relative frequencies of each category level.

Uploaded by

lamaram32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

2 R-Graphics

This document provides examples of creating bar charts and pie charts in R. It demonstrates how to create bar charts from numeric vectors and matrices, including adding titles, labels, and arranging bars beside each other or in stacks. It also shows how to create a pie chart from a vector of values and labels, and how to set the chart title and slice colors. Categorical data can be visualized using bar charts or pie charts showing counts or relative frequencies of each category level.

Uploaded by

lamaram32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Bar Chart

Creating Bar Chart of Numeric Vector

> aa=c(46, 76, 37, 86, 46, 75)

> barplot(aa)

To give title and sub-title to the graph created above

> barplot(aa, main = "Nightingale", sub = "2017")

To give titles to horizontal and vertical axes

> barplot(aa, main = "Nightingale", sub = "2017", xlab = "Districts", ylab = "Production")

Creating Bar Chart of Matrix

If bar plot of 'aa' created above corresponds to production of cereal in six districts say, "Kavre", "Kaski",
"Palpa", "Nuwakot", "Dhading", "Morang", then by using bar graph of vector, as created above, it is not
possible to provide names of different districts to different rectangles of bar graph. But if above values
are stored as matrix of one dimension, then by using 'dimnames' attribute it will be possible.

Ex. I

> aa = c(46, 76, 37, 86, 46, 75)

> bb = matrix(aa, nrow = 1, ncol=6, dimnames = list("District", c("Kavre", "Kaski", "Palpa", "Nuwakot",
"Dhading", "Morang")))

> barplot(bb)

Ex. II

For following matrix

To create stacked bar graph of marks in three subjects


> mm1 = matrix(c(34, 57, 54,76, 57, 87), nrow=2, ncol=3, dimnames = list(c("Rajan", "Hari"), c("Math",
"Science", "Computer")))

> barplot(mm1)

To create bar chart of above matrix with rectangles of each subject adjacent

> barplot(mm1, beside = TRUE)

To specify title and subtitle

> barplot(mm1, beside = TRUE, main = "Marks", sub = "Nightingale")

To provide titles for axes

Use 'xlab' and 'ylab'

For Categorical Data

For visualization of categorical data bar chart and pie chart can be used.

In following table, we can use bar chart and pie chart to visualize count of 'male' and 'female' in 'gender'
variable. It can also be used in 'caste' variable.

In the same way, bar chart and pie chart can also be used to visualize relative frequency or percentage of
different levels in categorical data.

Exm. To create bar plot for count of different levels in a factor

> bg = factor(c("A","B","A","AB","A","O","O","A","AB","B","B"))

Pie Chart

What function is used create pie chart? Ans.


What is the syntax of function used to create pie chart? Ans.
How to create a pie chart with four values 72, 65, 34, 89? Ans.
How to create above chart with four labels- New York, Beijing, New Delhi, Kathmandu? Ans.
How to give title "City Pie Chart" to above chart? Ans.
How to set different colors for slices of above piechart? Later

You might also like