Professional Documents
Culture Documents
----------------------------------------------
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
#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
#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