Exploration and Visualization of the Built-in Iris Dataset in R
Exploration and Visualization of the Built-in Iris Dataset in R
Aim
To explore the built-in Iris dataset in R and perform basic statistical analysis and data visualization.
Objectives
- To load and inspect the Iris dataset.
- To study the structure and summary statistics of the dataset.
- To perform basic exploratory data analysis.
- To visualize the data using scatter plots, histograms, box plots, and bar charts.
- To understand the relationship between different features and species.
Theory
The Iris dataset is one of the most widely used datasets in statistics and machine learning. It was introduced by Ronald Fisher in 1936 and contains measurements of three species of iris flowers:
- Setosa
- Versicolor
- Virginica
The dataset contains 150 observations and 5 variables.
Variables
| Variable | Description |
|---|---|
| Sepal.Length | Length of sepal (cm) |
| Sepal.Width | Width of sepal (cm) |
| Petal.Length | Length of petal (cm) |
| Petal.Width | Width of petal (cm) |
| Species | Species of flower |
There are 50 samples from each species.
Algorithm
- Load the built-in Iris dataset.
- Display the first and last observations.
- Study the structure and dimensions.
- Compute summary statistics.
- Find the frequency of each species.
-
Create various visualizations:
- Scatter plot
- Histogram
- Box plot
- Bar plot
- Interpret the results.
Program
# Load dataset
data(iris)
# Display first six rows
head(iris)
# Display last six rows
tail(iris)
# Dimensions
dim(iris)
# Structure
str(iris)
# Summary statistics
summary(iris)
# Frequency of species
table(iris$Species)
# Scatter plot
plot(iris$Sepal.Length,
iris$Petal.Length,
col=iris$Species,
pch=19,
main="Sepal Length vs Petal Length",
xlab="Sepal Length",
ylab="Petal Length")
# Histogram
hist(iris$Sepal.Length,
col="skyblue",
main="Histogram of Sepal Length",
xlab="Sepal Length")
# Box plot
boxplot(Sepal.Length~Species,
data=iris,
col=c("red","green","blue"),
main="Sepal Length by Species")
# Bar plot of species count
species_count <- table(iris$Species)
barplot(species_count,
col=c("red","green","blue"),
main="Species Frequency",
ylab="Count")
#saving plots in a pdf file
pdf("irisplots.pdf") plot(iris$Sepal.Length, iris$Petal.Length) hist(iris$Sepal.Length) dev.off()
Output
First Six Records
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 Setosa
2 4.9 3.0 1.4 0.2 Setosa
3 4.7 3.2 1.3 0.2 Setosa
4 4.6 3.1 1.5 0.2 Setosa
5 5.0 3.6 1.4 0.2 Setosa
6 5.4 3.9 1.7 0.4 Setosa
Dimensions
[1] 150 5
Species Frequency
Setosa 50
Versicolor 50
Virginica 50
Visualization
Scatter Plot
Relationship between Sepal Length and Petal Length.
Histogram
Box Plot
Bar Plot of Species Frequency
Result
The built-in Iris dataset was successfully explored and analyzed using R. Basic information such as dimensions, structure, and summary statistics were obtained. Various visualizations including scatter plots, histograms, box plots, and bar charts were generated to understand the distribution and relationships among the variables. The dataset contains 150 observations belonging equally to three species of iris flowers, making it suitable for statistical analysis and machine learning experiments.
Comments
Post a Comment