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 a built-inR dataset.
library(ggplot2)#Load the iris datasetdata(iris)#View the first few rows of the datasethead(iris)
# Create histograms using facet_wrap for grouped dataggplot(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()
Objective
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.
library(ggplot2)plot_density_by_group <-function(data, continuous_var, group_var, fill_colors =NULL) {# Check if the specified columns existif (!(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 providedif (!is.null(fill_colors)) { p <- p +scale_fill_manual(values = fill_colors) +scale_color_manual(values = fill_colors) }# Return the plotreturn(p)}# Basic usageplot_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.
To create a violin plot using ggplot2 in R that displays the distribution of a continuous variable with separate violins for each group using an in-built dataset.
ggplot(iris, aes(x = Species, y = Petal.Length, fill = Species)) +geom_violin(trim =FALSE, alpha =0.6, color ="black") +labs(title ="Distribution of Petal Length by Iris Species",x ="Species",y ="Petal Length (cm)" ) +theme_minimal(base_size =14)
Dot Plot for Grouped Data using ggplot2
Write a R program to create multiple dot plots for grouped data, comparing the distributions of variables across different categories, using ggplot2’s position _dodge function.
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
# Convert dose to a factor for groupingToothGrowth$dose <-as.factor(ToothGrowth$dose)# Plot with defined binwidthggplot(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# Controls spacing of dots on y-axis ) +labs(title ="Dot Plot of Tooth Length by Dose and Supplement Type",x ="Dose (mg/day)",y ="Tooth Length",color ="Supplement Type" ) +theme_minimal()
Correlation Matrix Visualization using ggplot2
Develop a R program to calculate and visualize co-relation matrix for a given dataset, with color coded cells indicating the strength and direction of co-relation using ggplot2 and geom_tile function.