Introduction


In this assignment, we explore the mtcars dataset, which contains information about various car models.

  • mpg: Miles per gallon (Numeric)
  • cyl: Number of cylinders (Numeric)
  • disp: Displacement Numeric cubic (inches)
  • hp: Gross horsepower Numeric (hp)
  • drat: Rear axle ratio Numeric (ratio)
  • wt: Weight Numeric (1000 lbs)
  • qsec:1/4 mile time Numeric (seconds)
  • vs V/S engine (0 = V-shaped, 1 = straight) Numeric (binary)
  • am Transmission (0 = automatic, 1 = manual) Numeric (binary)
  • gear Number of forward gears Numeric (gears)
  • carb Number of carburetors Numeric (carburetors)

  • library(tidyverse)
    library(ggplot2)
    # Load and show dataset
    data_mtcars <- mtcars
    head(data_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
    # Convert 'am' and 'cyl' to factors for categorical plotting
    data_mtcars$am <- as.factor(data_mtcars$am)
    data_mtcars$cyl <- as.factor(data_mtcars$cyl)


    Scatterplot

    # Create a scatter plot of car weight vs. miles per gallon, colored by cylinder count
    
    ggplot(data_mtcars, aes(x= wt, y = mpg, color = cyl)) +
      geom_point() + 
      labs(title = "Weight vs. Miles Per Gallon", x = "Weight (1000 lbs)", y = "Miles Per Gallon") +
      theme_minimal() +
      theme(
        plot.title = element_text(hjust=0.5, face='bold')
      )

    No clear relationship spotted between the weight of a car and its gas consumption. Instead, when cylinders are clustered, there is a clear patterns showing which cylinder consume my miles per gallons.


    Line Graph

    #Create a line graph of ordered mpg by the row number
    data_mtcars_line <- data_mtcars %>% 
      mutate(index = row_number())
    
    ggplot(data_mtcars_line, aes(x = index, y = mpg)) + 
      geom_line(color="gold", linewidth=1) +
        labs(title = "Miles Per Gallon", 
             subtitle="By Index", 
             x = "Index", y = "Miles Per Gallon") +
      theme_minimal() +
      theme(
        plot.title= element_text(hjust=0.5, face="bold"),
        plot.subtitle = element_text(hjust=0.5, face="bold")
      )


    Bar Graph

    hp_by_cyl <- data_mtcars %>% 
      group_by(cyl) %>% 
      summarise(avg_hp = mean(hp))
    
    ggplot(hp_by_cyl, aes(x = avg_hp, y = cyl)) +
      geom_bar(stat = "identity", fill = "turquoise", color = "white") +
      labs(title = "Average HP", subtitle = "By Cylinder Count", y = "Cylinder Count", x = "Average Horsepower") +
      theme_minimal() +
      theme(
        plot.title= element_text(hjust=0.5, face="bold"),
        plot.subtitle = element_text(hjust=0.5, face="bold")
      )

    #Create a stacked bar chart of average mpg, disp, hp, and wt, grouped by cyl
    bar_data_mtcars <- data_mtcars %>% 
      group_by(cyl) %>% 
      summarize(mpg = mean(mpg), disp = mean(disp), hp = mean(hp), wt = mean(wt)) %>% 
      pivot_longer(cols = c("mpg", "disp", "hp", "wt"),
                   names_to = "Measurement", values_to = "Average") #Calculate average values for each measurement, and pivot the data into a long format.
    
    ggplot(bar_data_mtcars, aes(x = cyl, y = Average, fill= Measurement)) +
      geom_bar(stat = "identity") +
      labs(title = "Average Measurements", subtitle ="By Cylinder Count", x = "Cylinder Count", y = "Average Measurement") +
       theme_minimal() +
      theme(
        plot.title= element_text(hjust=0.5, face="bold"),
        plot.subtitle = element_text(hjust=0.5, face="bold")
      )