PROGRAM 3

Author

JAGADISH J M

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

1 + 1
[1] 2

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).

Program 3

Introduction

library(ggplot2)
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
library(tidyr)

Step 2:Load the Built-in airpassenger Dataset

# Convert time-series data to a data frame
data <- data.frame(
  Date = seq(as.Date("1949-01-01"), by = "month", length.out = length(AirPassengers)),
  passengers = as.numeric(AirPassengers),  # Corrected to as.numeric
  Year = as.factor(format(seq(as.Date("1949-01-01"), by = "month", length.out = length(AirPassengers)), "%Y"))
)

# Display the first 20 rows
head(data, n=20)  # Correct syntax for displaying the first 20 rows
         Date passengers Year
1  1949-01-01        112 1949
2  1949-02-01        118 1949
3  1949-03-01        132 1949
4  1949-04-01        129 1949
5  1949-05-01        121 1949
6  1949-06-01        135 1949
7  1949-07-01        148 1949
8  1949-08-01        148 1949
9  1949-09-01        136 1949
10 1949-10-01        119 1949
11 1949-11-01        104 1949
12 1949-12-01        118 1949
13 1950-01-01        115 1950
14 1950-02-01        126 1950
15 1950-03-01        141 1950
16 1950-04-01        135 1950
17 1950-05-01        125 1950
18 1950-06-01        149 1950
19 1950-07-01        170 1950
20 1950-08-01        170 1950

Step 3:Define a Function for Time-Series LINe Graph

# Function to plot time-series trend
plot_time_series <- function(data, x_col, y_col, group_col, title = "Air passenger Trends") {
  ggplot(data, aes(x = {{x_col}}, y = {{y_col}}, color = {{group_col}})) + 
    geom_line(size = 1.2) +  # Line graph
    geom_point(size = 2) +   # Add points for clarity
    labs(title = title,
         x = "Year",
         y = "Number of passengers",
         color = "Year") +  # Legend title
    theme_minimal() +
    theme(legend.position = "top")
}

# Call the function
plot_time_series(data, Date, passengers, Year, "Trend of Airline Passengers Over Time")
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.