8. Write an R program loading a dataset and demonstrating a scatter plot,line plot , histogram,box plot using grouped data and curve style for mathematical equation provided of built in datasets in R studio.
Step 1:Loading Required Libraries
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: Creating a Scatter Plot with Built-in Dataset
iris_data <- irisggplot(iris_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")
Step 3: Creating a Line Graph for Time Series of Built-in Dataset
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 ="right")}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.
Step 4: Histogram using Built-in Dataset
ggplot(iris_data, aes(x = Petal.Length, fill = Species)) +geom_histogram(aes(y =after_stat(density)), alpha =0.4, position ="identity", bins =30) +geom_density(aes(color = Species), linewidth =1.2) +labs(title ="Distribution of Petal Length with Density Curves",x ="Petal Length", y ="Density", fill ="Species") +theme_minimal()
Step 5: Creating a Bar Graph with Built-in Dataset
data<-mtcarsdata$cyl<-as.factor(data$cyl)ggplot(data, aes(x=cyl,fill=cyl))+geom_bar(position="dodge")+#dodge:pillars will be side wiselabs(title="Frequency of cylinders grouped by gear type",x="Number of cylinders",y="Count",fill="cyl")+theme_minimal()
Step 6:Box Plot with grouping of built-in Dataset
p<-ggplot(data=iris,aes(x=Species,y=Petal.Width,fill=Species))#intializing aestheticsp <- p+geom_boxplot()p <- p+labs(title="Box Plot of Petal Width by Species",x="Species",y="Petal Width")+theme_minimal() #adds labels and title to graphp
Step 7: Create Curve Style Graph
x <-seq(-2*pi, 2*pi, length.out =500)#equation y1 <-sin(x)y2 <-cos(x)df <-data.frame(x =rep(x,2),y =c(y1, y2),group =rep(c("sin(x)", "cos(x)"), each =length(x)))#equation solved and graph got plottedp <-ggplot(df, aes(x = x, y = y, color = group, linetype = group))p <- p +geom_line(size =1.2)p <- p +labs(title ="function curves : sin(x) and cos(x)",x ="x",y ="y = f(x)",color ="Function",linetype ="Function")p<-p +theme_minimal()p