PROGRAM 6

Author

JAGADISH

Write an R script to construct a box plot showcasing the distribution of a continuous variable, grouped by a categorical variable, using ggplot2’s fill aesthetic.

Step 1: Load Required Library

# Load ggplot2 package for visualization
library(ggplot2)

Step 2: Explore the Inbuilt Dataset

# Load dataset
data <- mtcars

# Display first few rows
head(data)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
head(iris) # View sample data
  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

Step 3: Construct Box Plot with Grouping

Step 3.1: Initialize ggplot with Aesthetic Mappings

# Initialize ggplot with data and aesthetic mappings

p <- ggplot(data = iris, aes(x = Species, y = Petal.Width, fill = Species))

Explanation:

  • x = Species: Grouping variable (categorical)

  • y = Petal.Width: Continuous variable to show distribution

  • fill = Species: Fill box colors by species

Step 3.2: Add Box Plot Layer

# Add the box plot layer

p <- p + geom_boxplot()

Explanation:

  • geom_boxplot() creates box plots for each group

  • Automatically shows median, quartiles, and outliers

Step 3.3: Add Labels and Theme

# Add title and labels and use a minimal theme

p <- p + labs(title = "Box Plot of Petal Width by Species",
              x = "Species",
              y = "Petal Width") +
         theme_minimal()

Explanation:

  • labs() adds a descriptive title and axis labels

  • theme_minimal() gives a clean, modern look

Step 3.4: Display the Plot

# Render the final plot
p

Summary

  • Used the iris dataset

  • Visualized Petal.Width as a box plot

  • Grouped by Species

  • Used fill = Species for colorful grouping

  • Each box represents the distribution of values for one species