prog15

Author

Reshma

Program 9

Create multiple histogram using ggplot2::facet_wrap() to visualize how a varible(e.g, Sepal.length) is distributed across different groups in a built in R dataset

Step 1:Load the 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

Step 2 :Load dataset

data(iris)
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 :create hostogram using face wrap

ggplot(iris, aes(x = Sepal.Length)) +
  geom_histogram(binwidth = 0.3, color = "black", fill = "skyblue", alpha = 0.7) +
  facet_wrap(~ Species) +
  labs(title = "Distribution of Sepal Length by Species",
       x = "Sepal Length",
       y = "Frequency") +
  theme_minimal()

program 10

Develop an R function to draw a density curve representing the probability density function of a continuous variable, with separte curve for each group, using ggplot2

step 1 :Load required libraries

library(ggplot2)

Step 2: Define the Function

plot_density_by_group <- function(data, numeric_var, group_var, fill = TRUE) {
  ggplot(data, aes_string(x = numeric_var, color = group_var, fill = group_var)) +
    geom_density(alpha = if (fill) 0.4 else 0) +
    labs(
      title = paste("Density Plot of", numeric_var, "by", group_var),
      x = numeric_var,
      y = "Density"
    ) +
    theme_minimal()
}

Step 3: Load Dataset

data(iris)
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 4: Call the Function with Example

plot_density_by_group(iris, "Sepal.Length", "Species")
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.

Program 11

To generate a basic box plot using ggplot2, enhanced with notcjes and ouliers , and grouped by a categorical variables using an in-built dataset in R

step1:Load library

library(ggplot2)

Step 2:Required dataset

data(iris)
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
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 ...

Step3:Create a box plot with notches

ggplot(iris, aes(x = Species, y = Sepal.Length))+
  geom_boxplot(
    notch = TRUE,
    notchwidth = 0.5,
    outlier.colour = "black",
    fill = "pink" ) +
  labs(title = "basic box plot",
       x = "Species",
       y = "Sepal.Length")+
  theme_minimal()

Program 12

Develop a script in R to create a violin plot displaying distribution of a continuous varible, with separate violins for each group using ggplot2

Step 1:Load Required library

library(ggplot2)

Step 2:Load dataset

data(iris)
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
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 ...

Step 3:Creating violin using ggplot2

ggplot(iris, aes(x = Species , y = Sepal.Length , fill = Species))+
  geom_violin(trim = FALSE, alpha=0.6, color= "black")+
    labs(title = "Distributions of petal length by iris Species",
       x = "Species",
       y = "Petal.Length")+
  theme_minimal(base_size = 14)

Program 13

Write an R program to create multiple dot plot for grouped data, comparing the distribution of variable across different categories using ggplot2

Step 1:Load Required library

library(ggplot2)

Step 2:Load dataset

data(iris)
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
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 ...

Step 3:Creating dotplot

ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
  geom_dotplot(
    binaxis = "y", 
    stackdir = "center",
    dotsize = 0.7,
    alpha = 0.6, 
    color = "black"
  ) +
  labs(
    title = "Distribution of Sepal Length by Iris Species",
    x = "Species",
    y = "Sepal Length"
  ) +
  theme_minimal(base_size = 14)
Bin width defaults to 1/30 of the range of the data. Pick better value with
`binwidth`.

Develop an R program to calculate and visualize a correlation matrix for a given dataset, with color-coded cells indicating the strength and direction of the correlation using the ggplot2 package and the geom_tile() function.

step 1:- load the require liabry

library(ggplot2)
library(tidyr)
library(dplyr)
library(reshape2)

Attaching package: 'reshape2'
The following object is masked from 'package:tidyr':

    smiths

step 2:- preview the dataset

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
cor_matrix <- cor(mtcars)
cor_df <- as.data.frame(as.table(cor_matrix))
head(cor_df)
  Var1 Var2       Freq
1  mpg  mpg  1.0000000
2  cyl  mpg -0.8521620
3 disp  mpg -0.8475514
4   hp  mpg -0.7761684
5 drat  mpg  0.6811719
6   wt  mpg -0.8676594
data <- mtcars

# Step 1: Calculate the correlation matrix
cor_matrix <- cor(data)

# Step 2: Convert the matrix to a long format data frame
cor_df <- melt(cor_matrix)
ggplot(cor_df, aes(x = Var1, y = Var2, fill = value)) +
  geom_tile(color = "white") +
  geom_text(aes(label = round(value, 2)), color = "black", size = 3.5) +
  
  scale_fill_gradient2(
    low = "green",
    high = "pink",
    mid = "white",
    midpoint = 0,
    limit = c(-1, 1),
    name = "Correlation"
  ) +
  theme_minimal() +
  coord_fixed() +
  labs(
    title = "Correlation Matrix Heatmap with Values",
    x = "",
    y = ""
  ) +
  theme(
    axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)
  )