program15

Author

Swathi_1nt23is228

Compilation of programs from 9 to 14

Create multiple histograms using ggplot2::facet_wrap() to visualize how a variable is distributed across different groups in a built-in R data set

Step 1: Load the necessary library

library(ggplot2)

Step 2: Load the data set

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 histogram of sepal length

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.Develop an R program to draw a density curve representing the probability density of a continuous variable, with separate curves for each group , using ggplot2

Step 1: Load required library

library(ggplot2)

Step 2: Define the function

plot_density_by_group <- function(data, continuous_var, group_var, fill_colors = NULL) {
  # Check if the specified columns exist
  if (!(continuous_var %in% names(data)) || !(group_var %in% names(data))) {
    stop("Invalid column names. Make sure both variables exist in the dataset.")
  }

  # Create the ggplot object
  p <- ggplot(data, aes_string(x = continuous_var, color = group_var, fill = group_var)) +
    geom_density(alpha = 0.4) +
    labs(title = paste("Density Plot of", continuous_var, "by", group_var),
         x = continuous_var,
         y = "Density") +
    theme_minimal()

  # Apply custom fill colors if provided
  if (!is.null(fill_colors)) {
    p <- p + scale_fill_manual(values = fill_colors) +
             scale_color_manual(values = fill_colors)
  }

  # Return the plot
  return(p)
}

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

# Basic usage
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.

Step 5: Example with Custom Colors

# Define custom colors
custom_colors <- c("setosa" = "steelblue",
                   "versicolor" = "forestgreen",
                   "virginica" = "darkorange")

# Plot with custom colors
plot_density_by_group(iris, "Petal.Length", "Species", fill_colors = custom_colors)

11. To generate a basic box plot using ggplot2, enhanced with notches and outliners , and grouped by a categorical variable using an in-built dataset in R

Step 1 : Load the library

library(ggplot2)

Step 2: Load and explore the 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: Create a box plot

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

12. Develop a script in R to create a violin plot displaying the distribution of a continous variable with a separate violins for each group using ggplot2.

Step1- load the library

library(ggplot2)

Step2- Create the violin plot

violin_plot <- ggplot(data = iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
  geom_violin(trim = FALSE) +  # Show full distribution without trimming tails
  geom_boxplot(width = 0.1, fill = "white") +  # Add a boxplot inside violins
  labs(title = "Distribution of Sepal Length by Species",
       x = "Species",
       y = "Sepal Length") +
  theme_minimal() +
  theme(legend.position = "none")

Step3- Display the plot

print(violin_plot)

Write an R program to create multiple dot plots or group data , comparing the distributions of variables across different categories, using ggplot2’s position_dodge function

Step1: Load the library

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

Step2: Explore the data-set

data(ToothGrowth)
head(ToothGrowth)
   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5
5  6.4   VC  0.5
6 10.0   VC  0.5
str(ToothGrowth)
'data.frame':   60 obs. of  3 variables:
 $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
 $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
 $ dose: num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
p=ToothGrowth

Step 3: Create a dot plot

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
ToothGrowth$dose
 [1] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 1   1   1   1   1   1   1   1   1  
[20] 1   2   2   2   2   2   2   2   2   2   2   0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
[39] 0.5 0.5 1   1   1   1   1   1   1   1   1   1   2   2   2   2   2   2   2  
[58] 2   2   2  
Levels: 0.5 1 2
ggplot(ToothGrowth, aes(x=dose, y=len, color=supp)) 

ggplot(ToothGrowth, aes(x=dose, y=len, color=supp))+ 
  geom_dotplot(
    binaxis = 'y',  #used to group many entries into separate bins, x means group                      vertically , y means group horizontally
    stackdir = 'center', #which direction to stack the dots
    position = position_dodge(width = 0.8),
    dotsize = 0.6,
    binwidth = 1.5
  ) + labs(title = "Dotplot of Tooth Length and Dosage",
              x="Dose",
              y="Len")+ theme_minimal()

Create a R program to calculate and visualize co relation matrix for a given data set with color coded cells the strength and direction of co relations , using ggplot2 geom_tile function

Step 1 : Load the libraries

library(ggplot2)
library(dplyr)

Step 2: Explore the dataset

data(mtcars)
head(mtcars, n=10)
                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
cor_matrix <- cor(mtcars)
cor_df <- as.data.frame(as.table(cor_matrix))
cor_df
    Var1 Var2        Freq
1    mpg  mpg  1.00000000
2    cyl  mpg -0.85216196
3   disp  mpg -0.84755138
4     hp  mpg -0.77616837
5   drat  mpg  0.68117191
6     wt  mpg -0.86765938
7   qsec  mpg  0.41868403
8     vs  mpg  0.66403892
9     am  mpg  0.59983243
10  gear  mpg  0.48028476
11  carb  mpg -0.55092507
12   mpg  cyl -0.85216196
13   cyl  cyl  1.00000000
14  disp  cyl  0.90203287
15    hp  cyl  0.83244745
16  drat  cyl -0.69993811
17    wt  cyl  0.78249579
18  qsec  cyl -0.59124207
19    vs  cyl -0.81081180
20    am  cyl -0.52260705
21  gear  cyl -0.49268660
22  carb  cyl  0.52698829
23   mpg disp -0.84755138
24   cyl disp  0.90203287
25  disp disp  1.00000000
26    hp disp  0.79094859
27  drat disp -0.71021393
28    wt disp  0.88797992
29  qsec disp -0.43369788
30    vs disp -0.71041589
31    am disp -0.59122704
32  gear disp -0.55556920
33  carb disp  0.39497686
34   mpg   hp -0.77616837
35   cyl   hp  0.83244745
36  disp   hp  0.79094859
37    hp   hp  1.00000000
38  drat   hp -0.44875912
39    wt   hp  0.65874789
40  qsec   hp -0.70822339
41    vs   hp -0.72309674
42    am   hp -0.24320426
43  gear   hp -0.12570426
44  carb   hp  0.74981247
45   mpg drat  0.68117191
46   cyl drat -0.69993811
47  disp drat -0.71021393
48    hp drat -0.44875912
49  drat drat  1.00000000
50    wt drat -0.71244065
51  qsec drat  0.09120476
52    vs drat  0.44027846
53    am drat  0.71271113
54  gear drat  0.69961013
55  carb drat -0.09078980
56   mpg   wt -0.86765938
57   cyl   wt  0.78249579
58  disp   wt  0.88797992
59    hp   wt  0.65874789
60  drat   wt -0.71244065
61    wt   wt  1.00000000
62  qsec   wt -0.17471588
63    vs   wt -0.55491568
64    am   wt -0.69249526
65  gear   wt -0.58328700
66  carb   wt  0.42760594
67   mpg qsec  0.41868403
68   cyl qsec -0.59124207
69  disp qsec -0.43369788
70    hp qsec -0.70822339
71  drat qsec  0.09120476
72    wt qsec -0.17471588
73  qsec qsec  1.00000000
74    vs qsec  0.74453544
75    am qsec -0.22986086
76  gear qsec -0.21268223
77  carb qsec -0.65624923
78   mpg   vs  0.66403892
79   cyl   vs -0.81081180
80  disp   vs -0.71041589
81    hp   vs -0.72309674
82  drat   vs  0.44027846
83    wt   vs -0.55491568
84  qsec   vs  0.74453544
85    vs   vs  1.00000000
86    am   vs  0.16834512
87  gear   vs  0.20602335
88  carb   vs -0.56960714
89   mpg   am  0.59983243
90   cyl   am -0.52260705
91  disp   am -0.59122704
92    hp   am -0.24320426
93  drat   am  0.71271113
94    wt   am -0.69249526
95  qsec   am -0.22986086
96    vs   am  0.16834512
97    am   am  1.00000000
98  gear   am  0.79405876
99  carb   am  0.05753435
100  mpg gear  0.48028476
101  cyl gear -0.49268660
102 disp gear -0.55556920
103   hp gear -0.12570426
104 drat gear  0.69961013
105   wt gear -0.58328700
106 qsec gear -0.21268223
107   vs gear  0.20602335
108   am gear  0.79405876
109 gear gear  1.00000000
110 carb gear  0.27407284
111  mpg carb -0.55092507
112  cyl carb  0.52698829
113 disp carb  0.39497686
114   hp carb  0.74981247
115 drat carb -0.09078980
116   wt carb  0.42760594
117 qsec carb -0.65624923
118   vs carb -0.56960714
119   am carb  0.05753435
120 gear carb  0.27407284
121 carb carb  1.00000000

Step 3: Visualize the graph

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="Corelation"
  ) + geom_text(aes(label = round(Freq, 2)), size =3) + theme_minimal()+
  labs(title="Corelation matrix(mtcars)", x="", y="")+ theme(axis.text.x = element_text(angle = 45, hjust=1))