Introduction

This interactive document explores the built-in mtcars dataset in R. The dataset contains information on 32 cars and includes variables related to fuel efficiency, engine size, horsepower, weight, transmission type, and number of cylinders. I chose this dataset because it is small enough to understand easily, but it still has enough variables to show patterns in the data.

In this report, I use narrative text, embedded R code chunks, and interactive Plotly visualizations. This follows the idea of an interactive R Markdown document, where analysis and explanation are combined in one place and can be shared as an HTML report or published online. Interactive documents can include widgets and reactive outputs, and R Markdown supports this workflow directly in HTML-based formats. citeturn798174view0turn798174view1

Load and Preview the Dataset

data(mtcars)
head(mtcars)
##                    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

The first few rows show the basic structure of the dataset. Each row represents a car model, and each column is one of the measured variables.

Basic Summary

summary(mtcars)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000

The summary output gives a quick look at the center and spread of each variable. For example, we can see that miles per gallon (mpg) ranges quite a bit, and horsepower (hp) varies a lot across the cars.

Interactive Visualization 1: MPG vs Horsepower

This plot looks at the relationship between fuel efficiency and horsepower. I also color the points by the number of cylinders to make the groups easier to compare.

p1 <- ggplot(mtcars, aes(x = hp, y = mpg, color = factor(cyl), text = rownames(mtcars))) +
  geom_point(size = 3) +
  labs(
    title = "Miles per Gallon vs Horsepower",
    x = "Horsepower",
    y = "Miles per Gallon",
    color = "Cylinders"
  ) +
  theme_minimal()

ggplotly(p1, tooltip = c("text", "x", "y", "color"))

This interactive graph makes it easier to hover over each point and identify the car. In general, cars with more horsepower tend to have lower fuel efficiency.

Interactive Visualization 2: Weight Distribution by Cylinder Count

This second chart compares car weight across cylinder groups.

p2 <- ggplot(mtcars, aes(x = factor(cyl), y = wt, fill = factor(cyl), text = rownames(mtcars))) +
  geom_boxplot(alpha = 0.8) +
  labs(
    title = "Weight by Number of Cylinders",
    x = "Cylinders",
    y = "Weight (1000 lbs)",
    fill = "Cylinders"
  ) +
  theme_minimal()

ggplotly(p2, tooltip = c("text", "x", "y"))

This plot shows that cars with more cylinders usually weigh more. The interactive version helps by letting the reader inspect the data in more detail.

Filtered View of Automatic and Manual Cars

The dataset uses am to code transmission type. A value of 0 means automatic and 1 means manual.

mtcars <- mtcars %>%
  mutate(transmission = ifelse(am == 0, "Automatic", "Manual"))

mtcars %>%
  group_by(transmission) %>%
  summarise(
    avg_mpg = mean(mpg),
    avg_hp = mean(hp),
    avg_wt = mean(wt)
  )
## # A tibble: 2 × 4
##   transmission avg_mpg avg_hp avg_wt
##   <chr>          <dbl>  <dbl>  <dbl>
## 1 Automatic       17.1   160.   3.77
## 2 Manual          24.4   127.   2.41

This grouped summary shows some differences between automatic and manual cars. Manual cars in this dataset tend to have slightly better fuel efficiency on average.

Conclusion

Overall, this interactive document shows how R Markdown can combine writing, code, and visualizations in one place. The mtcars dataset works well for this because it has multiple numeric variables that are easy to compare. The Plotly graphs make the report more interactive since the user can hover over points and explore the data more closely instead of only looking at static plots.

If needed for submission, this document can be rendered to an HTML file in RStudio or published to RPubs.