Program 8

Author

Aditya Suresh

USN: 1NT24IS016

Program 1

Develop a R program to quickly explore a given dataset, including categorical analysis using the group_by() command, visualise

What We Will Do

In this program, we will:

  1. Load the required libraries and dataset.
  2. Explore the structure of the dataset
  3. Convert a numeric variable into a categorical variable.
  4. Perform categorical analysis using ‘group_by()’ and ‘summarise()’.
  5. Visualize the result

Step 1: Load the required Libraries and Dataset

  • tidyverse is a collection of packages for data science.
  • dplyr is used for grouping and summarizing data.
library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.5.3
Warning: package 'readr' was built under R version 4.5.3
Warning: package 'forcats' was built under R version 4.5.3
Warning: package 'lubridate' was built under R version 4.5.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.2     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)

# Load built-in dataset
data <- mtcars

Step 2: Explore the Dataset

Before performing any analysis, we should understand the dataset.

We will check:

  • Number of rows and columns
  • Column names
  • Data types
  • Summary statistics
  • First few rows
#Dimensions (rows and columns)
dim(data)
[1] 32 11
# Column names
names(data)
 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
[11] "carb"
# Structure of dataset
str(data)
'data.frame':   32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
# Summary statistics
summary(data)
      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  
# First six rows
head(data)
                   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

Step 3: Convert Numeric Value to Categorical

The variable cyl represents the number of cylinders in a car.

Although it is numeric (4,6,8), it represents categories.
For categorical analysis, we convert it into a factor.

# Convert 'cyl' to factor
data$cyl <- as.factor(data$cyl)

# Confirm conversion
str(data$cyl)
 Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...
levels(data$cyl)
[1] "4" "6" "8"

Why Is This Important?

Step 4: Perform Categorical Analysis

We calculate the average miles per gallon (mpg) for each cylinder category

How These Functions Work Together

  • %>% passes output from one function to the next.
  • group_by(cyl) splits the dataset into groups.
  • summarise() calculates statistics per group.
  • mean(mpg) computes average mileage.
  • .groups = "drop" removes grouping afterward.
summary_data <- data %>%
  group_by(cyl) %>%
  summarise(
    avg_mpg = mean(mpg),
    .groups = "drop"
  )
summary_data
# A tibble: 3 × 2
  cyl   avg_mpg
  <fct>   <dbl>
1 4        26.7
2 6        19.7
3 8        15.1

Step 6: Visualize Using a Bar Plot

Understanding ggplot Components

  • ggplot(summary_data, aes(...)) defines dataset and mappings.
  • aes(x=cyl, y=avg_mpg, fill=cyl) sets axes and colors.
  • geom_bar(stat = "identity") uses actual values.
  • labs() adds titles and labels.
  • theme_minimal() applies a clean theme.
ggplot(summary_data, aes(x = cyl, y= avg_mpg, fill = cyl)) +
  geom_bar(stat = "identity") +
  labs(
    title="Average MPG by Cylinder Count",
    x= "Number of Cylinders",
    y = "Average MPG"
  ) +
  theme_minimal()

Program 2

Write an R script to create a scatter plot, incorporating categorical analysis though color-coded data points representing different groups, using ggplot2

Step 1: Load libraries We had two libraries:

  • ggplot is used to build plots layer-by-layer (we will use it to create the scatter plot).
  • dplyr provides functions for exploring and summarizing data (we will use it to understand the categories in the dataset).
library(ggplot2)
library(dplyr)

Step 2: Load the dataset(iris)

We use the built-in dataset iris.

What this dataset contains:

  • Each row is one flower sample (on observation).
  • There are 150 total observations.
  • The column Species is a categorical variable with 3 groups:
    • setosa
    • versicolor
    • virginica
  • The columns Sepal.Length and Sepal.Width are numeric measurements that we will plot.
data <- iris

Step 3: Preview the dataset (see the first few rows)

We preview the data to understand:

head(data,10)
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1           5.1         3.5          1.4         0.2  setosa
2           4.9         3.0          1.4         0.2  setosa
3           4.7         3.2          1.3         0.2  setosa
4           4.6         3.1          1.5         0.2  setosa
5           5.0         3.6          1.4         0.2  setosa
6           5.4         3.9          1.7         0.4  setosa
7           4.6         3.4          1.4         0.3  setosa
8           5.0         3.4          1.5         0.2  setosa
9           4.4         2.9          1.4         0.2  setosa
10          4.9         3.1          1.5         0.1  setosa
tail(data,10)
    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
141          6.7         3.1          5.6         2.4 virginica
142          6.9         3.1          5.1         2.3 virginica
143          5.8         2.7          5.1         1.9 virginica
144          6.8         3.2          5.9         2.3 virginica
145          6.7         3.3          5.7         2.5 virginica
146          6.7         3.0          5.2         2.3 virginica
147          6.3         2.5          5.0         1.9 virginica
148          6.5         3.0          5.2         2.0 virginica
149          6.2         3.4          5.4         2.3 virginica
150          5.9         3.0          5.1         1.8 virginica
str(data)
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(data)
  Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
 Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
 1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
 Median :5.800   Median :3.000   Median :4.350   Median :1.300  
 Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
 3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
 Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
       Species  
 setosa    :50  
 versicolor:50  
 virginica :50  
                
                
                
names(data)
[1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"     
dim(data)
[1] 150   5
data[1]
    Sepal.Length
1            5.1
2            4.9
3            4.7
4            4.6
5            5.0
6            5.4
7            4.6
8            5.0
9            4.4
10           4.9
11           5.4
12           4.8
13           4.8
14           4.3
15           5.8
16           5.7
17           5.4
18           5.1
19           5.7
20           5.1
21           5.4
22           5.1
23           4.6
24           5.1
25           4.8
26           5.0
27           5.0
28           5.2
29           5.2
30           4.7
31           4.8
32           5.4
33           5.2
34           5.5
35           4.9
36           5.0
37           5.5
38           4.9
39           4.4
40           5.1
41           5.0
42           4.5
43           4.4
44           5.0
45           5.1
46           4.8
47           5.1
48           4.6
49           5.3
50           5.0
51           7.0
52           6.4
53           6.9
54           5.5
55           6.5
56           5.7
57           6.3
58           4.9
59           6.6
60           5.2
61           5.0
62           5.9
63           6.0
64           6.1
65           5.6
66           6.7
67           5.6
68           5.8
69           6.2
70           5.6
71           5.9
72           6.1
73           6.3
74           6.1
75           6.4
76           6.6
77           6.8
78           6.7
79           6.0
80           5.7
81           5.5
82           5.5
83           5.8
84           6.0
85           5.4
86           6.0
87           6.7
88           6.3
89           5.6
90           5.5
91           5.5
92           6.1
93           5.8
94           5.0
95           5.6
96           5.7
97           5.7
98           6.2
99           5.1
100          5.7
101          6.3
102          5.8
103          7.1
104          6.3
105          6.5
106          7.6
107          4.9
108          7.3
109          6.7
110          7.2
111          6.5
112          6.4
113          6.8
114          5.7
115          5.8
116          6.4
117          6.5
118          7.7
119          7.7
120          6.0
121          6.9
122          5.6
123          7.7
124          6.3
125          6.7
126          7.2
127          6.2
128          6.1
129          6.4
130          7.2
131          7.4
132          7.9
133          6.4
134          6.3
135          6.1
136          7.7
137          6.3
138          6.4
139          6.0
140          6.9
141          6.7
142          6.9
143          5.8
144          6.8
145          6.7
146          6.7
147          6.3
148          6.5
149          6.2
150          5.9
data$Sepal.Length
  [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1
 [19] 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0
 [37] 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 7.0 6.4 6.9 5.5
 [55] 6.5 5.7 6.3 4.9 6.6 5.2 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1
 [73] 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7 5.5 5.5 5.8 6.0 5.4 6.0 6.7 6.3 5.6 5.5
 [91] 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3
[109] 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7 6.0 6.9 5.6 7.7 6.3 6.7 7.2
[127] 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7 6.3 6.4 6.0 6.9 6.7 6.9 5.8 6.8
[145] 6.7 6.7 6.3 6.5 6.2 5.9
typeof(data$Sepal.Length)
[1] "double"
data[][1]
    Sepal.Length
1            5.1
2            4.9
3            4.7
4            4.6
5            5.0
6            5.4
7            4.6
8            5.0
9            4.4
10           4.9
11           5.4
12           4.8
13           4.8
14           4.3
15           5.8
16           5.7
17           5.4
18           5.1
19           5.7
20           5.1
21           5.4
22           5.1
23           4.6
24           5.1
25           4.8
26           5.0
27           5.0
28           5.2
29           5.2
30           4.7
31           4.8
32           5.4
33           5.2
34           5.5
35           4.9
36           5.0
37           5.5
38           4.9
39           4.4
40           5.1
41           5.0
42           4.5
43           4.4
44           5.0
45           5.1
46           4.8
47           5.1
48           4.6
49           5.3
50           5.0
51           7.0
52           6.4
53           6.9
54           5.5
55           6.5
56           5.7
57           6.3
58           4.9
59           6.6
60           5.2
61           5.0
62           5.9
63           6.0
64           6.1
65           5.6
66           6.7
67           5.6
68           5.8
69           6.2
70           5.6
71           5.9
72           6.1
73           6.3
74           6.1
75           6.4
76           6.6
77           6.8
78           6.7
79           6.0
80           5.7
81           5.5
82           5.5
83           5.8
84           6.0
85           5.4
86           6.0
87           6.7
88           6.3
89           5.6
90           5.5
91           5.5
92           6.1
93           5.8
94           5.0
95           5.6
96           5.7
97           5.7
98           6.2
99           5.1
100          5.7
101          6.3
102          5.8
103          7.1
104          6.3
105          6.5
106          7.6
107          4.9
108          7.3
109          6.7
110          7.2
111          6.5
112          6.4
113          6.8
114          5.7
115          5.8
116          6.4
117          6.5
118          7.7
119          7.7
120          6.0
121          6.9
122          5.6
123          7.7
124          6.3
125          6.7
126          7.2
127          6.2
128          6.1
129          6.4
130          7.2
131          7.4
132          7.9
133          6.4
134          6.3
135          6.1
136          7.7
137          6.3
138          6.4
139          6.0
140          6.9
141          6.7
142          6.9
143          5.8
144          6.8
145          6.7
146          6.7
147          6.3
148          6.5
149          6.2
150          5.9
data[2][]
    Sepal.Width
1           3.5
2           3.0
3           3.2
4           3.1
5           3.6
6           3.9
7           3.4
8           3.4
9           2.9
10          3.1
11          3.7
12          3.4
13          3.0
14          3.0
15          4.0
16          4.4
17          3.9
18          3.5
19          3.8
20          3.8
21          3.4
22          3.7
23          3.6
24          3.3
25          3.4
26          3.0
27          3.4
28          3.5
29          3.4
30          3.2
31          3.1
32          3.4
33          4.1
34          4.2
35          3.1
36          3.2
37          3.5
38          3.6
39          3.0
40          3.4
41          3.5
42          2.3
43          3.2
44          3.5
45          3.8
46          3.0
47          3.8
48          3.2
49          3.7
50          3.3
51          3.2
52          3.2
53          3.1
54          2.3
55          2.8
56          2.8
57          3.3
58          2.4
59          2.9
60          2.7
61          2.0
62          3.0
63          2.2
64          2.9
65          2.9
66          3.1
67          3.0
68          2.7
69          2.2
70          2.5
71          3.2
72          2.8
73          2.5
74          2.8
75          2.9
76          3.0
77          2.8
78          3.0
79          2.9
80          2.6
81          2.4
82          2.4
83          2.7
84          2.7
85          3.0
86          3.4
87          3.1
88          2.3
89          3.0
90          2.5
91          2.6
92          3.0
93          2.6
94          2.3
95          2.7
96          3.0
97          2.9
98          2.9
99          2.5
100         2.8
101         3.3
102         2.7
103         3.0
104         2.9
105         3.0
106         3.0
107         2.5
108         2.9
109         2.5
110         3.6
111         3.2
112         2.7
113         3.0
114         2.5
115         2.8
116         3.2
117         3.0
118         3.8
119         2.6
120         2.2
121         3.2
122         2.8
123         2.8
124         2.7
125         3.3
126         3.2
127         2.8
128         3.0
129         2.8
130         3.0
131         2.8
132         3.8
133         2.8
134         2.8
135         2.6
136         3.0
137         3.4
138         3.1
139         3.0
140         3.1
141         3.1
142         3.1
143         2.7
144         3.2
145         3.3
146         3.0
147         2.5
148         3.0
149         3.4
150         3.0
data[1:5, 1:3]
  Sepal.Length Sepal.Width Petal.Length
1          5.1         3.5          1.4
2          4.9         3.0          1.4
3          4.7         3.2          1.3
4          4.6         3.1          1.5
5          5.0         3.6          1.4

Step 4: Inspect the categorical variable (Species)

This is our categorical analysis step.

table(data$Species) counts how many observations belong to each species.

Why do this?

  • It confirms that Species has the groups we expect.
  • It helps us understand whether the
table(data$Species)

    setosa versicolor  virginica 
        50         50         50 

Step 5: Create a basic scatter plot (no categories yet)

A scatter plot shows the relationship between two numeric variables.

Here we plot:

  • X-axis: Sepal.Length
  • Y-axis: Sepal.Width

Important point:

  • Each dot represents one flower (one row in the dataset).
ggplot(data, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point()


Step 6: Add categorical grouping using color = Species

Now we include the categorical variable:

  • color = Sepcies tells ggplot2 to assign a different color to each species.

What changes?

  • The plot now visually separates the three species based on color.
  • This is the main “categorical analysis” idea: we can see if different groups cluster differently.
ggplot(data, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point()


Step 7: Improve point visibility (size and transparency)

We adjust how points look:

  • size = 3 makes each dot bigger, so it is easier to see.
  • alpha = 0.7 makes dots slightly transparent, which helps when points overlap.

Why transparency helps:

  • If many points overlap in the same region, transparency makes dense areas more visible.
ggplot(data, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3, alpha = 0.7)


Step 8: Add informative labels (title, axes, legend)

Good plots should clearly communicate what the viewer is seeing.

labs() adds:

  • title for the plot heading.
  • x and y axis labels
  • color legend title (so the legend has a meaningful name)
ggplot(data, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3, alpha = 0.7) +
  labs(
    title = "Scatter Plot of Sepal Dimensions",
    x = "Sepal Length",
    y = "Sepal Width",
    color = "Species"
  )


Step 9: Apply a clean theme and move the legend

Themes control the background, grids, and text styling.

  • theme_minimal() removes heavy backgrounds and gives a clean look.
  • theme(legend.position = "top") moves the legend above the plot.

Why move the legend?

  • When the legend is at the top, it is often easier to notice and read, especially in presentations.
ggplot(data, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3, alpha = 0.7) +
  labs(
    title = "Scatter Plot of Sepal Dimensions",
    x = "Sepal Length",
    y = "Sepal Width",
    color = "Species"
  ) +
  theme_minimal() +
  theme(legend.position = "top")


Program 3

Introduction

This document demonstrates how to create a time-series lines graph using the built-in AirPassengers dataset in R.

  • The dataset contains monthly airline passenger counts from 1949 to 1960.
  • We will convert the time-series object into a dataframe (because ggplot2 works best with dataframe).
  • We will visualize passenger trwnds over time using ggplot2.
  • We will draw separate lines for each year using group aesthetic (and color for easy comparison).

Step 1: Load necessary libraries

We load:

  • ggplot2 to create the line plot.
  • dplyr for optical data handling (filtering, summarising, etc).
  • tidyr for optional reshaping (not strictly required here, but commonly used in tidy workflows).
library(ggplot2)
library(dplyr)
library(tidyr)

Step 2: Load the Built-in AirPassengers` Dataset and Convert It to a Dataframe

Why Convert?

AirPassengers is a time-series (ts) object, not a dataframe.

ggplot2 expects data in a tabular structure, where:

  • each row is one observation
  • each column is one variable

So we build a dataframe with:

  • Data: a sequence of monthly dates from 1949-01 to 1960-12

  • Passengers: the numeric values from the time-series

  • Year: extracted from the date, used as the grouping variable (factor)

What This Code Is Doing (Line-by-Line)

  • seq(as.Date("1949-01-01"), by = "month", length.out = length(AirPassengers)) creates a monthly date sequence
# Create a monthly date sequence that matches AirPassengers length
date_seq <- seq(
  as.Date("1949-01-01"),
  by = "month",
  length.out = length(AirPassengers)
)

# Convert the time-series object into a dataframe for ggplot2
data <- data.frame(
  Date = date_seq,
  Passengers = as.numeric(AirPassengers),
  Year = as.factor(format(date_seq, "%Y"))
)

# Display first few rows
head(data, n=20)
         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: Understand the Data Structure We Created

Before plotting, it helps to confirm:

  • the types of columns (Data, numeric, factor)
  • the range of dates
  • how many months per year we have
str(data)
'data.frame':   144 obs. of  3 variables:
 $ Date      : Date, format: "1949-01-01" "1949-02-01" ...
 $ Passengers: num  112 118 132 129 121 135 148 148 136 119 ...
 $ Year      : Factor w/ 12 levels "1949","1950",..: 1 1 1 1 1 1 1 1 1 1 ...
# Check the earliest and latest dates
range(data$Date)
[1] "1949-01-01" "1960-12-01"
# How many months per year? (Should be 12 for each year)
table(data$Year)

1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 
  12   12   12   12   12   12   12   12   12   12   12   12 

Step 4: Define a Function to Create the Grouped Time-Series Line Graph

What the Function Inputs Mean

  1. data : the dataframe with time-series values
  2. x_col : name of the time column (e.g., "Date")
  3. y_col : name of the numeric column (e.g., "Passengers")
  4. group_col : name if the grouping column (e.g., "Year")
  5. title : custom plot title
plot_time_series <- function(data, x_col, y_col, group_col, title = "Air Passenger Trends") {
  ggplot(
    data,
    aes_string(
      x = x_col,
      y = y_col,
      color = group_col,
      group = group_col
    )
  ) +
    geom_line(size = 1.2) +
    geom_point(size = 2) +
    labs(
      title = title,
      x = "Date",
      y = "Number of Passengers",
      color = "Year"
    ) +
    theme_minimal() +
    theme(legend.position = "top")
}

Step 5: Call the Funtion to Generate the Plot

Here we use the function we created.

We pass:

  • "Date" as the time variable
  • "Passengers" as the values to plot
  • "Year" as the grouping variable
plot_time_series(
  data,
  "Date",
  "Passengers",
  "Year",
  "Trend of Airline Passengers Over Time"
)
Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
ℹ Please use tidy evaluation idioms with `aes()`.
ℹ See also `vignette("ggplot2-in-packages")` for more information.
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.


Program 4

Problem statement:

Develop a R script to produce a bar graph displaying the frequency distribution of categorical data, grouped by a specific variable using ggplot2


Step 1: Load required Libraries

#install.package(`ggplot2`)
library(ggplot2)

Step 2: Load and inspect the dataset

We load the builtin dataset mtcars and view the first few rows to understand its structure

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

Step 3: Exploratory data analysis

Before creating any visulization, we explore the dataset to understand the variable and types.

str(data)
'data.frame':   32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
summary(data)
      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  
  • str(data) helps us to identify the data types of each variable
  • summary(data) provides statistical summaries

Step 4: Convert the numeric variables to factors

To correctly visualize categorical data, we convert relevant variables into factors

data$cyl
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
table(data$cyl)

 4  6  8 
11  7 14 
data$gear
 [1] 4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4
table(data$gear)

 3  4  5 
15 12  5 
class(data$cyl)
[1] "numeric"
class(data$gear)
[1] "numeric"
data$cyl=as.factor(data$cyl)
data$gear=as.factor(data$gear)
class(data$cyl)
[1] "factor"
data$cyl
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
Levels: 4 6 8
class(data$gear)
[1] "factor"
data$gear
 [1] 4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4
Levels: 3 4 5
summary(data)
      mpg        cyl         disp             hp             drat      
 Min.   :10.40   4:11   Min.   : 71.1   Min.   : 52.0   Min.   :2.760  
 1st Qu.:15.43   6: 7   1st Qu.:120.8   1st Qu.: 96.5   1st Qu.:3.080  
 Median :19.20   8:14   Median :196.3   Median :123.0   Median :3.695  
 Mean   :20.09          Mean   :230.7   Mean   :146.7   Mean   :3.597  
 3rd Qu.:22.80          3rd Qu.:326.0   3rd Qu.:180.0   3rd Qu.:3.920  
 Max.   :33.90          Max.   :472.0   Max.   :335.0   Max.   :4.930  
       wt             qsec             vs               am         gear  
 Min.   :1.513   Min.   :14.50   Min.   :0.0000   Min.   :0.0000   3:15  
 1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000   1st Qu.:0.0000   4:12  
 Median :3.325   Median :17.71   Median :0.0000   Median :0.0000   5: 5  
 Mean   :3.217   Mean   :17.85   Mean   :0.4375   Mean   :0.4062         
 3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000   3rd Qu.:1.0000         
 Max.   :5.424   Max.   :22.90   Max.   :1.0000   Max.   :1.0000         
      carb      
 Min.   :1.000  
 1st Qu.:2.000  
 Median :2.000  
 Mean   :2.812  
 3rd Qu.:4.000  
 Max.   :8.000  
str(data)
'data.frame':   32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: Factor w/ 3 levels "3","4","5": 2 2 2 1 1 1 1 2 2 2 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

Step 5: Examine Frequency Distribution

Before plotting, we analyze how the data is distributed across categories

table(data$cyl)

 4  6  8 
11  7 14 
table(data$gear)

 3  4  5 
15 12  5 
table(data$cyl, data$gear)
   
     3  4  5
  4  1  8  2
  6  2  4  1
  8 12  0  2
  • Helps us to understand the count of each category
  • Provide insight into relationships between variables
  • prepares us for interpreting the visualization

Step 6: Create the bar graph

ggplot(data, aes(x=cyl, fill=gear))

ggplot(data, aes(x=cyl, fill=gear))+geom_bar()

ggplot(data, aes(x=cyl, fill=gear))+geom_bar(position="dodge")

ggplot(data, aes(x=cyl, fill=gear))+
  geom_bar(position="dodge")+
  theme_minimal()+
  theme(legend.position='top')

ggplot(data, aes(x=cyl, fill=gear))+
  geom_bar(position="dodge")+
  theme_minimal()+
  theme(legend.position='top')+
  labs(title="bar graph displaying the frequency distribution of categorical data", y="Count", x="Number of cylinders")


Program 5

Problem statement:

Implement a R program to create a histogram illustrating the distribution of a continuous variable, with overlays of density curves for each hroup, using ggplot2.


Step 1: Load Required Library

We first load the ggplot2 package, which is used for data visualization in R.

#load ggplot2 package for visualization
library(ggplot2)

Step 2: Explore the Inbuilt Dataset

We use the built-in dataset. In this dataset:

#use the built-in `iris` dataset
# `Petal.Length` is a continuous variable
# `Species` is a categorical grouping variable

str(iris)
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
head(iris, n=2)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
tail(iris)
    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
145          6.7         3.3          5.7         2.5 virginica
146          6.7         3.0          5.2         2.3 virginica
147          6.3         2.5          5.0         1.9 virginica
148          6.5         3.0          5.2         2.0 virginica
149          6.2         3.4          5.4         2.3 virginica
150          5.9         3.0          5.1         1.8 virginica
tail(iris, n=2)
    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
149          6.2         3.4          5.4         2.3 virginica
150          5.9         3.0          5.1         1.8 virginica

Step 3.1

# STart ggplot with iris dataset
# Map Petal.Length to x-axis and fill by Species

p <- ggplot(data = iris, aes(x = Petal.Length, fill = Species))
p

Step 3.2

# Add histogram with density scaling

p <- p + geom_histogram(aes(y = ..density..),
                        alpha = 0.4,
                        position = "identity",
                        bins= 30)
p
Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(density)` instead.

Explanation

Step 3.3

# Overlay density curves for each group

p <- p +
  geom_density(aes(color = Species),
               size = 1.2)
p

Step 3.4

Now we improve the appearance of the graph

# Add title and axis labels, and apply clean theme

p <- p + labs(
  title = "Distribution of Petal Length with Group-wise Density Curves",
  x= "Petal Length",
  y = "Density")+
  theme_minimal()

p


Program 6

Problem Statement:

Write an R script to construct a box plot showcasing the distribution of a continuous variable, grouped by a categorical variable, using ggplot2’s fill aesthetic.

Step 1: Load Required Libary

# Load ggplot2 package for visualization
library(ggplot2)

Step 2: Explore the Inbuilt Dataset

# Use the built-in `iris` dataset
# `Petal.Width` is a continuous variable
# `Species` is a categorical grouping variable

str(iris)
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

Step 3: Construct Box Plot with Grouping

Step 3.1: Initialize ggplot with Aesthetic

#Initialize ggplot with data and aesthetic mappings

p <- ggplot(data = iris, aes(x = Species, y = Petal.Width, fill + Species))

Step 3.2

# Add the box plot layer
p <- p + geom_boxplot()
p

p+ 
  theme_minimal()+
  theme(legend.position='top')+
  labs(title="IRIS.box plot", x='Species', y='Petals.Width')


Program 7

Program

Develop a function in R to plot a function curve based on a mathematical equation provided as input, with difference curve styles for each group, using ggplot2

Objective

To create a simple R script that:

Step 1: Load Required Library

# Load ggplot2 package for advanced plotting
library(ggplot2)

We use the ggplot2 package because it allows elegant and flexible plotting. It supports layering and grouping very well.

Step 2: Create Data for the functions

# Create a sequence of x values ranging from -2 to 2
x <- seq(-2*pi, 2*pi, length.out = 500)

# Evaluate sin(x) and cos(x) over the x range
y1 <- sin(x)
y2 <- cos(x)

# Combine data into one data frame
df <- data.frame(
  x = rep(x, 2),            # Repeat x values for both functions
  y = c(y1, y2),            # Combine y values: first sin(x), then cos(x)
  group = rep(c("sin(x)", "cos(x)"), each = length(x))
  # Label each row by function
)

df
               x             y  group
1    -6.28318531  2.449213e-16 sin(x)
2    -6.25800220  2.518045e-02 sin(x)
3    -6.23281909  5.034492e-02 sin(x)
4    -6.20763598  7.547747e-02 sin(x)
5    -6.18245288  1.005622e-01 sin(x)
6    -6.15726977  1.255831e-01 sin(x)
7    -6.13208666  1.505244e-01 sin(x)
8    -6.10690356  1.753702e-01 sin(x)
9    -6.08172045  2.001048e-01 sin(x)
10   -6.05653734  2.247125e-01 sin(x)
11   -6.03135423  2.491777e-01 sin(x)
12   -6.00617113  2.734849e-01 sin(x)
13   -5.98098802  2.976186e-01 sin(x)
14   -5.95580491  3.215637e-01 sin(x)
15   -5.93062180  3.453048e-01 sin(x)
16   -5.90543870  3.688269e-01 sin(x)
17   -5.88025559  3.921151e-01 sin(x)
18   -5.85507248  4.151547e-01 sin(x)
19   -5.82988937  4.379310e-01 sin(x)
20   -5.80470627  4.604296e-01 sin(x)
21   -5.77952316  4.826362e-01 sin(x)
22   -5.75434005  5.045367e-01 sin(x)
23   -5.72915694  5.261173e-01 sin(x)
24   -5.70397384  5.473642e-01 sin(x)
25   -5.67879073  5.682640e-01 sin(x)
26   -5.65360762  5.888035e-01 sin(x)
27   -5.62842451  6.089695e-01 sin(x)
28   -5.60324141  6.287494e-01 sin(x)
29   -5.57805830  6.481306e-01 sin(x)
30   -5.55287519  6.671007e-01 sin(x)
31   -5.52769208  6.856478e-01 sin(x)
32   -5.50250898  7.037601e-01 sin(x)
33   -5.47732587  7.214261e-01 sin(x)
34   -5.45214276  7.386346e-01 sin(x)
35   -5.42695965  7.553746e-01 sin(x)
36   -5.40177655  7.716357e-01 sin(x)
37   -5.37659344  7.874074e-01 sin(x)
38   -5.35141033  8.026798e-01 sin(x)
39   -5.32622722  8.174432e-01 sin(x)
40   -5.30104412  8.316882e-01 sin(x)
41   -5.27586101  8.454057e-01 sin(x)
42   -5.25067790  8.585872e-01 sin(x)
43   -5.22549479  8.712241e-01 sin(x)
44   -5.20031169  8.833086e-01 sin(x)
45   -5.17512858  8.948329e-01 sin(x)
46   -5.14994547  9.057897e-01 sin(x)
47   -5.12476236  9.161722e-01 sin(x)
48   -5.09957926  9.259736e-01 sin(x)
49   -5.07439615  9.351879e-01 sin(x)
50   -5.04921304  9.438090e-01 sin(x)
51   -5.02402993  9.518317e-01 sin(x)
52   -4.99884683  9.592507e-01 sin(x)
53   -4.97366372  9.660615e-01 sin(x)
54   -4.94848061  9.722596e-01 sin(x)
55   -4.92329751  9.778411e-01 sin(x)
56   -4.89811440  9.828026e-01 sin(x)
57   -4.87293129  9.871407e-01 sin(x)
58   -4.84774818  9.908529e-01 sin(x)
59   -4.82256508  9.939368e-01 sin(x)
60   -4.79738197  9.963903e-01 sin(x)
61   -4.77219886  9.982119e-01 sin(x)
62   -4.74701575  9.994006e-01 sin(x)
63   -4.72183265  9.999554e-01 sin(x)
64   -4.69664954  9.998761e-01 sin(x)
65   -4.67146643  9.991628e-01 sin(x)
66   -4.64628332  9.978158e-01 sin(x)
67   -4.62110022  9.958361e-01 sin(x)
68   -4.59591711  9.932248e-01 sin(x)
69   -4.57073400  9.899837e-01 sin(x)
70   -4.54555089  9.861148e-01 sin(x)
71   -4.52036779  9.816205e-01 sin(x)
72   -4.49518468  9.765037e-01 sin(x)
73   -4.47000157  9.707677e-01 sin(x)
74   -4.44481846  9.644161e-01 sin(x)
75   -4.41963536  9.574528e-01 sin(x)
76   -4.39445225  9.498824e-01 sin(x)
77   -4.36926914  9.417097e-01 sin(x)
78   -4.34408603  9.329397e-01 sin(x)
79   -4.31890293  9.235781e-01 sin(x)
80   -4.29371982  9.136308e-01 sin(x)
81   -4.26853671  9.031041e-01 sin(x)
82   -4.24335360  8.920047e-01 sin(x)
83   -4.21817050  8.803397e-01 sin(x)
84   -4.19298739  8.681164e-01 sin(x)
85   -4.16780428  8.553425e-01 sin(x)
86   -4.14262117  8.420263e-01 sin(x)
87   -4.11743807  8.281760e-01 sin(x)
88   -4.09225496  8.138006e-01 sin(x)
89   -4.06707185  7.989091e-01 sin(x)
90   -4.04188874  7.835109e-01 sin(x)
91   -4.01670564  7.676159e-01 sin(x)
92   -3.99152253  7.512341e-01 sin(x)
93   -3.96633942  7.343759e-01 sin(x)
94   -3.94115631  7.170520e-01 sin(x)
95   -3.91597321  6.992734e-01 sin(x)
96   -3.89079010  6.810513e-01 sin(x)
97   -3.86560699  6.623974e-01 sin(x)
98   -3.84042389  6.433233e-01 sin(x)
99   -3.81524078  6.238413e-01 sin(x)
100  -3.79005767  6.039637e-01 sin(x)
101  -3.76487456  5.837031e-01 sin(x)
102  -3.73969146  5.630723e-01 sin(x)
103  -3.71450835  5.420845e-01 sin(x)
104  -3.68932524  5.207529e-01 sin(x)
105  -3.66414213  4.990910e-01 sin(x)
106  -3.63895903  4.771127e-01 sin(x)
107  -3.61377592  4.548317e-01 sin(x)
108  -3.58859281  4.322624e-01 sin(x)
109  -3.56340970  4.094189e-01 sin(x)
110  -3.53822660  3.863158e-01 sin(x)
111  -3.51304349  3.629677e-01 sin(x)
112  -3.48786038  3.393894e-01 sin(x)
113  -3.46267727  3.155959e-01 sin(x)
114  -3.43749417  2.916023e-01 sin(x)
115  -3.41231106  2.674237e-01 sin(x)
116  -3.38712795  2.430756e-01 sin(x)
117  -3.36194484  2.185733e-01 sin(x)
118  -3.33676174  1.939324e-01 sin(x)
119  -3.31157863  1.691685e-01 sin(x)
120  -3.28639552  1.442974e-01 sin(x)
121  -3.26121241  1.193347e-01 sin(x)
122  -3.23602931  9.429635e-02 sin(x)
123  -3.21084620  6.919820e-02 sin(x)
124  -3.18566309  4.405617e-02 sin(x)
125  -3.16047998  1.888621e-02 sin(x)
126  -3.13529688 -6.295735e-03 sin(x)
127  -3.11011377 -3.147369e-02 sin(x)
128  -3.08493066 -5.663168e-02 sin(x)
129  -3.05974755 -8.175375e-02 sin(x)
130  -3.03456445 -1.068240e-01 sin(x)
131  -3.00938134 -1.318265e-01 sin(x)
132  -2.98419823 -1.567454e-01 sin(x)
133  -2.95901512 -1.815649e-01 sin(x)
134  -2.93383202 -2.062692e-01 sin(x)
135  -2.90864891 -2.308428e-01 sin(x)
136  -2.88346580 -2.552699e-01 sin(x)
137  -2.85828269 -2.795352e-01 sin(x)
138  -2.83309959 -3.036232e-01 sin(x)
139  -2.80791648 -3.275186e-01 sin(x)
140  -2.78273337 -3.512064e-01 sin(x)
141  -2.75755027 -3.746715e-01 sin(x)
142  -2.73236716 -3.978989e-01 sin(x)
143  -2.70718405 -4.208740e-01 sin(x)
144  -2.68200094 -4.435822e-01 sin(x)
145  -2.65681784 -4.660091e-01 sin(x)
146  -2.63163473 -4.881405e-01 sin(x)
147  -2.60645162 -5.099624e-01 sin(x)
148  -2.58126851 -5.314608e-01 sin(x)
149  -2.55608541 -5.526222e-01 sin(x)
150  -2.53090230 -5.734332e-01 sin(x)
151  -2.50571919 -5.938805e-01 sin(x)
152  -2.48053608 -6.139512e-01 sin(x)
153  -2.45535298 -6.336326e-01 sin(x)
154  -2.43016987 -6.529121e-01 sin(x)
155  -2.40498676 -6.717776e-01 sin(x)
156  -2.37980365 -6.902171e-01 sin(x)
157  -2.35462055 -7.082189e-01 sin(x)
158  -2.32943744 -7.257715e-01 sin(x)
159  -2.30425433 -7.428639e-01 sin(x)
160  -2.27907122 -7.594852e-01 sin(x)
161  -2.25388812 -7.756249e-01 sin(x)
162  -2.22870501 -7.912727e-01 sin(x)
163  -2.20352190 -8.064188e-01 sin(x)
164  -2.17833879 -8.210534e-01 sin(x)
165  -2.15315569 -8.351673e-01 sin(x)
166  -2.12797258 -8.487517e-01 sin(x)
167  -2.10278947 -8.617978e-01 sin(x)
168  -2.07760636 -8.742973e-01 sin(x)
169  -2.05242326 -8.862425e-01 sin(x)
170  -2.02724015 -8.976256e-01 sin(x)
171  -2.00205704 -9.084395e-01 sin(x)
172  -1.97687393 -9.186773e-01 sin(x)
173  -1.95169083 -9.283325e-01 sin(x)
174  -1.92650772 -9.373990e-01 sin(x)
175  -1.90132461 -9.458710e-01 sin(x)
176  -1.87614150 -9.537432e-01 sin(x)
177  -1.85095840 -9.610106e-01 sin(x)
178  -1.82577529 -9.676686e-01 sin(x)
179  -1.80059218 -9.737129e-01 sin(x)
180  -1.77540907 -9.791397e-01 sin(x)
181  -1.75022597 -9.839456e-01 sin(x)
182  -1.72504286 -9.881276e-01 sin(x)
183  -1.69985975 -9.916829e-01 sin(x)
184  -1.67467664 -9.946093e-01 sin(x)
185  -1.64949354 -9.969050e-01 sin(x)
186  -1.62431043 -9.985685e-01 sin(x)
187  -1.59912732 -9.995987e-01 sin(x)
188  -1.57394422 -9.999950e-01 sin(x)
189  -1.54876111 -9.997572e-01 sin(x)
190  -1.52357800 -9.988854e-01 sin(x)
191  -1.49839489 -9.973802e-01 sin(x)
192  -1.47321179 -9.952424e-01 sin(x)
193  -1.44802868 -9.924735e-01 sin(x)
194  -1.42284557 -9.890752e-01 sin(x)
195  -1.39766246 -9.850497e-01 sin(x)
196  -1.37247936 -9.803996e-01 sin(x)
197  -1.34729625 -9.751277e-01 sin(x)
198  -1.32211314 -9.692374e-01 sin(x)
199  -1.29693003 -9.627324e-01 sin(x)
200  -1.27174693 -9.556170e-01 sin(x)
201  -1.24656382 -9.478955e-01 sin(x)
202  -1.22138071 -9.395729e-01 sin(x)
203  -1.19619760 -9.306545e-01 sin(x)
204  -1.17101450 -9.211459e-01 sin(x)
205  -1.14583139 -9.110532e-01 sin(x)
206  -1.12064828 -9.003827e-01 sin(x)
207  -1.09546517 -8.891412e-01 sin(x)
208  -1.07028207 -8.773359e-01 sin(x)
209  -1.04509896 -8.649742e-01 sin(x)
210  -1.01991585 -8.520640e-01 sin(x)
211  -0.99473274 -8.386134e-01 sin(x)
212  -0.96954964 -8.246310e-01 sin(x)
213  -0.94436653 -8.101257e-01 sin(x)
214  -0.91918342 -7.951067e-01 sin(x)
215  -0.89400031 -7.795834e-01 sin(x)
216  -0.86881721 -7.635657e-01 sin(x)
217  -0.84363410 -7.470638e-01 sin(x)
218  -0.81845099 -7.300882e-01 sin(x)
219  -0.79326788 -7.126496e-01 sin(x)
220  -0.76808478 -6.947590e-01 sin(x)
221  -0.74290167 -6.764279e-01 sin(x)
222  -0.71771856 -6.576678e-01 sin(x)
223  -0.69253545 -6.384906e-01 sin(x)
224  -0.66735235 -6.189085e-01 sin(x)
225  -0.64216924 -5.989340e-01 sin(x)
226  -0.61698613 -5.785796e-01 sin(x)
227  -0.59180302 -5.578583e-01 sin(x)
228  -0.56661992 -5.367833e-01 sin(x)
229  -0.54143681 -5.153678e-01 sin(x)
230  -0.51625370 -4.936255e-01 sin(x)
231  -0.49107060 -4.715702e-01 sin(x)
232  -0.46588749 -4.492159e-01 sin(x)
233  -0.44070438 -4.265766e-01 sin(x)
234  -0.41552127 -4.036669e-01 sin(x)
235  -0.39033817 -3.805012e-01 sin(x)
236  -0.36515506 -3.570941e-01 sin(x)
237  -0.33997195 -3.334606e-01 sin(x)
238  -0.31478884 -3.096157e-01 sin(x)
239  -0.28960574 -2.855744e-01 sin(x)
240  -0.26442263 -2.613520e-01 sin(x)
241  -0.23923952 -2.369639e-01 sin(x)
242  -0.21405641 -2.124255e-01 sin(x)
243  -0.18887331 -1.877524e-01 sin(x)
244  -0.16369020 -1.629602e-01 sin(x)
245  -0.13850709 -1.380647e-01 sin(x)
246  -0.11332398 -1.130816e-01 sin(x)
247  -0.08814088 -8.802680e-02 sin(x)
248  -0.06295777 -6.291619e-02 sin(x)
249  -0.03777466 -3.776568e-02 sin(x)
250  -0.01259155 -1.259122e-02 sin(x)
251   0.01259155  1.259122e-02 sin(x)
252   0.03777466  3.776568e-02 sin(x)
253   0.06295777  6.291619e-02 sin(x)
254   0.08814088  8.802680e-02 sin(x)
255   0.11332398  1.130816e-01 sin(x)
256   0.13850709  1.380647e-01 sin(x)
257   0.16369020  1.629602e-01 sin(x)
258   0.18887331  1.877524e-01 sin(x)
259   0.21405641  2.124255e-01 sin(x)
260   0.23923952  2.369639e-01 sin(x)
261   0.26442263  2.613520e-01 sin(x)
262   0.28960574  2.855744e-01 sin(x)
263   0.31478884  3.096157e-01 sin(x)
264   0.33997195  3.334606e-01 sin(x)
265   0.36515506  3.570941e-01 sin(x)
266   0.39033817  3.805012e-01 sin(x)
267   0.41552127  4.036669e-01 sin(x)
268   0.44070438  4.265766e-01 sin(x)
269   0.46588749  4.492159e-01 sin(x)
270   0.49107060  4.715702e-01 sin(x)
271   0.51625370  4.936255e-01 sin(x)
272   0.54143681  5.153678e-01 sin(x)
273   0.56661992  5.367833e-01 sin(x)
274   0.59180302  5.578583e-01 sin(x)
275   0.61698613  5.785796e-01 sin(x)
276   0.64216924  5.989340e-01 sin(x)
277   0.66735235  6.189085e-01 sin(x)
278   0.69253545  6.384906e-01 sin(x)
279   0.71771856  6.576678e-01 sin(x)
280   0.74290167  6.764279e-01 sin(x)
281   0.76808478  6.947590e-01 sin(x)
282   0.79326788  7.126496e-01 sin(x)
283   0.81845099  7.300882e-01 sin(x)
284   0.84363410  7.470638e-01 sin(x)
285   0.86881721  7.635657e-01 sin(x)
286   0.89400031  7.795834e-01 sin(x)
287   0.91918342  7.951067e-01 sin(x)
288   0.94436653  8.101257e-01 sin(x)
289   0.96954964  8.246310e-01 sin(x)
290   0.99473274  8.386134e-01 sin(x)
291   1.01991585  8.520640e-01 sin(x)
292   1.04509896  8.649742e-01 sin(x)
293   1.07028207  8.773359e-01 sin(x)
294   1.09546517  8.891412e-01 sin(x)
295   1.12064828  9.003827e-01 sin(x)
296   1.14583139  9.110532e-01 sin(x)
297   1.17101450  9.211459e-01 sin(x)
298   1.19619760  9.306545e-01 sin(x)
299   1.22138071  9.395729e-01 sin(x)
300   1.24656382  9.478955e-01 sin(x)
301   1.27174693  9.556170e-01 sin(x)
302   1.29693003  9.627324e-01 sin(x)
303   1.32211314  9.692374e-01 sin(x)
304   1.34729625  9.751277e-01 sin(x)
305   1.37247936  9.803996e-01 sin(x)
306   1.39766246  9.850497e-01 sin(x)
307   1.42284557  9.890752e-01 sin(x)
308   1.44802868  9.924735e-01 sin(x)
309   1.47321179  9.952424e-01 sin(x)
310   1.49839489  9.973802e-01 sin(x)
311   1.52357800  9.988854e-01 sin(x)
312   1.54876111  9.997572e-01 sin(x)
313   1.57394422  9.999950e-01 sin(x)
314   1.59912732  9.995987e-01 sin(x)
315   1.62431043  9.985685e-01 sin(x)
316   1.64949354  9.969050e-01 sin(x)
317   1.67467664  9.946093e-01 sin(x)
318   1.69985975  9.916829e-01 sin(x)
319   1.72504286  9.881276e-01 sin(x)
320   1.75022597  9.839456e-01 sin(x)
321   1.77540907  9.791397e-01 sin(x)
322   1.80059218  9.737129e-01 sin(x)
323   1.82577529  9.676686e-01 sin(x)
324   1.85095840  9.610106e-01 sin(x)
325   1.87614150  9.537432e-01 sin(x)
326   1.90132461  9.458710e-01 sin(x)
327   1.92650772  9.373990e-01 sin(x)
328   1.95169083  9.283325e-01 sin(x)
329   1.97687393  9.186773e-01 sin(x)
330   2.00205704  9.084395e-01 sin(x)
331   2.02724015  8.976256e-01 sin(x)
332   2.05242326  8.862425e-01 sin(x)
333   2.07760636  8.742973e-01 sin(x)
334   2.10278947  8.617978e-01 sin(x)
335   2.12797258  8.487517e-01 sin(x)
336   2.15315569  8.351673e-01 sin(x)
337   2.17833879  8.210534e-01 sin(x)
338   2.20352190  8.064188e-01 sin(x)
339   2.22870501  7.912727e-01 sin(x)
340   2.25388812  7.756249e-01 sin(x)
341   2.27907122  7.594852e-01 sin(x)
342   2.30425433  7.428639e-01 sin(x)
343   2.32943744  7.257715e-01 sin(x)
344   2.35462055  7.082189e-01 sin(x)
345   2.37980365  6.902171e-01 sin(x)
346   2.40498676  6.717776e-01 sin(x)
347   2.43016987  6.529121e-01 sin(x)
348   2.45535298  6.336326e-01 sin(x)
349   2.48053608  6.139512e-01 sin(x)
350   2.50571919  5.938805e-01 sin(x)
351   2.53090230  5.734332e-01 sin(x)
352   2.55608541  5.526222e-01 sin(x)
353   2.58126851  5.314608e-01 sin(x)
354   2.60645162  5.099624e-01 sin(x)
355   2.63163473  4.881405e-01 sin(x)
356   2.65681784  4.660091e-01 sin(x)
357   2.68200094  4.435822e-01 sin(x)
358   2.70718405  4.208740e-01 sin(x)
359   2.73236716  3.978989e-01 sin(x)
360   2.75755027  3.746715e-01 sin(x)
361   2.78273337  3.512064e-01 sin(x)
362   2.80791648  3.275186e-01 sin(x)
363   2.83309959  3.036232e-01 sin(x)
364   2.85828269  2.795352e-01 sin(x)
365   2.88346580  2.552699e-01 sin(x)
366   2.90864891  2.308428e-01 sin(x)
367   2.93383202  2.062692e-01 sin(x)
368   2.95901512  1.815649e-01 sin(x)
369   2.98419823  1.567454e-01 sin(x)
370   3.00938134  1.318265e-01 sin(x)
371   3.03456445  1.068240e-01 sin(x)
372   3.05974755  8.175375e-02 sin(x)
373   3.08493066  5.663168e-02 sin(x)
374   3.11011377  3.147369e-02 sin(x)
375   3.13529688  6.295735e-03 sin(x)
376   3.16047998 -1.888621e-02 sin(x)
377   3.18566309 -4.405617e-02 sin(x)
378   3.21084620 -6.919820e-02 sin(x)
379   3.23602931 -9.429635e-02 sin(x)
380   3.26121241 -1.193347e-01 sin(x)
381   3.28639552 -1.442974e-01 sin(x)
382   3.31157863 -1.691685e-01 sin(x)
383   3.33676174 -1.939324e-01 sin(x)
384   3.36194484 -2.185733e-01 sin(x)
385   3.38712795 -2.430756e-01 sin(x)
386   3.41231106 -2.674237e-01 sin(x)
387   3.43749417 -2.916023e-01 sin(x)
388   3.46267727 -3.155959e-01 sin(x)
389   3.48786038 -3.393894e-01 sin(x)
390   3.51304349 -3.629677e-01 sin(x)
391   3.53822660 -3.863158e-01 sin(x)
392   3.56340970 -4.094189e-01 sin(x)
393   3.58859281 -4.322624e-01 sin(x)
394   3.61377592 -4.548317e-01 sin(x)
395   3.63895903 -4.771127e-01 sin(x)
396   3.66414213 -4.990910e-01 sin(x)
397   3.68932524 -5.207529e-01 sin(x)
398   3.71450835 -5.420845e-01 sin(x)
399   3.73969146 -5.630723e-01 sin(x)
400   3.76487456 -5.837031e-01 sin(x)
401   3.79005767 -6.039637e-01 sin(x)
402   3.81524078 -6.238413e-01 sin(x)
403   3.84042389 -6.433233e-01 sin(x)
404   3.86560699 -6.623974e-01 sin(x)
405   3.89079010 -6.810513e-01 sin(x)
406   3.91597321 -6.992734e-01 sin(x)
407   3.94115631 -7.170520e-01 sin(x)
408   3.96633942 -7.343759e-01 sin(x)
409   3.99152253 -7.512341e-01 sin(x)
410   4.01670564 -7.676159e-01 sin(x)
411   4.04188874 -7.835109e-01 sin(x)
412   4.06707185 -7.989091e-01 sin(x)
413   4.09225496 -8.138006e-01 sin(x)
414   4.11743807 -8.281760e-01 sin(x)
415   4.14262117 -8.420263e-01 sin(x)
416   4.16780428 -8.553425e-01 sin(x)
417   4.19298739 -8.681164e-01 sin(x)
418   4.21817050 -8.803397e-01 sin(x)
419   4.24335360 -8.920047e-01 sin(x)
420   4.26853671 -9.031041e-01 sin(x)
421   4.29371982 -9.136308e-01 sin(x)
422   4.31890293 -9.235781e-01 sin(x)
423   4.34408603 -9.329397e-01 sin(x)
424   4.36926914 -9.417097e-01 sin(x)
425   4.39445225 -9.498824e-01 sin(x)
426   4.41963536 -9.574528e-01 sin(x)
427   4.44481846 -9.644161e-01 sin(x)
428   4.47000157 -9.707677e-01 sin(x)
429   4.49518468 -9.765037e-01 sin(x)
430   4.52036779 -9.816205e-01 sin(x)
431   4.54555089 -9.861148e-01 sin(x)
432   4.57073400 -9.899837e-01 sin(x)
433   4.59591711 -9.932248e-01 sin(x)
434   4.62110022 -9.958361e-01 sin(x)
435   4.64628332 -9.978158e-01 sin(x)
436   4.67146643 -9.991628e-01 sin(x)
437   4.69664954 -9.998761e-01 sin(x)
438   4.72183265 -9.999554e-01 sin(x)
439   4.74701575 -9.994006e-01 sin(x)
440   4.77219886 -9.982119e-01 sin(x)
441   4.79738197 -9.963903e-01 sin(x)
442   4.82256508 -9.939368e-01 sin(x)
443   4.84774818 -9.908529e-01 sin(x)
444   4.87293129 -9.871407e-01 sin(x)
445   4.89811440 -9.828026e-01 sin(x)
446   4.92329751 -9.778411e-01 sin(x)
447   4.94848061 -9.722596e-01 sin(x)
448   4.97366372 -9.660615e-01 sin(x)
449   4.99884683 -9.592507e-01 sin(x)
450   5.02402993 -9.518317e-01 sin(x)
451   5.04921304 -9.438090e-01 sin(x)
452   5.07439615 -9.351879e-01 sin(x)
453   5.09957926 -9.259736e-01 sin(x)
454   5.12476236 -9.161722e-01 sin(x)
455   5.14994547 -9.057897e-01 sin(x)
456   5.17512858 -8.948329e-01 sin(x)
457   5.20031169 -8.833086e-01 sin(x)
458   5.22549479 -8.712241e-01 sin(x)
459   5.25067790 -8.585872e-01 sin(x)
460   5.27586101 -8.454057e-01 sin(x)
461   5.30104412 -8.316882e-01 sin(x)
462   5.32622722 -8.174432e-01 sin(x)
463   5.35141033 -8.026798e-01 sin(x)
464   5.37659344 -7.874074e-01 sin(x)
465   5.40177655 -7.716357e-01 sin(x)
466   5.42695965 -7.553746e-01 sin(x)
467   5.45214276 -7.386346e-01 sin(x)
468   5.47732587 -7.214261e-01 sin(x)
469   5.50250898 -7.037601e-01 sin(x)
470   5.52769208 -6.856478e-01 sin(x)
471   5.55287519 -6.671007e-01 sin(x)
472   5.57805830 -6.481306e-01 sin(x)
473   5.60324141 -6.287494e-01 sin(x)
474   5.62842451 -6.089695e-01 sin(x)
475   5.65360762 -5.888035e-01 sin(x)
476   5.67879073 -5.682640e-01 sin(x)
477   5.70397384 -5.473642e-01 sin(x)
478   5.72915694 -5.261173e-01 sin(x)
479   5.75434005 -5.045367e-01 sin(x)
480   5.77952316 -4.826362e-01 sin(x)
481   5.80470627 -4.604296e-01 sin(x)
482   5.82988937 -4.379310e-01 sin(x)
483   5.85507248 -4.151547e-01 sin(x)
484   5.88025559 -3.921151e-01 sin(x)
485   5.90543870 -3.688269e-01 sin(x)
486   5.93062180 -3.453048e-01 sin(x)
487   5.95580491 -3.215637e-01 sin(x)
488   5.98098802 -2.976186e-01 sin(x)
489   6.00617113 -2.734849e-01 sin(x)
490   6.03135423 -2.491777e-01 sin(x)
491   6.05653734 -2.247125e-01 sin(x)
492   6.08172045 -2.001048e-01 sin(x)
493   6.10690356 -1.753702e-01 sin(x)
494   6.13208666 -1.505244e-01 sin(x)
495   6.15726977 -1.255831e-01 sin(x)
496   6.18245288 -1.005622e-01 sin(x)
497   6.20763598 -7.547747e-02 sin(x)
498   6.23281909 -5.034492e-02 sin(x)
499   6.25800220 -2.518045e-02 sin(x)
500   6.28318531 -2.449213e-16 sin(x)
501  -6.28318531  1.000000e+00 cos(x)
502  -6.25800220  9.996829e-01 cos(x)
503  -6.23281909  9.987319e-01 cos(x)
504  -6.20763598  9.971475e-01 cos(x)
505  -6.18245288  9.949308e-01 cos(x)
506  -6.15726977  9.920831e-01 cos(x)
507  -6.13208666  9.886063e-01 cos(x)
508  -6.10690356  9.845026e-01 cos(x)
509  -6.08172045  9.797745e-01 cos(x)
510  -6.05653734  9.744251e-01 cos(x)
511  -6.03135423  9.684578e-01 cos(x)
512  -6.00617113  9.618763e-01 cos(x)
513  -5.98098802  9.546848e-01 cos(x)
514  -5.95580491  9.468880e-01 cos(x)
515  -5.93062180  9.384906e-01 cos(x)
516  -5.90543870  9.294981e-01 cos(x)
517  -5.88025559  9.199162e-01 cos(x)
518  -5.85507248  9.097508e-01 cos(x)
519  -5.82988937  8.990086e-01 cos(x)
520  -5.80470627  8.876962e-01 cos(x)
521  -5.77952316  8.758210e-01 cos(x)
522  -5.75434005  8.633903e-01 cos(x)
523  -5.72915694  8.504120e-01 cos(x)
524  -5.70397384  8.368945e-01 cos(x)
525  -5.67879073  8.228463e-01 cos(x)
526  -5.65360762  8.082762e-01 cos(x)
527  -5.62842451  7.931936e-01 cos(x)
528  -5.60324141  7.776080e-01 cos(x)
529  -5.57805830  7.615292e-01 cos(x)
530  -5.55287519  7.449676e-01 cos(x)
531  -5.52769208  7.279335e-01 cos(x)
532  -5.50250898  7.104377e-01 cos(x)
533  -5.47732587  6.924915e-01 cos(x)
534  -5.45214276  6.741061e-01 cos(x)
535  -5.42695965  6.552932e-01 cos(x)
536  -5.40177655  6.360647e-01 cos(x)
537  -5.37659344  6.164329e-01 cos(x)
538  -5.35141033  5.964102e-01 cos(x)
539  -5.32622722  5.760092e-01 cos(x)
540  -5.30104412  5.552430e-01 cos(x)
541  -5.27586101  5.341247e-01 cos(x)
542  -5.25067790  5.126676e-01 cos(x)
543  -5.22549479  4.908855e-01 cos(x)
544  -5.20031169  4.687920e-01 cos(x)
545  -5.17512858  4.464013e-01 cos(x)
546  -5.14994547  4.237274e-01 cos(x)
547  -5.12476236  4.007849e-01 cos(x)
548  -5.09957926  3.775882e-01 cos(x)
549  -5.07439615  3.541520e-01 cos(x)
550  -5.04921304  3.304913e-01 cos(x)
551  -5.02402993  3.066210e-01 cos(x)
552  -4.99884683  2.825562e-01 cos(x)
553  -4.97366372  2.583122e-01 cos(x)
554  -4.94848061  2.339045e-01 cos(x)
555  -4.92329751  2.093484e-01 cos(x)
556  -4.89811440  1.846595e-01 cos(x)
557  -4.87293129  1.598536e-01 cos(x)
558  -4.84774818  1.349462e-01 cos(x)
559  -4.82256508  1.099533e-01 cos(x)
560  -4.79738197  8.489070e-02 cos(x)
561  -4.77219886  5.977423e-02 cos(x)
562  -4.74701575  3.461985e-02 cos(x)
563  -4.72183265  9.443525e-03 cos(x)
564  -4.69664954 -1.573879e-02 cos(x)
565  -4.67146643 -4.091113e-02 cos(x)
566  -4.64628332 -6.605752e-02 cos(x)
567  -4.62110022 -9.116202e-02 cos(x)
568  -4.59591711 -1.162087e-01 cos(x)
569  -4.57073400 -1.411817e-01 cos(x)
570  -4.54555089 -1.660652e-01 cos(x)
571  -4.52036779 -1.908433e-01 cos(x)
572  -4.49518468 -2.155005e-01 cos(x)
573  -4.47000157 -2.400209e-01 cos(x)
574  -4.44481846 -2.643892e-01 cos(x)
575  -4.41963536 -2.885898e-01 cos(x)
576  -4.39445225 -3.126074e-01 cos(x)
577  -4.36926914 -3.364267e-01 cos(x)
578  -4.34408603 -3.600327e-01 cos(x)
579  -4.31890293 -3.834104e-01 cos(x)
580  -4.29371982 -4.065449e-01 cos(x)
581  -4.26853671 -4.294216e-01 cos(x)
582  -4.24335360 -4.520260e-01 cos(x)
583  -4.21817050 -4.743438e-01 cos(x)
584  -4.19298739 -4.963607e-01 cos(x)
585  -4.16780428 -5.180629e-01 cos(x)
586  -4.14262117 -5.394365e-01 cos(x)
587  -4.11743807 -5.604681e-01 cos(x)
588  -4.09225496 -5.811442e-01 cos(x)
589  -4.06707185 -6.014518e-01 cos(x)
590  -4.04188874 -6.213780e-01 cos(x)
591  -4.01670564 -6.409101e-01 cos(x)
592  -3.99152253 -6.600358e-01 cos(x)
593  -3.96633942 -6.787430e-01 cos(x)
594  -3.94115631 -6.970197e-01 cos(x)
595  -3.91597321 -7.148543e-01 cos(x)
596  -3.89079010 -7.322357e-01 cos(x)
597  -3.86560699 -7.491527e-01 cos(x)
598  -3.84042389 -7.655946e-01 cos(x)
599  -3.81524078 -7.815510e-01 cos(x)
600  -3.79005767 -7.970118e-01 cos(x)
601  -3.76487456 -8.119672e-01 cos(x)
602  -3.73969146 -8.264076e-01 cos(x)
603  -3.71450835 -8.403240e-01 cos(x)
604  -3.68932524 -8.537075e-01 cos(x)
605  -3.66414213 -8.665496e-01 cos(x)
606  -3.63895903 -8.788421e-01 cos(x)
607  -3.61377592 -8.905774e-01 cos(x)
608  -3.58859281 -9.017479e-01 cos(x)
609  -3.56340970 -9.123465e-01 cos(x)
610  -3.53822660 -9.223666e-01 cos(x)
611  -3.51304349 -9.318017e-01 cos(x)
612  -3.48786038 -9.406460e-01 cos(x)
613  -3.46267727 -9.488937e-01 cos(x)
614  -3.43749417 -9.565396e-01 cos(x)
615  -3.41231106 -9.635790e-01 cos(x)
616  -3.38712795 -9.700073e-01 cos(x)
617  -3.36194484 -9.758205e-01 cos(x)
618  -3.33676174 -9.810149e-01 cos(x)
619  -3.31157863 -9.855871e-01 cos(x)
620  -3.28639552 -9.895344e-01 cos(x)
621  -3.26121241 -9.928541e-01 cos(x)
622  -3.23602931 -9.955442e-01 cos(x)
623  -3.21084620 -9.976029e-01 cos(x)
624  -3.18566309 -9.990291e-01 cos(x)
625  -3.16047998 -9.998216e-01 cos(x)
626  -3.13529688 -9.999802e-01 cos(x)
627  -3.11011377 -9.995046e-01 cos(x)
628  -3.08493066 -9.983951e-01 cos(x)
629  -3.05974755 -9.966526e-01 cos(x)
630  -3.03456445 -9.942779e-01 cos(x)
631  -3.00938134 -9.912728e-01 cos(x)
632  -2.98419823 -9.876390e-01 cos(x)
633  -2.95901512 -9.833790e-01 cos(x)
634  -2.93383202 -9.784953e-01 cos(x)
635  -2.90864891 -9.729911e-01 cos(x)
636  -2.88346580 -9.668698e-01 cos(x)
637  -2.85828269 -9.601354e-01 cos(x)
638  -2.83309959 -9.527922e-01 cos(x)
639  -2.80791648 -9.448447e-01 cos(x)
640  -2.78273337 -9.362981e-01 cos(x)
641  -2.75755027 -9.271576e-01 cos(x)
642  -2.73236716 -9.174293e-01 cos(x)
643  -2.70718405 -9.071191e-01 cos(x)
644  -2.68200094 -8.962337e-01 cos(x)
645  -2.65681784 -8.847799e-01 cos(x)
646  -2.63163473 -8.727650e-01 cos(x)
647  -2.60645162 -8.601967e-01 cos(x)
648  -2.58126851 -8.470829e-01 cos(x)
649  -2.55608541 -8.334319e-01 cos(x)
650  -2.53090230 -8.192523e-01 cos(x)
651  -2.50571919 -8.045533e-01 cos(x)
652  -2.48053608 -7.893440e-01 cos(x)
653  -2.45535298 -7.736341e-01 cos(x)
654  -2.43016987 -7.574337e-01 cos(x)
655  -2.40498676 -7.407529e-01 cos(x)
656  -2.37980365 -7.236024e-01 cos(x)
657  -2.35462055 -7.059930e-01 cos(x)
658  -2.32943744 -6.879358e-01 cos(x)
659  -2.30425433 -6.694425e-01 cos(x)
660  -2.27907122 -6.505245e-01 cos(x)
661  -2.25388812 -6.311941e-01 cos(x)
662  -2.22870501 -6.114634e-01 cos(x)
663  -2.20352190 -5.913449e-01 cos(x)
664  -2.17833879 -5.708514e-01 cos(x)
665  -2.15315569 -5.499959e-01 cos(x)
666  -2.12797258 -5.287916e-01 cos(x)
667  -2.10278947 -5.072520e-01 cos(x)
668  -2.07760636 -4.853907e-01 cos(x)
669  -2.05242326 -4.632216e-01 cos(x)
670  -2.02724015 -4.407588e-01 cos(x)
671  -2.00205704 -4.180164e-01 cos(x)
672  -1.97687393 -3.950090e-01 cos(x)
673  -1.95169083 -3.717510e-01 cos(x)
674  -1.92650772 -3.482573e-01 cos(x)
675  -1.90132461 -3.245428e-01 cos(x)
676  -1.87614150 -3.006224e-01 cos(x)
677  -1.85095840 -2.765114e-01 cos(x)
678  -1.82577529 -2.522251e-01 cos(x)
679  -1.80059218 -2.277788e-01 cos(x)
680  -1.77540907 -2.031880e-01 cos(x)
681  -1.75022597 -1.784684e-01 cos(x)
682  -1.72504286 -1.536356e-01 cos(x)
683  -1.69985975 -1.287054e-01 cos(x)
684  -1.67467664 -1.036936e-01 cos(x)
685  -1.64949354 -7.861600e-02 cos(x)
686  -1.62431043 -5.348857e-02 cos(x)
687  -1.59912732 -2.832721e-02 cos(x)
688  -1.57394422 -3.147883e-03 cos(x)
689  -1.54876111  2.203344e-02 cos(x)
690  -1.52357800  4.720078e-02 cos(x)
691  -1.49839489  7.233820e-02 cos(x)
692  -1.47321179  9.742974e-02 cos(x)
693  -1.44802868  1.224595e-01 cos(x)
694  -1.42284557  1.474116e-01 cos(x)
695  -1.39766246  1.722702e-01 cos(x)
696  -1.37247936  1.970196e-01 cos(x)
697  -1.34729625  2.216440e-01 cos(x)
698  -1.32211314  2.461279e-01 cos(x)
699  -1.29693003  2.704557e-01 cos(x)
700  -1.27174693  2.946119e-01 cos(x)
701  -1.24656382  3.185814e-01 cos(x)
702  -1.22138071  3.423488e-01 cos(x)
703  -1.19619760  3.658991e-01 cos(x)
704  -1.17101450  3.892174e-01 cos(x)
705  -1.14583139  4.122888e-01 cos(x)
706  -1.12064828  4.350988e-01 cos(x)
707  -1.09546517  4.576329e-01 cos(x)
708  -1.07028207  4.798768e-01 cos(x)
709  -1.04509896  5.018163e-01 cos(x)
710  -1.01991585  5.234377e-01 cos(x)
711  -0.99473274  5.447270e-01 cos(x)
712  -0.96954964  5.656710e-01 cos(x)
713  -0.94436653  5.862562e-01 cos(x)
714  -0.91918342  6.064696e-01 cos(x)
715  -0.89400031  6.262985e-01 cos(x)
716  -0.86881721  6.457301e-01 cos(x)
717  -0.84363410  6.647523e-01 cos(x)
718  -0.81845099  6.833529e-01 cos(x)
719  -0.79326788  7.015202e-01 cos(x)
720  -0.76808478  7.192426e-01 cos(x)
721  -0.74290167  7.365089e-01 cos(x)
722  -0.71771856  7.533081e-01 cos(x)
723  -0.69253545  7.696296e-01 cos(x)
724  -0.66735235  7.854631e-01 cos(x)
725  -0.64216924  8.007984e-01 cos(x)
726  -0.61698613  8.156259e-01 cos(x)
727  -0.59180302  8.299362e-01 cos(x)
728  -0.56661992  8.437202e-01 cos(x)
729  -0.54143681  8.569691e-01 cos(x)
730  -0.51625370  8.696745e-01 cos(x)
731  -0.49107060  8.818285e-01 cos(x)
732  -0.46588749  8.934232e-01 cos(x)
733  -0.44070438  9.044514e-01 cos(x)
734  -0.41552127  9.149060e-01 cos(x)
735  -0.39033817  9.247804e-01 cos(x)
736  -0.36515506  9.340684e-01 cos(x)
737  -0.33997195  9.427640e-01 cos(x)
738  -0.31478884  9.508618e-01 cos(x)
739  -0.28960574  9.583565e-01 cos(x)
740  -0.26442263  9.652436e-01 cos(x)
741  -0.23923952  9.715185e-01 cos(x)
742  -0.21405641  9.771773e-01 cos(x)
743  -0.18887331  9.822164e-01 cos(x)
744  -0.16369020  9.866326e-01 cos(x)
745  -0.13850709  9.904232e-01 cos(x)
746  -0.11332398  9.935857e-01 cos(x)
747  -0.08814088  9.961181e-01 cos(x)
748  -0.06295777  9.980188e-01 cos(x)
749  -0.03777466  9.992866e-01 cos(x)
750  -0.01259155  9.999207e-01 cos(x)
751   0.01259155  9.999207e-01 cos(x)
752   0.03777466  9.992866e-01 cos(x)
753   0.06295777  9.980188e-01 cos(x)
754   0.08814088  9.961181e-01 cos(x)
755   0.11332398  9.935857e-01 cos(x)
756   0.13850709  9.904232e-01 cos(x)
757   0.16369020  9.866326e-01 cos(x)
758   0.18887331  9.822164e-01 cos(x)
759   0.21405641  9.771773e-01 cos(x)
760   0.23923952  9.715185e-01 cos(x)
761   0.26442263  9.652436e-01 cos(x)
762   0.28960574  9.583565e-01 cos(x)
763   0.31478884  9.508618e-01 cos(x)
764   0.33997195  9.427640e-01 cos(x)
765   0.36515506  9.340684e-01 cos(x)
766   0.39033817  9.247804e-01 cos(x)
767   0.41552127  9.149060e-01 cos(x)
768   0.44070438  9.044514e-01 cos(x)
769   0.46588749  8.934232e-01 cos(x)
770   0.49107060  8.818285e-01 cos(x)
771   0.51625370  8.696745e-01 cos(x)
772   0.54143681  8.569691e-01 cos(x)
773   0.56661992  8.437202e-01 cos(x)
774   0.59180302  8.299362e-01 cos(x)
775   0.61698613  8.156259e-01 cos(x)
776   0.64216924  8.007984e-01 cos(x)
777   0.66735235  7.854631e-01 cos(x)
778   0.69253545  7.696296e-01 cos(x)
779   0.71771856  7.533081e-01 cos(x)
780   0.74290167  7.365089e-01 cos(x)
781   0.76808478  7.192426e-01 cos(x)
782   0.79326788  7.015202e-01 cos(x)
783   0.81845099  6.833529e-01 cos(x)
784   0.84363410  6.647523e-01 cos(x)
785   0.86881721  6.457301e-01 cos(x)
786   0.89400031  6.262985e-01 cos(x)
787   0.91918342  6.064696e-01 cos(x)
788   0.94436653  5.862562e-01 cos(x)
789   0.96954964  5.656710e-01 cos(x)
790   0.99473274  5.447270e-01 cos(x)
791   1.01991585  5.234377e-01 cos(x)
792   1.04509896  5.018163e-01 cos(x)
793   1.07028207  4.798768e-01 cos(x)
794   1.09546517  4.576329e-01 cos(x)
795   1.12064828  4.350988e-01 cos(x)
796   1.14583139  4.122888e-01 cos(x)
797   1.17101450  3.892174e-01 cos(x)
798   1.19619760  3.658991e-01 cos(x)
799   1.22138071  3.423488e-01 cos(x)
800   1.24656382  3.185814e-01 cos(x)
801   1.27174693  2.946119e-01 cos(x)
802   1.29693003  2.704557e-01 cos(x)
803   1.32211314  2.461279e-01 cos(x)
804   1.34729625  2.216440e-01 cos(x)
805   1.37247936  1.970196e-01 cos(x)
806   1.39766246  1.722702e-01 cos(x)
807   1.42284557  1.474116e-01 cos(x)
808   1.44802868  1.224595e-01 cos(x)
809   1.47321179  9.742974e-02 cos(x)
810   1.49839489  7.233820e-02 cos(x)
811   1.52357800  4.720078e-02 cos(x)
812   1.54876111  2.203344e-02 cos(x)
813   1.57394422 -3.147883e-03 cos(x)
814   1.59912732 -2.832721e-02 cos(x)
815   1.62431043 -5.348857e-02 cos(x)
816   1.64949354 -7.861600e-02 cos(x)
817   1.67467664 -1.036936e-01 cos(x)
818   1.69985975 -1.287054e-01 cos(x)
819   1.72504286 -1.536356e-01 cos(x)
820   1.75022597 -1.784684e-01 cos(x)
821   1.77540907 -2.031880e-01 cos(x)
822   1.80059218 -2.277788e-01 cos(x)
823   1.82577529 -2.522251e-01 cos(x)
824   1.85095840 -2.765114e-01 cos(x)
825   1.87614150 -3.006224e-01 cos(x)
826   1.90132461 -3.245428e-01 cos(x)
827   1.92650772 -3.482573e-01 cos(x)
828   1.95169083 -3.717510e-01 cos(x)
829   1.97687393 -3.950090e-01 cos(x)
830   2.00205704 -4.180164e-01 cos(x)
831   2.02724015 -4.407588e-01 cos(x)
832   2.05242326 -4.632216e-01 cos(x)
833   2.07760636 -4.853907e-01 cos(x)
834   2.10278947 -5.072520e-01 cos(x)
835   2.12797258 -5.287916e-01 cos(x)
836   2.15315569 -5.499959e-01 cos(x)
837   2.17833879 -5.708514e-01 cos(x)
838   2.20352190 -5.913449e-01 cos(x)
839   2.22870501 -6.114634e-01 cos(x)
840   2.25388812 -6.311941e-01 cos(x)
841   2.27907122 -6.505245e-01 cos(x)
842   2.30425433 -6.694425e-01 cos(x)
843   2.32943744 -6.879358e-01 cos(x)
844   2.35462055 -7.059930e-01 cos(x)
845   2.37980365 -7.236024e-01 cos(x)
846   2.40498676 -7.407529e-01 cos(x)
847   2.43016987 -7.574337e-01 cos(x)
848   2.45535298 -7.736341e-01 cos(x)
849   2.48053608 -7.893440e-01 cos(x)
850   2.50571919 -8.045533e-01 cos(x)
851   2.53090230 -8.192523e-01 cos(x)
852   2.55608541 -8.334319e-01 cos(x)
853   2.58126851 -8.470829e-01 cos(x)
854   2.60645162 -8.601967e-01 cos(x)
855   2.63163473 -8.727650e-01 cos(x)
856   2.65681784 -8.847799e-01 cos(x)
857   2.68200094 -8.962337e-01 cos(x)
858   2.70718405 -9.071191e-01 cos(x)
859   2.73236716 -9.174293e-01 cos(x)
860   2.75755027 -9.271576e-01 cos(x)
861   2.78273337 -9.362981e-01 cos(x)
862   2.80791648 -9.448447e-01 cos(x)
863   2.83309959 -9.527922e-01 cos(x)
864   2.85828269 -9.601354e-01 cos(x)
865   2.88346580 -9.668698e-01 cos(x)
866   2.90864891 -9.729911e-01 cos(x)
867   2.93383202 -9.784953e-01 cos(x)
868   2.95901512 -9.833790e-01 cos(x)
869   2.98419823 -9.876390e-01 cos(x)
870   3.00938134 -9.912728e-01 cos(x)
871   3.03456445 -9.942779e-01 cos(x)
872   3.05974755 -9.966526e-01 cos(x)
873   3.08493066 -9.983951e-01 cos(x)
874   3.11011377 -9.995046e-01 cos(x)
875   3.13529688 -9.999802e-01 cos(x)
876   3.16047998 -9.998216e-01 cos(x)
877   3.18566309 -9.990291e-01 cos(x)
878   3.21084620 -9.976029e-01 cos(x)
879   3.23602931 -9.955442e-01 cos(x)
880   3.26121241 -9.928541e-01 cos(x)
881   3.28639552 -9.895344e-01 cos(x)
882   3.31157863 -9.855871e-01 cos(x)
883   3.33676174 -9.810149e-01 cos(x)
884   3.36194484 -9.758205e-01 cos(x)
885   3.38712795 -9.700073e-01 cos(x)
886   3.41231106 -9.635790e-01 cos(x)
887   3.43749417 -9.565396e-01 cos(x)
888   3.46267727 -9.488937e-01 cos(x)
889   3.48786038 -9.406460e-01 cos(x)
890   3.51304349 -9.318017e-01 cos(x)
891   3.53822660 -9.223666e-01 cos(x)
892   3.56340970 -9.123465e-01 cos(x)
893   3.58859281 -9.017479e-01 cos(x)
894   3.61377592 -8.905774e-01 cos(x)
895   3.63895903 -8.788421e-01 cos(x)
896   3.66414213 -8.665496e-01 cos(x)
897   3.68932524 -8.537075e-01 cos(x)
898   3.71450835 -8.403240e-01 cos(x)
899   3.73969146 -8.264076e-01 cos(x)
900   3.76487456 -8.119672e-01 cos(x)
901   3.79005767 -7.970118e-01 cos(x)
902   3.81524078 -7.815510e-01 cos(x)
903   3.84042389 -7.655946e-01 cos(x)
904   3.86560699 -7.491527e-01 cos(x)
905   3.89079010 -7.322357e-01 cos(x)
906   3.91597321 -7.148543e-01 cos(x)
907   3.94115631 -6.970197e-01 cos(x)
908   3.96633942 -6.787430e-01 cos(x)
909   3.99152253 -6.600358e-01 cos(x)
910   4.01670564 -6.409101e-01 cos(x)
911   4.04188874 -6.213780e-01 cos(x)
912   4.06707185 -6.014518e-01 cos(x)
913   4.09225496 -5.811442e-01 cos(x)
914   4.11743807 -5.604681e-01 cos(x)
915   4.14262117 -5.394365e-01 cos(x)
916   4.16780428 -5.180629e-01 cos(x)
917   4.19298739 -4.963607e-01 cos(x)
918   4.21817050 -4.743438e-01 cos(x)
919   4.24335360 -4.520260e-01 cos(x)
920   4.26853671 -4.294216e-01 cos(x)
921   4.29371982 -4.065449e-01 cos(x)
922   4.31890293 -3.834104e-01 cos(x)
923   4.34408603 -3.600327e-01 cos(x)
924   4.36926914 -3.364267e-01 cos(x)
925   4.39445225 -3.126074e-01 cos(x)
926   4.41963536 -2.885898e-01 cos(x)
927   4.44481846 -2.643892e-01 cos(x)
928   4.47000157 -2.400209e-01 cos(x)
929   4.49518468 -2.155005e-01 cos(x)
930   4.52036779 -1.908433e-01 cos(x)
931   4.54555089 -1.660652e-01 cos(x)
932   4.57073400 -1.411817e-01 cos(x)
933   4.59591711 -1.162087e-01 cos(x)
934   4.62110022 -9.116202e-02 cos(x)
935   4.64628332 -6.605752e-02 cos(x)
936   4.67146643 -4.091113e-02 cos(x)
937   4.69664954 -1.573879e-02 cos(x)
938   4.72183265  9.443525e-03 cos(x)
939   4.74701575  3.461985e-02 cos(x)
940   4.77219886  5.977423e-02 cos(x)
941   4.79738197  8.489070e-02 cos(x)
942   4.82256508  1.099533e-01 cos(x)
943   4.84774818  1.349462e-01 cos(x)
944   4.87293129  1.598536e-01 cos(x)
945   4.89811440  1.846595e-01 cos(x)
946   4.92329751  2.093484e-01 cos(x)
947   4.94848061  2.339045e-01 cos(x)
948   4.97366372  2.583122e-01 cos(x)
949   4.99884683  2.825562e-01 cos(x)
950   5.02402993  3.066210e-01 cos(x)
951   5.04921304  3.304913e-01 cos(x)
952   5.07439615  3.541520e-01 cos(x)
953   5.09957926  3.775882e-01 cos(x)
954   5.12476236  4.007849e-01 cos(x)
955   5.14994547  4.237274e-01 cos(x)
956   5.17512858  4.464013e-01 cos(x)
957   5.20031169  4.687920e-01 cos(x)
958   5.22549479  4.908855e-01 cos(x)
959   5.25067790  5.126676e-01 cos(x)
960   5.27586101  5.341247e-01 cos(x)
961   5.30104412  5.552430e-01 cos(x)
962   5.32622722  5.760092e-01 cos(x)
963   5.35141033  5.964102e-01 cos(x)
964   5.37659344  6.164329e-01 cos(x)
965   5.40177655  6.360647e-01 cos(x)
966   5.42695965  6.552932e-01 cos(x)
967   5.45214276  6.741061e-01 cos(x)
968   5.47732587  6.924915e-01 cos(x)
969   5.50250898  7.104377e-01 cos(x)
970   5.52769208  7.279335e-01 cos(x)
971   5.55287519  7.449676e-01 cos(x)
972   5.57805830  7.615292e-01 cos(x)
973   5.60324141  7.776080e-01 cos(x)
974   5.62842451  7.931936e-01 cos(x)
975   5.65360762  8.082762e-01 cos(x)
976   5.67879073  8.228463e-01 cos(x)
977   5.70397384  8.368945e-01 cos(x)
978   5.72915694  8.504120e-01 cos(x)
979   5.75434005  8.633903e-01 cos(x)
980   5.77952316  8.758210e-01 cos(x)
981   5.80470627  8.876962e-01 cos(x)
982   5.82988937  8.990086e-01 cos(x)
983   5.85507248  9.097508e-01 cos(x)
984   5.88025559  9.199162e-01 cos(x)
985   5.90543870  9.294981e-01 cos(x)
986   5.93062180  9.384906e-01 cos(x)
987   5.95580491  9.468880e-01 cos(x)
988   5.98098802  9.546848e-01 cos(x)
989   6.00617113  9.618763e-01 cos(x)
990   6.03135423  9.684578e-01 cos(x)
991   6.05653734  9.744251e-01 cos(x)
992   6.08172045  9.797745e-01 cos(x)
993   6.10690356  9.845026e-01 cos(x)
994   6.13208666  9.886063e-01 cos(x)
995   6.15726977  9.920831e-01 cos(x)
996   6.18245288  9.949308e-01 cos(x)
997   6.20763598  9.971475e-01 cos(x)
998   6.23281909  9.987319e-01 cos(x)
999   6.25800220  9.996829e-01 cos(x)
1000  6.28318531  1.000000e+00 cos(x)

Step 3:

Step 3.1: Initialize the ggplot Object

# Start building the ggplot using the data frame and aesthetics

p <- ggplot(df, aes(x = x, y = y, color = group, linetype = group))
p

Step 3.2: Add the Line Geometry

# Add smooth lines to represent each function cruve

p <- p + geom_line(size = 1.2)
p

Step 3.3

# Add title, ax is labels, and legends

p <- p + labs(title = "Funtion Curves: sin(x) and cos(x)",
              x = "x",
              y = "y = f(x)",
              color = "Function",
              linetype = "Function")
p

Step 3.4: Apply a Clean Theme

# Use a clean and simple background theme

p <- p + theme_minimal()
p

# Display the complete plot
p=p+theme(legend.position = 'top')
p