Univariate/Bi Variate Analysis
Univariate/Bi Variate Analysis
Univariate/Bi Variate Analysis
analysis
Working with data.
• Accessing columns.
• D has our data in it…. But you can’t see it directly.
• To select a column use D$column.
Working with data.
• Subsetting data.
• Use a logical operator to do this.
• ==, >, <, <=, >=, <> are all logical operators.
• Note that the “equals” logical operator is two = signs.
• Example:
• D[D$Gender == “M”,]
• This will return the rows of D where Gender is “M”.
• Remember R is case sensitive!
• This code does nothing to the original dataset.
• D.M <- D[D$Gender == “M”,] gives a dataset with
the appropriate rows.
Basic Graphics
• Histogram
• hist(D$wg)
Basic Graphics
• Add a title…
• The “main” statement will
give the plot an overall
heading.
• hist(D$wg ,
main=‘Weight Gain’)
Basic Graphics
• Adding axis labels…
• Use “xlab” and “ylab” to
label the X and Y axes,
respectively.
• hist(D$wg , main=‘Weight
Gain’,xlab=‘Weight
Gain’, ylab
=‘Frequency’)
Basic Graphics
• Changing colors…
• Use the col statement.
• ?colors will give you help
on the colors.
• Common colors may
simply put in using the
name.
• hist(D$wg,
main=“Weight
Gain”,xlab=“Weight
Gain”, ylab
=“Frequency”,
col=“blue”)
Basic Graphics – Colors
Basic Plots
• Box Plots
• boxplot(D$wg)
Boxplots
• Change it!
• boxplot(D$wg,main='Weigh
t Gain',ylab='Weight
Gain (lbs)')
Box-Plots - Groupings
• What if we want several box plots side by side to be
able to compare them.
• First Subset the Data into separate variables.
• wg.m <- D[D$Gender=="M",]
• wg.f <- D[D$Gender=="F",]
• Then Create the box plot.
• boxplot(wg.m$wg,wg.f$wg)
Boxplots – Groupings
Boxplots - Groupings
plot(t1,D2$DELL,type="l")
Line Plots
lines(t1,D2$Intel,lty=2)
Overlaying Graphs
Adding a Legend
• Adding a legend is a bit tricky in R.
• Syntax
• legend( x, y, names, line types)
X
coordinate
Y Names of Corresponding
series in line types
coordinate
column
format
Adding a Legend
legend(60,45,c('Intel','Dell'),lty=c(1,2))
Paneling Graphics
• Suppose we want more than one graphic on a
panel.
• We can partition the graphics panel to give us a
framework in which to panel our plots.
• par(mfrow = c( nrow, ncol))