PART B

 9)


--- title: "prg9" author: "Kiran T L" format: docx editor: visual ---

Objectives

Create multiple histograms using ggplot2::facet_wrap() to visualize how a variable (e.g., Sepal.Length) is distrubuted across different groups (e.g., Species) in a built-in R dataset.

{r} #Load the ggplot2 pckage library(ggplot2)

Step 1: Load and Explore the Dateset

{r} # Load the iris databset data("iris") # View the first few row of the datset head(iris)

Step 2: Create grouped histograms using Facet_wrap


{r} #Create histogram using Facet_wrap for grouped data ggplot(iris, aes(x = Sepal.Length))+ geom_histogram(binwidth = 0.3,fill = "skyblue", color = "black")+ facet_wrap(~ Species)+ labs(title = "Distribution of sepal length by species", x = "Sepal Length (cm)", y = "Frequency")+ theme_minimal()


10)

# Install package (only first time)
install.packages("ggplot2")

# Load library
library(ggplot2)

# -------------------------------
# Function to Draw Density Curves
# -------------------------------

density_plot <- function(data, variable, group_var) {
  
  ggplot(data, aes(x = .data[[variable]], 
                   color = .data[[group_var]], 
                   fill = .data[[group_var]])) +
    
    geom_density(alpha = 0.3, size = 1) +
    
    labs(
      title = "Density Curve by Group",
      x = variable,
      y = "Density",
      color = "Group",
      fill = "Group"
    ) +
    
    theme_minimal()
}

# -----------------------------------
# Example using built-in iris dataset
# -----------------------------------

density_plot(iris, "Sepal.Length", "Species")


11)

--- title: "program 11" author: "Kiran T L" format: docx editor: visual ---

Objectives

To generate a basic box plot using ggplt2, enhanced with notches and outlies , and grouped by a categorical variable using an in-built dataset in R

Step 1: Load Required Package

we use the ggplot2 package for data visuallization. If it's not already installed , you can install it using:

{r} #insatll.package("ggplot2") # uncomment if nedded library(ggplot2)

Step 2: Use an inbuilt dataset

We will use the built-in iris dataset. This dataset contains measurement of sepal and petal dimentions for three species of iris flowers:

  • setosa

  • versicolor

  • virginica

{r} #Load and preview the dataset data(iris) head(iris) str(iris)

the Species column is categorical, making it suitable for grouping , while Sepal.Length is a numeric variable we'll analyze.

Step 3: Create a notched box plot grouped by species

We now creat a box plot for Sepal.Length , grouped by Species. We'll enhance the plot using: - Notches to show the confidence interval around the median - outlier highlighting using color and shape - Aeshetic enhancements like fill color and theme


{r} ggplot(iris, aes(c = Species, y = Sepal.Length)) + geom_boxplot( notch = TRUE, notchwidth = 0.6, outlier.colour = "red", outlier.shape = 16, fill = "skyblue", alpha = 0.7 ) + labs( title = "Sepal length distribution by iris species", sutitle = "Box plot with notches and outlier highlighting", x = "species", y = "Sepal length (cm)" )+ theme_minimal()

12)

--- title: "program 12" author: "Kiran T L" format: docx editor: visual ---
{r} library(ggplot2)
{r} data("iris")

{r} ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + geom_violin() + labs(title = "Violin Plot of Sepal Length by Species", x = "Species", y = "Sepal Length") + theme_minimal() theme(legend.position = "top")

13)

--- title: "prg 13" author: "Kiran TL" format: docx editor: visual ---
{r} library(ggplot2) library(dplyr)
{r} data("ToothGrowth")
{r} ToothGrowth$dose <- as.factor(ToothGrowth$dose)

{r} ggplot(ToothGrowth, aes(x = len, y = dose)) + geom_dotplot(binaxis = "x", stackdir = "center", dotsize = 0.7, fill = "blue") + labs(title = "Dot Plot of Tooth Growth by Dose", x = "Tooth Length", y = "Dose") + theme_minimal()


14)

--- title: "prg 14" author: "Kiran T L" format: docx editor: visual ---̥

Develop a script in R to calculate and visualize a correlation matrix for a given dataset, with color-coded cells indicating the strength and direction of correlations, using ggplot2's geom_tile function.

{r} library(ggplot2) library(tidyr) library(dplyr)

Dataset

we use the biult-in tcars dataset.

{r} head(mtcars)
{r} # Use built-in mtcars dataset data("mtcars") # comute corelation matrix cor_matrix = cor(mtcars) cor_matrix
{r} #convert matrix to a data frame for plotting cor_df = as.data.frame(as.table(cor_matrix)) head(cor_df)

Explanation :

  • cor(mtcars) computes pairwise correlation.

  • as.table() flattens the matrix intoo a long-format table.

  • the results has 3 column:Var1, Var2, and the correlation value (Freq).

Step 2: Visualize using ggplot2::geom_tile


{r} ggplot(cor_df,aes(x = Var1, y = Var2, fill = Freq))+ geom_tile(color = "white") + scale_fill_gradient2( low = "blue", mid = "white", high = "red", midpoint = 0,limit = c(-1,1), name = "correlation" )+ geom_text(aes(label = round(Freq, 2)),size = 3)+ theme_minimal()+ labs( title = "correlation matrix(mtcars)", x = "",y= "" )+ theme(axis.text.x = element_text(angle = 45,hjust =1))

Comments

Popular posts from this blog

..

PART A