# data[].attributes.body
R-script (1):
# Set working directory
setwd("E:/R Data")
# Download data
d<- read.table(file = "surveystat.csv", header = TRUE, sep = ",")
install.packages("mosaic")
library("mosaic")
mosaicplot(table(d$genhlth,d$gender), main="Distribution of categorical values of general health by gender",col=3:4)

# data[].attributes.body
Data collection: interview of the group of students in TTU. Date: December 2017.
Number of observations: 39. Number of variables: 17.
Variables: (1) 'city'; (2) 'gender' (m, f); (3) 'age'; (4) 'height'; (5) 'weight'; (6) 'weighDs' - weight desired; (7) genhlth - respondents were asked to evaluate their general health, responding either excellent (E), good (G), npt that good (N) or bad (B); (8) 'phex' -  respondents were asked whether they exercise (y, n); (9) 'phexNum' - number of physical exercises per month; (10) 'smoke' - respondents were asked whether they smoke (y, n); (11) 'smokeNum' - number of cigarettes smoked per day; (12) 'coffee' - respondents were asked whether they drink coffee (y, n); (13) 'coffeeNum' - number of cups of coffee per day; (14) 'sleep' - hours of sleep per day; (15) 'visdoc' - respondents were asked whether they vsit doctors regularly (y, n); (16) 'visdocNum' - number of visits to doctors per year; (17) 'score' - average score.
Purpose of survey: study purpose; R-scripts are provided so as students could learn how to visualize data in R-Studio. File '.csv' with data is available in the computer classes.

# data[].attributes.body
R-script(2):
mosaicplot(table(d$city,d$genhlth),main="General health by city", col=2:5)

# data[].attributes.headline
Mosaicplot: General health by smoking habits

# data[].attributes.subject
Mosaicplot: General health by smoking habits

# data[].attributes.body
R-script(3):
mosaicplot(table(d$genhlth,d$smoke),main="General health by smoking habits",col=3:2)

# data[].attributes.headline
Mosaicplot: General health by coffee drinking habits

# data[].attributes.subject
Mosaicplot: General health by coffee drinking habits

# data[].attributes.body
R-script(4):
mosaicplot(table(d$genhlth,d$coffee),main="General health by coffee drinking habits",col=3:2)

# data[].attributes.headline
Mosaicplot: General health in relation to physical exercises

# data[].attributes.subject
Mosaicplot: General health in relation to physical exercises

# data[].attributes.body
R-script(5):
mosaicplot(table(d$genhlth,d$phex),main="General health by P.E. habits",col=2:3)

# data[].attributes.headline
Boxplots and violin plot for categorical and numeric variables

# data[].attributes.subject
Boxplots and violin plot for categorical and numeric variables

# data[].attributes.body
R-script(1):
# Relationship between categorical and numeric vaiables
install.packages("ggplot2")
library("ggplot2")
qplot(data=d,x=genhlth,y=age,geom="violin",xlab="general health (B-bad, E-excellent, G-good, N-not that good)",main="General health by age")

# data[].attributes.headline
Side-by-side boxplots: General health in relation to smoking

# data[].attributes.subject
Side-by-side boxplots: General health in relation to smoking

# data[].attributes.body
R-script(2):
qplot(data=d,x=genhlth,y=smokeNum,geom="boxplot",xlab="general health (B-bad, E-excellent, G-good, N-not that good)",main="General health dependent on number of cigarettes smoked per day")

# data[].attributes.headline
Side-by-side boxplots: General health in relation to coffee drinking

# data[].attributes.subject
Side-by-side boxplots: General health in relation to coffee drinking

# data[].attributes.body
R-script(3):
qplot(data=d,x=genhlth,y=coffeeNum,geom="boxplot",xlab="general health (B-bad, E-excellent, G-good, N-not that good)",main="General health dependent on coffee drinking per day")

# data[].attributes.headline
Side-by-side boxplots: General health in relation to physical exercises

# data[].attributes.subject
Side-by-side boxplots: General health in relation to physical exercises

# data[].attributes.body
R-script(4):
qplot(data=d,x=genhlth,y=phexNum,geom="boxplot",xlab="general health (B-bad, E-excellent, G-good, N-not that good)",main="General health dependent on number of physical exercises per month")

# data[].attributes.headline
Side-by-side boxplots: General health and visiting doctors

# data[].attributes.subject
Side-by-side boxplots: General health and visiting doctors

# data[].attributes.body
R-script(5):
qplot(data=d,x=genhlth,y=visdocNum,geom="boxplot",xlab="general health (B-bad, E-excellent, G-good, N-not that good)",main="General health dependent on visiting doctors per year")

# data[].attributes.headline
Distribution between smoking and coffee drinking habits, evaluated by Age and Gender

# data[].attributes.subject
Distribution between smoking and coffee drinking habits, evaluated by Age and Gender

# data[].attributes.body
R-script(3):
radius<-sqrt(d$age/pi)
symbols(d$smokeNum,d$coffeeNum,circles=radius,inches=0.15, fg="white", bg="turquoise", xlab="Cigarettes smoked per day", ylab="Coffee drank per day",main="Smoking/coffee drinking habits by Age and Gender")
text(d$smokeNum,d$coffeeNum,d$gender, cex=0.5)
Bubble size is determined by Age

# data[].attributes.headline
Distribution between physical exercises and visiting doctors, evaluated by Age and Gender

# data[].attributes.subject
Distribution between physical exercises and visiting doctors, evaluated by Age and Gender

# data[].attributes.body
R-script(2):
symbols(d$phexNum, d$visdocNum, squares=sqrt(d$age), fg="white", bg="tomato1",inches=0.4,xlab="Physical exercises per month", ylab="Visits to doctor per year", main="P.E. and Visiting doctors, evaluated by Age and Gender")
text(d$phexNum, d$visdocNum,d$gender, cex=0.5)
Square size is determined by Age

# data[].attributes.headline
Distribution between Weight and Height, evaluated by Age

# data[].attributes.subject
Distribution between Weight and Height, evaluated by Age

# data[].attributes.body
R-script(1):
radius<-sqrt(d$age/pi)
symbols(d$weight,d$height,circles=radius,inches=0.2, fg="white", bg="violet", xlab="Weight, kg", ylab="Height, cm",main="Weight and Height of respondents, evaluated by Age")
Bubble size is determined by Age

# data[].attributes.headline
Distribution between Visiting doctors and Smoking, evaluated by the Number of smokes and Gender

# data[].attributes.subject
Distribution between Visiting doctors and Smoking, evaluated by the Number of smokes and Gender

# data[].attributes.body
R-script(4):
radius<-sqrt(d$smokeNum/pi)
symbols(d$visdocNum,d$smokeNum,circles=radius,inches=0.2, fg="white", bg="seagreen1",  xlab="Visiting doctors, times/year", ylab="Cigarettes smoked per day",main="Visiting doctors/Smoking, evaluated by Smoking and Gender")
text(d$visdocNum,d$smokeNum,d$gender, cex=0.7)
Bubble size is determined by the number of smokes per day

# data[].attributes.headline
Distribution between Physical exercises and Visiting doctors, evaluated by P.E.

# data[].attributes.subject
Distribution between Physical exercises and Visiting doctors, evaluated by P.E.

# data[].attributes.body
R-script(5):
symbols(d$phexNum,d$visdocNum,squares=sqrt(d$phexNum),inches=0.5, fg="white", bg="orange1",  xlab="Physical exercises, times/month", ylab="Visiting doctors, times/year",main="Physical Exercises and Visiting doctors, evaluated by P.E.")
Square size is determined by the number of physical exercises per month

# data[].attributes.headline
Mosaicplot: Doing physical exercises by females

# data[].attributes.subject
Mosaicplot: Doing physical exercises by females

# data[].attributes.body
R-script(1):
dsf<-subset(d,d$gender=="f")
mosaicplot(table(dsf$phex),col=2:3,main="Doing physical exercises by females")

# data[].attributes.body
R-script(2):
mosaicplot(table(dsf$visdoc),col=2:3,main="Visiting doctors by females")

# data[].attributes.body
R-script(3):
mosaicplot(table(dsf$coffee),col=3:2,main="Drinking coffee by females")

# data[].attributes.headline
Mosaicplot: Doing physical exercises by males

# data[].attributes.subject
Mosaicplot: Doing physical exercises by males

# data[].attributes.body
R-script(1):
dsm<-subset(d,d$gender=="m")
mosaicplot(table(dsm$phex),col=2:3,main="Doing physical exercises by males")

# data[].attributes.body
R-script(2):
mosaicplot(table(dsm$visdoc),col=2:3,main="Visiting doctors by males")

# data[].attributes.body
R-script(3):
mosaicplot(table(dsm$coffee),col=3:2,main="Drinking coffee by males")

# data[].attributes.headline
Sunflower plot: Distribution of Number of cigarettes smoked across Age

# data[].attributes.subject
Sunflower plot: Distribution of Number of cigarettes smoked across Age

# data[].attributes.body
S-script:
sunflowerplot(d$age,d$smokeNum,pch = 16, cex = 0.8, cex.fact = 1.5,col = par("col"), bg = NA, size = 1/6, seg.col = 3, seg.lwd = 2.5,main='Relationship between Age and Smoking',xlab='Age',ylab='Numbr of cigarettes smoked per day')

# data[].attributes.headline
Dotplot: Distribution of hours of sleep by females

# data[].attributes.subject
Dotplot: Distribution of hours of sleep by females

# data[].attributes.body
R-script(4):
qplot(dsf$sleep,geom = "dotplot",binwidth=0.5,xlab="Hours of sleep per day",main="Hours of sleep per day by females")

# data[].attributes.headline
Q-Q Plot: Distribution of hours of sleep by females

# data[].attributes.subject
Q-Q Plot: Distribution of hours of sleep by females

# data[].attributes.body
R-script(5):
qqnorm(dsf$sleep,main="Normal Q-Q plot for hours of sleep by females")
qqline(dsf$sleep)

# data[].attributes.headline
Dotplot: Distribution of hours of sleep by males

# data[].attributes.subject
Dotplot: Distribution of hours of sleep by males

# data[].attributes.body
R-script(4):
qplot(dsm$sleep,geom = "dotplot",binwidth=0.5,xlab="Hours of sleep per day",main="Hours of sleep per day by males")

# data[].attributes.headline
Q-Q Plot: Distribution of hours of sleep by males

# data[].attributes.subject
Q-Q Plot: Distribution of hours of sleep by males

# data[].attributes.body
R-script(5):
qqnorm(dsm$sleep,main="Normal Q-Q plot for hours of sleep by males")
qqline(dsm$sleep)

# data[].attributes.headline
Histogram: Distribution of weight for females

# data[].attributes.subject
Histogram: Distribution of weight for females

# data[].attributes.body
R-script:
hist(dsf$weight,col = "pink3",border="pink",xlab="Weight, kg",main="Distribution of weight for females")

# data[].attributes.headline
Histogram: Distribution of weight for males

# data[].attributes.subject
Histogram: Distribution of weight for males

# data[].attributes.body
R-script:
hist(dsm$weight,col = "skyblue",border="blue",xlab="Weight, kg",main="Distribution of weight for males")

# data[].attributes.headline
Boxplot: Distribution of Height for females

# data[].attributes.subject
Boxplot: Distribution of Height for females

# data[].attributes.body
R-script:
boxplot(dsf$height,col = "orchid",horizontal=TRUE,xlab="Height, cm",pch=18,boxwex=0.9,main="Distribution of height for females")

# data[].attributes.headline
Boxplot: Distribution of Height for males

# data[].attributes.subject
Boxplot: Distribution of Height for males

# data[].attributes.body
R-script:
boxplot(dsm$height,col = "slateblue",horizontal=TRUE,xlab="Height, cm",pch=18,boxwex=0.9,main="Distribution of height for males")