You are on page 1of 13

LAB - 10

Name: Sam Melvin M Course: FDA

Reg no: 19MIS1106 Course Code: CSE3505

----------------------------------------------

PART A
library(MASS)
data(package='MASS')
survey
newsurvey=na.omit(survey)
newsurvey

#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.
f2=table(newsurvey$W.Hnd)
f2
barplot(f2,main="Left Handers and Right Handers",col=c("yellow","green"))
#3. Plot the distribution between male left handers and female left handers
using barchart. Provide the title as “Female Left Handers and Male Left
Handers , y-axis labelas “count” and specify the colours for the bars.
library(dplyr)
a1=nrow(newsurvey %>% filter(Sex=="Male",W.Hnd=="Left"))
a2=nrow(newsurvey %>% filter(Sex=="Female",W.Hnd=="Left"))
a1
a2
barplot(c(a1,a2),main = "Female Left Handers and Male Left
Handers",col=c("blue","green"))
#4. Draw the distribution of smoking habits of male left handers using pie
chart.
f3=newsurvey %>% filter(Sex=="Male",W.Hnd=="Left")
c=table(f3$Smoke)
c
pie(c)
#5. Draw the histogram of age distribution with the title as ‘Age distribution’
and xlabelas ‘Age range’ and ylabel as ‘frequency’.

f4=table(newsurvey$Age)
f4
hist(f4,main="Age distribution",xlab = "Age range",ylab="frequency")
#6 Reveal the relationship between the age and writing hand span using
scatter plot.
plot(x = newsurvey$Wr.Hnd,y = newsurvey$Age,
xlab = "Writing hand span",
ylab = "Age")
#12.Draw the boxplot for pulse rate to analyse the five summary statistics.
Provide appropriate title and label.
s=boxplot(newsurvey$Pulse)$stats
s
PART B

#Install MASS package


install.packages("MASS")

#import Mass package


library("MASS")
survey
#clean the dataset
#Use na.omit() function to remove values from the survey database and
store it in new variable newsurvey
newsurvey <- na.omit(survey)
newsurvey
#Install ggplot2 and import it
install.packages("ggplot2")
library(ggplot2)

#2. Plot bar graph for number of male and female participants
x = data.frame(table(newsurvey$Sex))
x
#Use ggplot() function add gemo_bar() for barplot and ggtitle() for heading
p <- ggplot(data = x, aes(x=Var1, y = Freq,fill=Var1)) +
geom_bar(stat='identity')+ggtitle("Male and Female participants") +
xlab("Gender") + ylab("Frequency")
p

#3. Plot a bar graph for the number of left handers and right handers
x = data.frame(table(newsurvey$W.Hnd))
x
#Use ggplot() function add gemo_bar() for barplot and ggtitle() for heading
and fill = Var1 for color
p <- ggplot(data = x, aes(x=Var1, y = Freq,fill=Var1)) +
geom_bar(stat='identity')+ggtitle("Left Handers and Right Handers") +
xlab("Hand") + ylab("Frequency")
p

#4. Plot distribution between male left handers and female left handers.
library(dplyr)
ns = filter(newsurvey,W.Hnd=='Left')
mydata = data.frame(table(ns$Sex))
mydata
#Use ggplot() function add gemo_bar() for barplot and ggtitle() for heading
p <- ggplot(data = mydata, aes(x=Var1, y = Freq,fill=Var1)) +
geom_bar(stat='identity')+ggtitle("Female Left Handers and Male Left
Handers") + xlab("Gender (only Left handed)") + ylab("Frequency")
p

#5. Pie chart for smoking habit of male left handers


#Filter out values of male left handers
ns = filter(newsurvey,W.Hnd=='Left' & Sex == 'Male')
#use tabel function to count the type of smokers
t=data.frame(table(ns$Smoke))
t
#Use coord_polar for pie chart
p <- ggplot(data = t, aes(x="", y = Freq,fill=Var1)) +
geom_bar(width=1,stat='identity')+ggtitle("Smoking Male Left Handers")+
coord_polar("y", start=0)
p

#6. Histogram of age distribution with the title as ‘Age distribution’ and
xlabel as ‘Age range’ and ylabel as ‘frequency’.
#Use geom_histogram() function to plot the histogram and ggtitle(), xlab(),
ylab() for naming the graph
ggplot(data=newsurvey,aes(newsurvey$Age)) + geom_histogram() +
ggtitle("Age Distribution") + xlab('Age range') + ylab("Frequency")
#7. Relationship between the age and writing hand span using scatter plot

p <- ggplot(newsurvey, mapping = aes(x = Wr.Hnd, y = Age))


#Here we build the graph
#The geom_point() plots the points in the plane wrt x and y values
#The geom_jitter() adds a small amount of variation to the to the location
of each point.
#The geom_smooth() adds a linear regression line to the scatter plot
because we give method = "lm"
p
p + geom_point()
p + geom_point() + geom_jitter() + geom_smooth(method = "lm", color =
"red")

You might also like