0% found this document useful (0 votes)
4 views37 pages

Lecture 15 R

The document provides a comprehensive guide on creating various base R plots for both quantitative and qualitative data, including histograms, boxplots, scatter plots, and bar charts. It emphasizes the importance of selecting appropriate graphical representations based on data types and research objectives. Additionally, it includes practical exercises to reinforce the application of these plotting techniques using R programming.
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)
4 views37 pages

Lecture 15 R

The document provides a comprehensive guide on creating various base R plots for both quantitative and qualitative data, including histograms, boxplots, scatter plots, and bar charts. It emphasizes the importance of selecting appropriate graphical representations based on data types and research objectives. Additionally, it includes practical exercises to reinforce the application of these plotting techniques using R programming.
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

14 Base R plots Quantitative data Qualitative data

Training on Statistical Tools for Research: R


Graphical Presentation of Data in R

Md Monowar Hossain

Institute of Statistical Research and Training (ISRT)


University of Dhaka
mmhossain1@[Link]
[Link]

February 02, 2025

Md Monowar Hossain February 02, 2025 1 / 37


14 Base R plots Quantitative data Qualitative data

14 Base R plots

Md Monowar Hossain February 02, 2025 2 / 37


14 Base R plots Quantitative data Qualitative data

Graphical presentations in Statistics

• Graphs are helpful for presenting a summary of data and


results of a statistical analysis
• John W. Tukey, the father of exploratory data analysis
once said:
The greatest value of a picture is when it forces us to
notice what we never expected to see.

Md Monowar Hossain February 02, 2025 3 / 37


14 Base R plots Quantitative data Qualitative data

Graphical presentations of data

• There are several graphs available to use for describing


data, and the selection of the most appropriate graph
depends on the data type and the research objectives
• Quantitative data
• Histogram
• Boxplot
• Scatter plot
• Qualitative data
• Bar chart
• Pie chart

Md Monowar Hossain February 02, 2025 4 / 37


14 Base R plots Quantitative data Qualitative data

• Bivariate analysis involves two variables, depending on


the combinations of the variables, i.e., qualitative or
quantitative, there are different ways of presenting data
graphically
• Quantitative–qualitative combination → Histogram and
boxplot can be used for different levels of a qualitative
variable
• Quantitative–quantitative and qualitative–qualitative
combinations → Bar chart and scatter plot can be used

Md Monowar Hossain February 02, 2025 5 / 37


14 Base R plots Quantitative data Qualitative data

Quantitative data

Md Monowar Hossain February 02, 2025 6 / 37


14 Base R plots Quantitative data Qualitative data

1 Histogram

• The function hist() is used to obtain a histogram of a


quantitative variable
• Syntax of hist():

hist(x, . . . )

Md Monowar Hossain February 02, 2025 7 / 37


14 Base R plots Quantitative data Qualitative data

library(palmerpenguins)
data(penguins)
# x is a quantitative vector
hist(x = penguins$body_mass_g)

Histogram of penguins$body_mass_g
80
60
Frequency

40
20
0

3000 4000 5000 6000

penguins$body_mass_g

Md Monowar Hossain February 02, 2025 8 / 37


14 Base R plots Quantitative data Qualitative data

Histogram of penguins$body_mass_g
• Some useful arguments of hist() : xlab, main,
probability, etc.

Md Monowar Hossain February 02, 2025 9 / 37


14 Base R plots Quantitative data Qualitative data

hist(
x = penguins$body_mass_g,
xlab = "Body mass",
main = "Histogram of penguins' body mass"
)

Histogram of penguins' body mass


80
60
Frequency

40
20
0

3000 4000 5000 6000

Body mass

Md Monowar Hossain February 02, 2025 10 / 37


14 Base R plots Quantitative data Qualitative data

hist(
x = penguins$body_mass_g,
xlab = "Body mass",
main = "Histogram of penguins' body mass",
breaks = 20,
probability = T
)

Histogram of penguins' body mass


0e+00 2e−04 4e−04 6e−04
Density

3000 4000 5000 6000

Body mass
Md Monowar Hossain February 02, 2025 11 / 37
14 Base R plots Quantitative data Qualitative data

hist(
x = penguins$body_mass_g,
xlab = "Body mass",
main = "Histogram of penguins' body mass",
breaks = 20,
probability = T
)

lines(
density(x = penguins$body_mass_g, [Link] = T),
lwd = 3, col = "brown"
)

Md Monowar Hossain February 02, 2025 12 / 37


14 Base R plots Quantitative data Qualitative data

Histogram of penguins' body mass

0e+00 2e−04 4e−04 6e−04


Density

3000 4000 5000 6000

Body mass

Md Monowar Hossain February 02, 2025 13 / 37


14 Base R plots Quantitative data Qualitative data

Exercise 3.1.1
(use mtcars data frame to answer the followings)
• Create a histogram of mpg with appropriate labels
• Add density line to the plot obtained in Question 1.

Md Monowar Hossain February 02, 2025 14 / 37


14 Base R plots Quantitative data Qualitative data

2 Boxplot

• Boxplot is a useful graphical tool that can be used to


compare distribution of a quantitative variable at different
levels of a qualitative variable
E.g. examine the distribution of body mass over different
species of penguins

Md Monowar Hossain February 02, 2025 15 / 37


14 Base R plots Quantitative data Qualitative data

• boxplot() function can be used for both univariate and


bivariate analysis
• boxplot(x) → single quantitative vector
• boxplot(formula, data) → bivariate analysis, where
formula = quant_var ~ qual_var
data = data frame containing both variables

Md Monowar Hossain February 02, 2025 16 / 37


14 Base R plots Quantitative data Qualitative data

boxplot(formula = body_mass_g ~ species, data = penguins)

6000
body_mass_g

5000
4000
3000

Adelie Chinstrap Gentoo

species

Md Monowar Hossain February 02, 2025 17 / 37


14 Base R plots Quantitative data Qualitative data

boxplot(formula = body_mass_g ~ species, data = penguins)

6000
body_mass_g

5000
4000
3000

Adelie Chinstrap Gentoo

species

Md Monowar Hossain February 02, 2025 18 / 37


14 Base R plots Quantitative data Qualitative data

Exercise 3.1.2
(use mtcars data frame to answer the followings)
• Create a boxplot of qsec with appropriate labels
• Create a boxplot of mpg to compare its distribution at
different levels of cyl

Md Monowar Hossain February 02, 2025 19 / 37


14 Base R plots Quantitative data Qualitative data

3 Scatter plot

• The function plot(x, y) is used to obtain a scatter plot of


two quantitative variables
• Useful arguments: xlab, ylab, main, pch, cex, col

Md Monowar Hossain February 02, 2025 20 / 37


14 Base R plots Quantitative data Qualitative data

plot(x = penguins$bill_length_mm,
y = penguins$flipper_length_mm)

230
penguins$flipper_length_mm

210
190
170

35 40 45 50 55 60

penguins$bill_length_mm

Md Monowar Hossain February 02, 2025 21 / 37


14 Base R plots Quantitative data Qualitative data

plot(x = penguins$bill_length_mm,
y = penguins$flipper_length_mm,
xlab = "Flipper length")
230
penguins$flipper_length_mm

210
190
170

35 40 45 50 55 60

Flipper length

Md Monowar Hossain February 02, 2025 22 / 37


14 Base R plots Quantitative data Qualitative data

plot(x = penguins$flipper_length_mm,
y = penguins$bill_length_mm,
xlab = "Flipper length",
pch = 20,
cex = 1.5,
col = "brown")
60
penguins$bill_length_mm

55
50
45
40
35

170 180 190 200 210 220 230

Flipper length

Md Monowar Hossain February 02, 2025 23 / 37


14 Base R plots Quantitative data Qualitative data

Scatter plot with a linear model fit

plot(x = penguins$flipper_length_mm,
y = penguins$bill_length_mm,
xlab = "Flipper length",
pch = 20,
cex = 1.5,
col = "brown")

mod1 <- lm(bill_length_mm ~ flipper_length_mm, data = pengu


abline(mod1, col = "blue", lwd = 4)

Md Monowar Hossain February 02, 2025 24 / 37


14 Base R plots Quantitative data Qualitative data

Scatter plot with a linear model fit

60
penguins$bill_length_mm

55
50
45
40
35

170 180 190 200 210 220 230

Flipper length

Md Monowar Hossain February 02, 2025 25 / 37


14 Base R plots Quantitative data Qualitative data

Exercise 3.1.3
(use mtcars data frame to answer the followings)
• Create a scatter plot to examine the association between
mpg and disp
• Add the fit of a linear regression model mpg ~ disp to the
plot

Md Monowar Hossain February 02, 2025 26 / 37


14 Base R plots Quantitative data Qualitative data

Qualitative data

Md Monowar Hossain February 02, 2025 27 / 37


14 Base R plots Quantitative data Qualitative data

4 Bar chart

• Bar chart is used to examine the distribution of a


qualitative variable
• barplot(height, ...) → height represents frequency
• table() function takes a qualitative variable and returns
frequencies

table(penguins$species)

##
## Adelie Chinstrap Gentoo
## 152 68 124

Md Monowar Hossain February 02, 2025 28 / 37


14 Base R plots Quantitative data Qualitative data

barplot(height = table(penguins$species))

140
100
60
0 20

Adelie Chinstrap Gentoo

Md Monowar Hossain February 02, 2025 29 / 37


14 Base R plots Quantitative data Qualitative data

Exercise 3.1.4
(use mtcars data frame to answer the followings)
• Create a bar chart of cyl

Md Monowar Hossain February 02, 2025 30 / 37


14 Base R plots Quantitative data Qualitative data

Bivariate analysis

• The function par() has many arguments to produce


high-quality graphs
• mfrow argument splits figure layout into rows and columns
Example: mfrow = c(2, 3) → 2 rows × 3 columns

Md Monowar Hossain February 02, 2025 31 / 37


14 Base R plots Quantitative data Qualitative data

par(mfrow = c(1, 3))


hist(x = penguins$bill_length_mm[penguins$species == "Adeli
hist(x = penguins$bill_length_mm[penguins$species == "Gento
hist(x = penguins$bill_length_mm[penguins$species == "Chins

Histogram of penguins$bill_length_mm[penguins$species
Histogram of penguins$bill_length_mm[penguins$species
Histogram
== "Adelie"]
of penguins$bill_length_mm[penguins$species
== "Gentoo"] == "Chinstrap"]

30

20
25
30

15
20
Frequency

Frequency

Frequency
20

15

10
10
10

5
5
0

32 36 40 44 40 45 50 55 60 0 40 45 50 55

penguins$bill_length_mm[penguins$species
penguins$bill_length_mm[penguins$species
== "Adelie"] penguins$bill_length_mm[penguins$species
== "Gentoo"] == "Chinstrap"]

Md Monowar Hossain February 02, 2025 32 / 37


14 Base R plots Quantitative data Qualitative data

Exercise 3.1.5

• Association between bill and flipper lengths by species

par(mfrow = c(1, 3))


plot(bill_length_mm ~ flipper_length_mm,
data = subset(penguins, species == "Adelie"),
main = "Adelie", pch = 19, col = "blue")
plot(bill_length_mm ~ flipper_length_mm,
data = subset(penguins, species == "Gentoo"),
main = "Gentoo", pch = 19, col = "red")
plot(bill_length_mm ~ flipper_length_mm,
data = subset(penguins, species == "Chinstrap"),
main = "Chinstrap", pch = 19, col = "green")

Md Monowar Hossain February 02, 2025 33 / 37


14 Base R plots Quantitative data Qualitative data

Exercise 3.1.5

Adelie Gentoo Chinstrap

60
46
44

55
55
42
bill_length_mm

bill_length_mm

bill_length_mm
40

50
50
38
36

45
45
34
32

180 190 200 210 205 215 225 180 190 200 210

flipper_length_mm flipper_length_mm flipper_length_mm

Md Monowar Hossain February 02, 2025 34 / 37


14 Base R plots Quantitative data Qualitative data

Pie chart

pie(table(penguins$species))

Adelie

Chinstrap

Gentoo

Md Monowar Hossain February 02, 2025 35 / 37


14 Base R plots Quantitative data Qualitative data

Contact Information
• Email: mmhossain1@[Link]
• Website: [Link]

Md Monowar Hossain February 02, 2025 36 / 37


14 Base R plots Quantitative data Qualitative data

THANK YOU

Md Monowar Hossain February 02, 2025 37 / 37

You might also like