Professional Documents
Culture Documents
Data
data(cars)
R Library Used
Basic R, ggplot2
Histograms
Histograms display the distribution of a continuous variable by dividing the range of
scores into a specified number of bins on the x-axis and displaying the frequency of
scores in each bin on the y-axis.
Code - Histograms
par(mfrow=c(1,2)) # To display 2 graphs in one row
hist(mtcars$mpg)
hist(mtcars$mpg,
breaks=12,
col="red",
xlab="Miles Per Gallon",
main="Colored histogram with 12 bins")
hist(mtcars$mpg,
freq=FALSE,
breaks=12,
col="red",
xlab="Miles Per Gallon",
main="Histogram, rug plot, density curve")
rug(jitter(mtcars$mpg))
lines(density(mtcars$mpg), col="blue", lwd=2)
x <- mtcars$mpg
h<-hist(x,
breaks=12,
col="red",
xlab="Miles Per Gallon",
main="Histogram with normal curve and box")
xfit<-seq(min(x),max(x),length=40)
yfit<-dnorm(xfit,mean=mean(x),sd=sd(x))
yfit <- yfit*diff(h$mids[1:2])*length(x)
lines(xfit, yfit, col="blue", lwd=2)
box()
Box plots
A box-and-whiskers plot describes the distribution of a continuous variable by plotting its
five-number summary.
Using parallel box plots to compare groups
Box plots can be created for individual variables or for variables by group.
Code - Box plots
boxplot(mpg~cyl,data=mtcars,
main="Car Milage Data",
xlab="Number of Cylinders",
ylab="Miles Per Gallon")
notes
Bar plots needn’t be based on counts or frequencies. You can create bar plots that
represent means, medians, standard deviations, and so forth by using the aggregate
function and passing the results to the barplot() function.
Box plots for two crossed factors
Finally, you can produce box plots for more than one grouping factor.
Code - Box plots for two crossed factors
mtcars$cyl.f <- factor(mtcars$cyl,
levels=c(4,6,8),
labels=c("4","6","8"))
mtcars$am.f <- factor(mtcars$am,
levels=c(0,1),
labels=c("auto","standard"))
Follow
Understanding the data does not mean getting the mean, median,
standard deviation only. Lots of time it is important to learn the
variability or spread or distribution of the data. Both histogram
and boxplot are good for providing a lot of extra information about
a dataset that helps with the understanding of the data.
Histogram
A histogram takes only one variable from the dataset and shows
the frequency of each occurrence. I will use a simple dataset to
learn how histogram helps to understand a dataset. I will use
python to make the plots. Let’s import the dataset:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv("Cartwheeldata.csv")
df.head()
Boxplot
b. Median is 6
Data
Strength;
Pie Chart
Strength
Best practices
Pie charts are very difficult for humans to interpret accurately due to angle and area. To
improve perception:
Line Chart
Data
Strength
Best practices