LA 1

Author

Gagana L & M.S Anusha

Introduction

In this report, we explore the relationship between the weight of cars (wt) and their miles per gallon (mpg) using the mtcars dataset. An interactive scatter plot is created to visualize this relationship, providing insights into how these variables correlate.

Data Preparation

loading the necessary libraries

# Load required libraries
library(ggplot2)
library(plotly)

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

preparing the data

# Prepare the dataset
data <- mtcars %>%
  select(mpg, wt, cyl) %>%
  mutate(cyl = as.factor(cyl))

Interactive scatter plot

# Create the interactive scatter plot
plot <- ggplot(data, aes(x = wt, y = mpg, color = cyl)) +
  geom_point(size = 3) +
  labs(title = "Scatter Plot of MPG vs Weight",
       x = "Weight (1000 lbs)",
       y = "Miles per Gallon",
       color = "Number of Cylinders")

# Convert to interactive plotly object
interactive_plot <- ggplotly(plot)
interactive_plot