Assignment 9

 Step 1: Load the dataset

R

# Install and load necessary packages

install.packages("ggplot2")

library(ggplot2)


# Load the mtcars dataset

data(mtcars)

Step 2: Basic Visualization


For basic visualization without any package, you can use base R plotting functions. Let's create a scatter plot of miles per gallon (mpg) against horsepower (hp).


R

# Basic Scatter Plot

plot(mtcars$hp, mtcars$mpg, main = "Scatter Plot of Horsepower vs. MPG", 

     xlab = "Horsepower", ylab = "Miles Per Gallon")

Step 3: Lattice Visualization


For lattice visualization, you can use the lattice package. Let's create a histogram of car weights (wt).


R

# Install and load the lattice package

install.packages("lattice")

library(lattice)


# Lattice Histogram

histogram(~wt, data = mtcars, main = "Histogram of Car Weights", xlab = "Weight")

Step 4: ggplot2 Visualization


Finally, for ggplot2 visualization, let's create a boxplot of miles per gallon (mpg) for different numbers of cylinders (cyl).


R

# ggplot2 Boxplot

ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +

  geom_boxplot(fill = "skyblue", color = "darkblue") +

  labs(title = "Boxplot of MPG by Number of Cylinders", x = "Number of Cylinders", y = "

Comments