library(ggplot2)
Program 15
- Create multiple histograms using ggplot2::facet_wrap() to visualize how a variable(e.g.,Sepal.Length) is distributed across different groups(e.g., Species) in built-in R dataset.
Step 1: Load the required libraries
Step 2: Load 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
Step 3: Creating histogram plot
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()
- Develop an R function to draw a density curve representing the probability density function of a continuous variable, with separate curves for each group, using ggplot2.
Step 1: Load the required libraries
library(ggplot2)
Step 2: Define the function
<- function(data, continuous_var, group_var, fill_colors = NULL){
plot_density_by_group if(!(continuous_var %in% names(data)) || !(group_var %in% names(data))){
stop("Invalid column names. Make sure both variables exist in the dataset.")
}
<-ggplot(data, aes_string(x=continuous_var, color=group_var, fill=group_var)) +
pgeom_density(alpha=0.4) +
labs(title = paste("Density plot of", continuous_var, "by", group_var),
x = continuous_var,
y = "Density") +
theme_minimal()
#Custom fill colors if provided
if(!is.null(fill_colors)){
<- p + scale_fill_manual(values = fill_colors) +
p scale_color_manual(values = fill_colors)
}return(p)
}
Step 3: Example with Built-in iris dataset
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 4: Example with custom colors
<- c("setosa" = "steelblue",
custom_colors "versicolor" = "forestgreen",
"virginica" = "darkorange")
plot_density_by_group(iris, "Petal.Length", "Species", fill_colors = custom_colors)
- To generate a basic box plot using ggplot2, enhanced with notches and outliers and grouped by a categorical variable using an in-built data set R.
Step 1 : Load the required library
library(ggplot2)
Step 2 : Load the 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
Step 3 : Create box plot with notches
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot(
notch = TRUE,
notchwidth = 0.6,
outlier.colour = "black",
fill = "pink" ) +
labs(title="Box plot",
x = "Species",
y = "Sepal length") +
theme_minimal()
- Develop a script in R to create a violin plot displaying the distribution of continuous variable, with separate violins for each group using ggplot2
Step 1 : Load the required library
library(ggplot2)
Step 2 : Load 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 violin plot 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)
- Write an R program to create multiple dot plots or group data, comparing the distribution of variables across different categories, using ggplot2’s position_dodge function.
Step 1 : Load the required 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
Step 2 : Load the dataset
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 ...
table(ToothGrowth$dose)
0.5 1 2
20 20 20
Step 3 : Create multiple dot plots
$dose <- as.factor(ToothGrowth$dose)
ToothGrowth
ggplot(ToothGrowth, aes(x = dose, y = len, color = supp)) +
geom_dotplot(
binaxis = 'y',
stackdir = 'center',
position = position_dodge(width = 0.8),
dotsize = 0.6,
binwidth = 1.5
+
) labs(title = "Dot plot of Toothlength by Dosage and Supplement type",
x = "Dose",
y = "Tooth length",
color = "Supplement type") +
theme_minimal()
- Develop a R program to calculate and visualize correlation matrix for a given dataset, with color coded cells indicating the strength and relations of correlations, using ggplot2::geom_tile function.
Step 1 : Load the required library
library(ggplot2)
library(tidyr)
library(dplyr)
Step 2 : Load the dataset
data(mtcars)
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(mtcars)
cor_matrix <- as.data.frame(as.table(cor_matrix))
cor_df 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 using ggplot2::geom_tile
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) +
labs( title = "Correlation Matrix (mtcars)",
x = "Var1",
y = "Var2" ) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))