IanMac — Feb 4, 2013, 6:36 PM
#350 group 1 and 650 group 2 with randomly generated iQ scores - norm distributions
iq1 <- rep(c('group1'),each = 350) # group 1
set.seed(10)
iq1scores <- rnorm(350, 95, 20) # group 1 iQ (lower mean and dispersion)
iq1 <- cbind(iq1scores, iq1) # combined group 1
iq2 <- rep(c('group2'),each = 650) # group 2
set.seed(10)
iq2scores <- rnorm(650, 105, 25) # group 2 iQ (higher mean and dispersion)
iq2 <- cbind(iq2scores, iq2) # combined group 2
iQ <- as.data.frame(rbind(iq1, iq2),stringsAsFactors=F) # put both together in a data frame
colnames(iQ) <- c('iQ', 'group') # define column names
iQ$iQ <- as.numeric(iQ$iQ) # iQ values as numeric
##Dataframe now created, ready for plotting.
#boxplot all
boxplot(iQ$iQ, col='red')
#boxplot by group
boxplot(iQ$iQ ~ as.factor(iQ$group),col="blue")
#boxplot by group and show sample sizes (shown by width)
boxplot(iQ$iQ ~ as.factor(iQ$group),col=c("blue",'orange'),names=c('first group', 'second group'),varwidth=T)
#barplot - show sample sizes
barplot(table(iQ$group),col="blue")
#histogram
hist(iQ$iQ,col="orange", main='iQ Scores')
#histogram - more detail
hist(iQ$iQ,col="red",breaks=100, main='iQ Scores')
#density plots
plot(density(iQ$iQ), lwd=5, col='black', ylim=c(0,0.02)) #all events
iQF <- density(iQ$iQ[which(iQ$group=='group1')]) #group 1 density function
iQM <- density(iQ$iQ[which(iQ$group=='group2')]) #group 2 density function
lines(iQF,lwd=3,col="orange") #overlay group 1 density function
lines(iQM,lwd=3,col="blue") #overlay group 2 density function