Program 15

Author

Manjunath R

PROGRAM 15

Step 1: Load the Library.

# Load the 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)
library(reshape2)

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

    smiths

Step 2: Load Data set.

# Preview the dataset
data(iris)
data(ToothGrowth)

Cundefined

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

ggplot(iris, aes(x = Sepal.Length, color = Species, fill = Species)) +
  geom_density(alpha = 0.4) +
  theme_minimal() +
  labs(title = "Density Plot of Sepal Length by Species")

ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
  geom_boxplot(notch = TRUE, outlier.color = "red") +
  theme_minimal() +
  labs(title = "Boxplot of Sepal Length by Species")

ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
  geom_violin(trim = FALSE) +
  theme_minimal() +
  labs(title = "Violin Plot of Sepal Length by Species")

ggplot(ToothGrowth, aes(x = factor(dose), y = len, color = supp)) +
  geom_dotplot(binaxis = 'y', stackdir = 'center', position = position_dodge(0.8), dotsize = 0.6) +
  theme_minimal() +
  labs(title = "Dot Plot of Tooth Length", x = "Dose", y = "Tooth Length")
Bin width defaults to 1/30 of the range of the data. Pick better value with
`binwidth`.

# Prepare correlation matrix using iris numeric columns
iris_num <- iris[, 1:4]
cor_matrix <- round(cor(iris_num), 2)

# Convert to long format using tidyr::pivot_longer
cor_df <- as.data.frame(as.table(cor_matrix))

# Plot correlation heatmap
ggplot(cor_df, aes(Var1, Var2, fill = Freq)) +
  geom_tile(color = "white") +
  geom_text(aes(label = Freq), size = 4, color = "black") +
  scale_fill_gradient2(low = "red", high = "green", mid = "white",
                       midpoint = 0, limit = c(-1, 1), name = "Correlation") +
  theme_minimal() +
  labs(title = "Correlation Matrix of Iris Features", x = "", y = "") +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))