Professional Documents
Culture Documents
Fda 20brs1053 Lab10
Fda 20brs1053 Lab10
PART A
Use the newsurvey data obtained by cleaning ‘na’ values in survey data of MASS package to
do the following:
1. Plot a bar graph for the number of male and female participants
in the survey. Provide the title as “Male and Female
participants”, y-axis label as “frequency” and specify the
colours for the bars.
2. Plot a bar graph for the number of left handers and right handers
in the survey. Provide the title as “Left Handers and Right
Handers”, y-axis label as “count” and specify the colours for the
bars.
3. Plot the distribution between male left handers and female left
handers using bar chart. Provide the title as “Female Left
Handers and Male Left Handers , y-axis label as “count” and
specify the colours for the bars.
6. Reveal the relationship between the age and writing hand span
using scatter plot.
7. Draw the boxplot for pulse rate to analyse the five summary
statistics. Provide appropriate title and label.
library(MASS)
library(datasets)
new <- na.exclude(survey)
new
1. Plot a bar graph for the number of male and female participants in
the survey. Provide the title as “Male and Female participants”, y-axis
label as “frequency” and specify the colours for the bars.
attach(new)
gender.count<-table(new$Sex)
barplot(gender.count, names.arg=c("Male", "Female"),
main ='Male and Female participants', xlab='Gender',
ylab='Count')
2. Plot a bar graph for the number of left handers and right handers in
the survey. Provide the title as “Left Handers and Right Handers”, y-
axis label as “count” and specify the colours for the bars.
hand.count <- table(new$W.Hnd)
barplot(hand.count,
names.arg = c("Left", "Right"),
main = "Left Handers and Right Handers", xlab = "Hand", ylab =
"Count"
)
3. Plot the distribution between male left handers and female left
handers using bar chart. Provide the title as “Female Left Handers and
Male Left Handers , y-axis label as “count” and specify the colours for
the bars.
leftHand <- subset(new, W.Hnd == "Left")
leftHand.count <- table(leftHand$Sex)
barplot(leftHand.count,
names.arg = c("Male", "Female"),
main = "Female Left Handers and Male Left Handers", xlab=
"Gender", ylab = "Count"
)
4. Draw the distribution of smoking habits of male left handers using
pie chart.
leftHand.smoke <- subset(leftHand, Smoke == "Heavy" || Smoke ==
"Occas" || Smoke == "Regul")
6. Reveal the relationship between the age and writing hand span
using scatter plot.
plot(new$Age, new$Wr.Hnd,
main = "Age vs Writing Hand Span", xlab = "Age", ylab = "Writing
Hand Span"
)
7. Draw the boxplot for pulse rate to analyse the five summary
statistics. Provide appropriate title and label.
boxplot(new$Pulse,
main = "Pulse Rate", xlab = "Pulse", ylab = "Rate"
)
PART B
Use the newsurvey data obtained by cleaning ‘na’ values in survey data of MASS package
and ggplot2 package to do the following:
1. Install the package ggplot2 and import it.
2. Plot a bar graph for the number of male and female participants
in the survey. Provide the title as “Male and Female
participants” and specify the colours for the bars.
3. Plot a bar graph for the number of left handers and right handers
in the survey. Provide the title as “Left Handers and Right
Handers” and specify the colours for the bars.
4. Plot the distribution between male left handers and female left
handers using bar chart. Provide the title as “Female Left
Handers and Male Left Handers” and specify the colours for the
bars.
7. Reveal the relationship between the age and writing hand span
using scatter plot.
2. Plot a bar graph for the number of male and female participants in
the survey. Provide the title as “Male and Female participants” and
specify the colours for the bars.
library(ggplot2)
ggplot(new, aes(x=as.factor(Sex))) +
geom_bar(color="blue", fill=rgb(0.1,0.4,0.5,0.7) )
3. Plot a bar graph for the number of left handers and right handers in
the survey. Provide the title as “Left Handers and Right Handers” and
specify the colours for the bars.
ggplot(new, aes(x = as.factor(W.Hnd))) +
geom_bar(color = "blue", fill = rgb(0.1, 0.4, 0.5, 0.7))
4. Plot the distribution between male left handers and female left
handers using bar chart. Provide the title as “Female Left Handers and
Male Left Handers” and specify the colours for the bars.
leftHand <- subset(new, W.Hnd == "Left")
leftHand.count <- table(leftHand$Sex)
ggplot(leftHand, aes(x = as.factor(Sex))) +
geom_bar(color = "blue", fill = rgb(0.1, 0.4, 0.5, 0.7))
5. Draw the distribution of smoking habits of male left handers based
on age using pie chart.
ggplot(leftHand, aes(x = as.factor(Smoke))) +
geom_bar(color = "blue", fill = rgb(0.1, 0.4, 0.5, 0.7)) +
facet_wrap(~Age) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(title = "Distribution of smoking among male left handers
based on age", x = "Smoke", y = "Count")
6. Draw the histogram of age distribution with the title as ‘Age
distribution’ and xlabel as ‘Age range’ and ylabel as ‘frequency’.
ggplot(new, aes(x = Age)) +
geom_histogram(color = "blue", fill = rgb(0.1, 0.4, 0.5, 0.7)) +
labs(title = "Age distribution", x = "Age range", y = "Frequency")
+
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_x_continuous(breaks = seq(0, 100, 10)) +
scale_y_continuous(breaks = seq(0, 100, 10)) +
geom_vline(xintercept = mean(new$Age), color = "red", linetype =
"dashed", size = 1) +
geom_vline(xintercept = median(new$Age), color = "green", linetype
= "dashed", size = 1) +
geom_vline(xintercept = quantile(new$Age, 0.25), color = "yellow",
linetype = "dashed", size = 1) +
geom_vline(xintercept = quantile(new$Age, 0.75), color = "yellow",
linetype = "dashed", size = 1) +
annotate("text", x = mean(new$Age), y = 10, label = "Mean", color
= "red") +
annotate("text", x = median(new$Age), y = 10, label = "Median",
color = "green") +
annotate("text", x = quantile(new$Age, 0.25), y = 10, label =
"Q1", color = "yellow") +
annotate("text", x = quantile(new$Age, 0.75), y = 10, label =
"Q3", color = "yellow")