ggplot(summary_data, aes(x = cyl, y=avg_mpg, fill = cyl)) +geom_bar(stat ="identity")+labs(title ="Average MPG by cyclinder Count", x="Number of Cyclinders", y="Average MPG" ) +theme_minimal()
Program-02
Write an R script to create a scatter plot, incorporating categorical analysis through color-coded data points representing different groups, using ggplot2.
Step 1: Load necessary libraries.
library(ggplot2) library(dplyr)
Step 2: Load the Dataset.
Explanation:
The iris dataset contains 150 samples of iris flowers categorized into three species: setosa, versicolor, and virginica.
Differentiates three species using distinct colors
Customization
geom_point(size = 3, alpha = 0.7): Increases the size of points and makes them slightly transparent.
labs(): Adds a title and axis labels.
theme_minimal(): Uses a clean background for readability
theme(legend.position = "top"): Moves the legend to the top.
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-03
Implement an R function to generate a line graph depicting the trend of a time-series dataset, with separate lines for each group, utilizing ggplot2’s group aesthetic.
Introduction.
This document demonstrates how to create a time-series line graph using the built-in AirPassengers dataset in R.
The dataset contains monthly airline passenger counts from 1949 to 1960. We will use ggplot2 to visualize trends, with separate lines for each year.
Step 1: Load necessary libraries.
library(ggplot2) library(dplyr) library(tidyr)
Step 2 : Load the Built-in AirPassengers Dataset.
The AirPassengers dataset is a time series object in R.
We first convert it into a dataframe to use it with ggplot2.
Date: Represents the month and year (from January 1949 to December 1960).
Passengers: Monthly airline passenger counts.
Year: Extracted year from the date column, which will be used to group the data.
data <-data.frame( Date =seq(as.Date("1949-01-01"), by ="month", length.out =length(AirPassengers)), Passengers =as.numeric(AirPassengers), Year =as.factor(format(seq(as.Date("1949-01-01"), by ="month", length.out =length(AirPassengers)), "%Y"))) head(data, n=20)
Step 3: Define a Function for Time-Series Line Graph.
We define a function to create a time-series line graph where:
The x-axis represents time (Date).
The y-axis represents the number of passengers (Passengers).
Each year has a separate line to compare trends.
Function Inputs
data – The dataset containing time-series data.
x_col – The column representing time (Date).
y_col – The column representing values (Passengers).
group_col – The categorical variable for grouping (Year).
title – Custom plot title.
Features of the Line Graph
Group-based Visualization:
Each year has a distinct line color.
The group aesthetic ensures lines are drawn separately for each year.
geom_line(size = 1.2)
Adds a smooth line for trend analysis.
geom_point(size = 2)
Highlights individual data points.
theme_minimal() & theme(legend.position = “top”)
Enhances readability with a clean layout.
Moves legend to the top for better visualization.
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 ="Year", y ="Number of Passengers", color ="Year") +theme_minimal() +theme(legend.position ="top")} 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-04
Develop a script in R to produce a bar graph displaying the frequency distribution of categorical data in a given dataset, grouped by a specific variable, using ggplot2.
library(ggplot2)
Step 1: Load the Datatset.
We use the built-in mtcars dataset, which contains information about different car models.
ggplot2 traets factors as categories, making it easy to group and visualize.
Step 3: Create a Bar Graph
We now create a bar plot to show the frequency distribution of cyl, grouped by gear.
ggplot(data, aes(x=cyl,fill=gear)) +geom_bar(position ="dodge") +labs(title ="Frequenncy of Cyclinders Grouped by Gear Type", x ="number of cyclinder", y ="count", fill ="Gears") +theme_minimal()
Program-05
Implement an R program to create a histogram Illustrating the distribution of a continuous variable, with overlays of density curves for each group, using ggplot2.
Step 3: Create Histogarm with Group_wise censity Curves.
Step 3.1: Initialize the ggplot with aesthetic mappings.
p <-ggplot(data = iris, aes(x = Petal.Length, fill = Species)) p
Step 3.2: Add Histogram Layer.
p <- p +geom_histogram(aes(y = ..density..),alpha =0.4, # Set transparency position ="identity",# Overlap histograms bins =30) # Number of binsp
Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(density)` instead.
Step 3.3: Add Density Curve Layer.
p <- p +geom_density(aes(color = Species), size =1.2) p
Step 3.4: Add Labels and Theme.
p <- p +labs( title ="Distribution of Petal Length with Group-wise Density Curves", x ="Petal Length", y ="Density")+theme_minimal() p
Step 3.5: Display the Plot.
Program-06
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 3.1: Initialize ggplot with Aesthetic Mappings.
p <-ggplot(data = iris, aes(x = Species, y = Petal.Width, fill = Species))
Explanation:
x = Species: Grouping variable (categorical)
y = Petal.Width: Continuous variable to show distribution
fill = Species: Fill box colors by species
Step 3.2: Add Box Plot Layer.
p <- p +geom_boxplot()
Explanation:
geom_boxplot() creates box plots for each group
Automatically shows median, quartiles, and outliers
Step 3.3: Add Labels and Theme.
p <- p +labs(title ="Box Plot of Petal Width by Species", x ="Species", y ="Petal Width") +theme_minimal()
Explanation:
labs() adds a descriptive title and axis labels
theme_minimal() gives a clean, modern look
Step 3.4: Display the Plot.
p
Program-07
jStep 1: Load Required Library.
library(ggplot2)
Step 2: Create data for the functions.
x <-seq(-2* pi, 2* pi, length.out =500) y1 <-sin(x) y2 <-cos(x) df <-data.frame( x =c(x, x), y =c(y1, y2), group =rep(c("sin(x)", "cos(x)"), each =length(x)) )
Step-3: Createa Function to construct a graph
Step-3.1: Initialize the ggplot Object.
p <-ggplot(df, aes(x = x, y = y, color = group, linetype = group))
Skp-3.2: Add the line Geometry.
p <- p +geom_line(size =1.2)
Step 3: 3: Add plot Labels.
p <- p +labs( title ="Function Curves: sin(x) and cos(x)", x ="x", y ="f(x)", color ="Function", linetype ="Function") p