0% found this document useful (0 votes)
22 views24 pages

RCourse-Lecture49-Statistics-Graphics - Scatter Plot and Bar Plots

The document discusses different types of graphics and plots that can be created in R, including scatter plots, bar plots, and histograms. It provides examples of how to generate scatter plots and bar plots in R using the plot and barplot functions. The examples demonstrate how to customize bar plots by adding colors, titles, legends, subtitles, x-labels and y-labels.

Uploaded by

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

RCourse-Lecture49-Statistics-Graphics - Scatter Plot and Bar Plots

The document discusses different types of graphics and plots that can be created in R, including scatter plots, bar plots, and histograms. It provides examples of how to generate scatter plots and bar plots in R using the plot and barplot functions. The examples demonstrate how to customize bar plots by adding colors, titles, legends, subtitles, x-labels and y-labels.

Uploaded by

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

Foundations of R Software

Lecture 49
Graphics
:::
Scatter Plot and Bar Plots

Shalabh
Department of Mathematics and Statistics
Indian Institute of Technology Kanpur

1
Graphics:
Graphics summarize the information contained in a data.

For example, the mood of a person may be conveyed very easily


by the smilies:

They have an advantage that they convey the information hidden


inside the data more compactly

Appropriate number and choice of plots in analysis provides


better inferences. 2
Graphics:
Graphical tools‐ various type of plots
‐ 2D & 3D plots,
‐ scatter diagram
‐ Pie diagram
‐ Histogram
‐ Bar plot
‐ Stem and leaf plot
‐ Box plot ...

Appropriate number and choice of plots in analysis provides


better inferences.
3
Graphics:
In R, Such graphics can be easily created and saved in various
formats.
o Bar plot
o Pie chart
o Box plot
o Grouped box plot
o Scatter plot
o Histogram
o Various 3 dimensional plots
o …
4
Scatter Plot
Plot command for one variable:
x: Data vector
plot(x)

5
Scatter Plot
Example
Height of 50 persons are recorded in centimeters as follows:
166,125,130,142,147,159,159,147,165,156,149,164,137,166,135,142,
133,136,127,143,165,121,142,148,158,146,154,157,124,125,158,159,
164,143,154,152,141,164,131,152,152,161,143,143,139,131,125,145,
140,163
> height = c(166,125,130,142,147,159,159,147,
165,156,149,164,137,166,135,142,133,136,127,143,
165,121,142,148,158,146,154,157,124,125,158,159,
164,143,154,152,141,164,131,152,152,161,143,143,
139,131,125,145,140,163)
6
Scatter Plot
Example
plot(height)

7
Scatter Plot
Example
plot(height, col = "red")

8
Bar plots:
Visualize the relative or absolute frequencies of observed values
of a variable.

Used for categorical variables only.

It consists of one bar for each category.

The height of each bar is determined by either the absolute


frequency or the relative frequency of the respective category
and is shown on the y‐axis.

9
Bar plots:
Visualize the relative or absolute frequencies of observed values
of a variable.

barplot(x, width = 1, space = NULL,…)

Bar plot with absolute frequencies

barplot(table(x)) # Absolute frequencies

Bar plot with relative frequencies

barplot(table(x)/length(x))

10
Bar plots:
> help("barplot")

barplot(height, width = 1, space = NULL,


names.arg = NULL, legend.text = NULL, beside
= FALSE, horiz = FALSE, density = NULL, angle
= 45, col = NULL, border = par("fg"), main =
NULL, sub = NULL, xlab = NULL, ylab = NULL,
xlim = NULL, ylim = NULL, xpd = TRUE, log =
"", axes = TRUE, axisnames = TRUE, cex.axis =
par("cex.axis"), cex.names = par("cex.axis"),
inside = TRUE, plot = TRUE, axis.lty = 0,
offset = 0, add = FALSE, args.legend = NULL,
...)

11
Bar plots:
Example:
Code the 10 persons by using, say 1 for male (M) and 2 for female (F).
M, F, M, F, M, M, M, F, M, M
1, 2, 1, 2, 1, 1, 1, 2, 1, 1

> gender = c(1, 2, 1, 2, 1, 1, 1, 2, 1, 1)


> gender
[1] 1 2 1 2 1 1 1 2 1 1

12
Bar plots:
Example:

> barplot(gender)

Do you want this?

13
Bar plots:
Example:

> table(gender)
gender
1 2
7 3

> barplot(table(gender))

14
Bar plots:
Example:
> table(gender)/length(gender)
gender
1 2
0.7 0.3

> barplot(table(gender)/length(gender))
15
Bar plots:
Example:
Consider the following data on pizza home delivery. There are three
branches (East ‐ coded as 1, West ‐ coded as 2, Central ‐ coded as 3)
of the restaurant.
The 100 values from code Directions are as follows:

direction =
c(1,1,2,1,2,3,2,2,3,3,3,1,2,3,2,2,3,1,1,3,3,1,2
,1,3,3,3,2,2,2,2,1,2,2,1,1,1,3,2,2,1,2,3,2,2,1,
2,3,3,2,1,2,2,3,1,1,2,1,2,3,2,3,2,2,3,1,2,3,3,3
,2,1,1,1,2,1,1,2,1,2,3,3,1,2,3,3,2,1,2,3,2,1,3,
2,2,2,2,3,2,2)

16
Bar plots:
Example:

barplot(direction)

Do you want this?

17
Bar plots:
Example
barplot(table(direction))

18
Bar plots:
Example
barplot(table(direction)/length(direction))

19
Bar plots:
Example: Adding colours
barplot(table(direction), col=c("red", "green",
"blue") )

20
Bar plots:
Example: Adding title
barplot(table(direction), col=c("red", "green",
"blue"), main="Directions of food delivery" )

21
Bar plots:
Example: Adding legends
barplot(table(direction), col=c("red", "green",
"blue"), main="Directions of food delivery",
legend.text=c("dir1", "dir2", "dir3") )

22
Bar plots:
Example: Adding Subtitle
barplot(table(direction), col=c("red", "green",
"blue"), main="Directions of food delivery",
legend.text=c("dir1", "dir2", "dir3"),
sub="Three directions" )

23
Bar plots:
Example: Adding Subtitle
barplot(table(direction), col=c("red", "green",
"blue"), main="Directions of food delivery",
legend.text=c("dir1", "dir2", "dir3"),
sub="Three directions", xlab="Food Delivery
Directions", ylab="Number of Deliveries" )

24

You might also like