Create multiple histogram using ggplot2::face_wrap() to visualize how a variable (e.g sepal.length)is distributed across different groups (e.g species)in a built-in R dataset .
library(ggplot2)#load the iris datasetdata("iris")#view the first few rows of the datasethead(iris)
ggplot(iris, aes(x = Sepal.Length)) +geom_histogram(binwidth =0.3, fill ="skyblue", color ="black") +facet_wrap(~ Species, scales ="free") +# Split the histograms by Speciestheme_minimal() +# Minimal theme for better visualizationlabs(title ="Distribution of Sepal Length by Species",x ="Sepal Length",y ="Frequency")
PROGRAM-10
Develop an R function to draw a density curve representing the 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)}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.
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)
PROGRAM 13
Write R programming to create multiple dot plots of group data,comparing the distribution 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
ggplot(ToothGrowth,aes(x=dose,y=len,color=supp,fill='pink'))+geom_dotplot(binaxis ="y",stackdir ="center",position =position_dodge(width =0.8),dotsize=0.6,binwidth=1.5 )+labs(title="Dot Plot of tooth length by dose and supplement type",x="Dose(mg/day)",y="Tooth length",color="supplement type" )+theme_minimal()
PROGRAM 14
Develop r program,to calculate and visualize correlation matrix for a given data set,with color coded cells indicating the strength and relations of correlations,using ggplot2 ,geom_tile function.