importing the library
library(ggplot2)
defining a function with arguments, continuous and group variables are often used to specify the inputs of the function. Labels, or “labs,” are used to identify different components of the graph, while alpha in this context typically refers to the transparency level of those components. Alpha values can adjust the opacity of elements on the graph, allowing for a more visually appealing presentation of data.
plot_density_curves <-function(data,continuous_var,group_var)
{
ggplot(data,aes_string(x=continuous_var,color=group_var,fill=group_var))+
geom_density(alpha = 0.3)+
labs(
title=paste("density plot of",continuous_var ,"by",group_var),
x="sepal.length",
y="density"
)+
theme_minimal()
}
##calling the above defined function using iris
data("iris")
plot_density_curves(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.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.