program15

Author

G.GEYA 1NT23IS079

Develop an R program to generate facet_wrap histograms,Density Plot Function,Boxplot with Notch and Outliers,Violin Plot,Correlation Matrix Heatmap,Dot Plot with Grouped Variables

step 1: load 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:Multiple Histograms using facet_wrap

data(iris)
p1 <- ggplot(iris, aes(x = Sepal.Length)) +
  geom_histogram(binwidth = 0.3, fill = "skyblue", color = "black") +
  facet_wrap(~ Species) +
  labs(title = "Histogram: Sepal Length by Species", x = "Sepal Length", y = "Frequency") +
  theme_minimal()
p1

step3:Density Plot 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()
}
p2 <- 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.
p2

step 4:Boxplot with Notch and Outliers

p3 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot(notch = TRUE, notchwidth = 0.77, staplewidth = 0.3,
               fill = "grey", color = "black", outlier.shape = 15, outlier.color = "yellow") +
  labs(title = "Boxplot: Sepal Length by Species", x = "Species", y = "Sepal Length") +
  theme_minimal()
p3

step 5:Violin Plot

mtcars$cyl <- as.factor(mtcars$cyl)
p4 <- ggplot(mtcars, aes(x = cyl, y = mpg, fill = cyl)) +
  geom_violin(trim = FALSE) +
  labs(title = "Violin Plot: MPG by Cylinder", x = "Cylinders", y = "Miles Per Gallon") +
  theme_minimal()
p4

step 6:Dot Plot with Grouped Variables

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

step 7:Correlation Matrix Heatmap

data(mtcars)

cor_matrix <-cor(mtcars)
head(cor_matrix)
            mpg        cyl       disp         hp       drat         wt
mpg   1.0000000 -0.8521620 -0.8475514 -0.7761684  0.6811719 -0.8676594
cyl  -0.8521620  1.0000000  0.9020329  0.8324475 -0.6999381  0.7824958
disp -0.8475514  0.9020329  1.0000000  0.7909486 -0.7102139  0.8879799
hp   -0.7761684  0.8324475  0.7909486  1.0000000 -0.4487591  0.6587479
drat  0.6811719 -0.6999381 -0.7102139 -0.4487591  1.0000000 -0.7124406
wt   -0.8676594  0.7824958  0.8879799  0.6587479 -0.7124406  1.0000000
            qsec         vs         am       gear       carb
mpg   0.41868403  0.6640389  0.5998324  0.4802848 -0.5509251
cyl  -0.59124207 -0.8108118 -0.5226070 -0.4926866  0.5269883
disp -0.43369788 -0.7104159 -0.5912270 -0.5555692  0.3949769
hp   -0.70822339 -0.7230967 -0.2432043 -0.1257043  0.7498125
drat  0.09120476  0.4402785  0.7127111  0.6996101 -0.0907898
wt   -0.17471588 -0.5549157 -0.6924953 -0.5832870  0.4276059
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
# Ensure Var1 and Var2 are treated as factors (important for axis ordering in geom_tile)
cor_df$Var1 <- as.factor(cor_df$Var1)
cor_df$Var2 <- as.factor(cor_df$Var2)

# Plot
p6 <- ggplot(cor_df, aes(x = Var1, y = Var2, fill = Freq)) +
  geom_tile(color = "black") +
  scale_fill_gradient2(
    low = "violet", mid = "white", high = "red",
    midpoint = 0, limit = c(-1, 1), name = "Correlation"
  ) +
  geom_text(aes(label = round(Freq, 2)), size = 3) +
  labs(
    title = "Correlation Matrix Heatmap",
    x = "Variable 1",
    y = "Variable 2"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(p6)