0
0
R Programmingprogramming~30 mins

Histogram and density plots in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Histogram and Density Plots
📖 Scenario: You are a data analyst working with a small dataset of daily temperatures. You want to understand how the temperatures are distributed by creating a histogram and a density plot.
🎯 Goal: Build an R script that creates a vector of temperature data, sets a bin width for the histogram, generates both a histogram and a density plot using base R plotting functions, and then displays the plots.
📋 What You'll Learn
Create a numeric vector called temperatures with the exact values: 22, 25, 19, 24, 30, 28, 21, 23, 27, 26
Create a numeric variable called bin_width and set it to 3
Use the hist() function with breaks=seq() to create a histogram of temperatures with bins of width bin_width
Use the plot() function with density() to create a density plot of temperatures
Print both plots so they are visible
💡 Why This Matters
🌍 Real World
Histograms and density plots help you understand how data points are spread out, which is useful in weather analysis, sales data, or any measurements.
💼 Career
Data analysts and scientists use these plots to explore data distributions before making decisions or building models.
Progress0 / 4 steps
1
Create the temperature data vector
Create a numeric vector called temperatures with these exact values: 22, 25, 19, 24, 30, 28, 21, 23, 27, 26
R Programming
Need a hint?

Use the c() function to combine numbers into a vector.

2
Set the bin width for the histogram
Create a numeric variable called bin_width and set it to 3
R Programming
Need a hint?

Just assign the number 3 to the variable bin_width.

3
Create the histogram and density plot
Use the hist() function with breaks=seq() to create a histogram of temperatures with bins of width bin_width. Then use the plot() function with density() to create a density plot of temperatures.
R Programming
Need a hint?

Use seq(min(temperatures), max(temperatures) + bin_width, by=bin_width) to create breaks for the histogram bins.

Use density() to calculate density and plot() to draw it.

4
Display the plots
Run the code so that both the histogram and the density plot are displayed in the R plotting window.
R Programming
Need a hint?

Just run the full code to see both plots in the R plotting window.