# Load the ggplot2 package
library(ggplot2)Program 10
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 library
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: Explanation of Function Components
| Code | Description |
|---|---|
data |
The dataset (e.g., iris) |
continuous_var |
Name of the continuous variable (e.g., "Sepal.Length") |
group_var |
Grouping variable (e.g., "Species") |
aes_string() |
Maps the variables using string names (for flexibility) |
geom_density(alpha = 0.4) |
Draws smoothed density curves with transparency |
facet_wrap(~ group_var) |
Not used here; instead we overlay curves in one plot |
theme_minimal() |
Clean layout with minimal gridlines |
scale_fill_manual() |
Applies custom fill colors if provided |
Step 4: Example with built-in iris data set
# 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)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: Output Description
The X-axis shows the continuous variable (e.g.,
Sepal.Length).The Y-axis shows the probability density.
Each group (e.g.,
Species) is represented by a separate curve.The
alpha = 0.4setting allows curves to overlap transparently.