Bird
Raised Fist0
R Programmingprogramming~20 mins

Histogram and density plots in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Histogram and Density Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Histogram bin count effect
What is the effect on the histogram when changing the number of bins in this R code?
R Programming
data <- c(1,2,2,3,3,3,4,4,4,4)
hist(data, breaks=2, plot=FALSE)$counts
hist(data, breaks=5, plot=FALSE)$counts
A[5, 5] and [0, 2, 3, 3, 2]
B[5, 5] and [2, 2, 2, 2, 2]
C[5, 5] and [1, 2, 2, 2, 3]
D[5, 5] and [1, 1, 2, 3, 3]
Attempts:
2 left
💡 Hint
Try running hist() with different breaks values and check the counts component.
🧠 Conceptual
intermediate
1:30remaining
Difference between histogram and density plot
Which statement correctly describes the difference between a histogram and a density plot in R?
AHistogram shows cumulative counts; density plot shows raw data points.
BHistogram shows a smoothed curve; density plot shows counts per bin.
CHistogram and density plot are identical visualizations with different names.
DHistogram shows counts per bin; density plot shows a smoothed estimate of the distribution.
Attempts:
2 left
💡 Hint
Think about what each plot type represents visually.
🔧 Debug
advanced
1:30remaining
Fix the error in density plot code
What error does this R code produce and why? density(data, bw="wrong") Assume data is a numeric vector.
AError: 'bw' must be numeric or a function, not a character string.
BError: object 'data' not found.
CError: density() requires a data frame, not a vector.
DNo error; code runs and plots density.
Attempts:
2 left
💡 Hint
Check the expected type for the 'bw' parameter in density().
❓ Predict Output
advanced
2:00remaining
Overlay histogram and density plot
What will be the output of this R code snippet?
R Programming
set.seed(1)
data <- rnorm(100)
hist(data, freq=FALSE, col='lightblue')
density_data <- density(data)
lines(density_data, col='red')
length(density_data$x)
A50
B100
C512
D101
Attempts:
2 left
💡 Hint
Check the default length of the density() output vector in R.
🚀 Application
expert
2:30remaining
Calculate area under density curve
Given a density object in R, which code correctly calculates the approximate area under the density curve?
Asum(density_data$x) * diff(density_data$y)[1]
Bsum(density_data$y) * diff(density_data$x)[1]
Cmean(density_data$y) * length(density_data$x)
Dsum(density_data$y)
Attempts:
2 left
💡 Hint
Think about numerical integration using the trapezoidal rule approximation.

Practice

(1/5)
1. What is the main difference between a histogram and a density plot in R?
easy
A. Histogram shows exact values; density plot shows counts only.
B. Histogram uses bars; density plot uses a smooth curve.
C. Histogram is for categorical data; density plot is for numeric data.
D. Histogram requires ggplot2; density plot does not.

Solution

  1. Step 1: Understand histogram representation

    A histogram displays data grouped into bins using bars to show frequency counts.
  2. Step 2: Understand density plot representation

    A density plot shows a smooth curve estimating the data distribution, not bars.
  3. Final Answer:

    Histogram uses bars; density plot uses a smooth curve. -> Option B
  4. Quick Check:

    Histogram = bars, Density plot = smooth curve [OK]
Hint: Bars mean histogram; smooth curve means density plot [OK]
Common Mistakes:
  • Thinking histogram shows exact values instead of grouped counts
  • Confusing density plot with bar charts
  • Assuming histograms only work with categorical data
2. Which of the following is the correct R code to create a histogram of the numeric vector data?
easy
A. hist(data)
B. plot.histogram(data)
C. histogram(data)
D. density(data)

Solution

  1. Step 1: Recall the base R function for histograms

    The correct function to create a histogram in base R is hist().
  2. Step 2: Check the options

    Only hist(data) is valid syntax; others are either incorrect function names or for density plots.
  3. Final Answer:

    hist(data) -> Option A
  4. Quick Check:

    hist() creates histograms in R [OK]
Hint: Use hist() for histograms in base R [OK]
Common Mistakes:
  • Using non-existent function histogram()
  • Confusing density() with hist()
  • Trying plot.histogram() which is invalid
3. What will be the output of the following R code?
set.seed(1)
data <- rnorm(100)
hist(data, breaks=5, col='blue')
medium
A. An error because breaks cannot be numeric
B. A density plot with 5 blue lines
C. A histogram with 5 blue bars showing data distribution
D. A scatter plot of 100 points

Solution

  1. Step 1: Understand hist() with breaks parameter

    The breaks=5 argument divides data into 5 bins for the histogram.
  2. Step 2: Check color and plot type

    The col='blue' colors the bars blue; hist() creates a bar plot, not a density or scatter plot.
  3. Final Answer:

    A histogram with 5 blue bars showing data distribution -> Option C
  4. Quick Check:

    hist() + breaks=5 + col='blue' = blue bars histogram [OK]
Hint: breaks sets bar count; col sets bar color [OK]
Common Mistakes:
  • Thinking breaks=5 creates density plot
  • Assuming breaks cannot be numeric
  • Confusing hist() with scatter plot
4. Identify the error in this R code for plotting a density plot:
data <- rnorm(50)
density(data, col='red')
medium
A. density() does not accept a col argument directly
B. rnorm() cannot generate 50 values
C. Missing parentheses after density
D. data must be a data frame, not a vector

Solution

  1. Step 1: Check density() function usage

    The density() function returns density data but does not plot directly or accept col.
  2. Step 2: Correct plotting method

    To plot with color, use plot(density(data), col='red') instead.
  3. Final Answer:

    density() does not accept a col argument directly -> Option A
  4. Quick Check:

    density() returns data; plot() colors it [OK]
Hint: Use plot(density()) to color density plots [OK]
Common Mistakes:
  • Passing col directly to density()
  • Thinking density() plots automatically
  • Assuming rnorm() can't generate 50 values
5. You want to compare the distribution of two numeric vectors x and y in R using a single plot. Which code correctly overlays their density plots with different colors?
hard
A. plot(density(x, col='blue'), density(y, col='red'))
B. hist(x, col='blue'); hist(y, col='red')
C. density(x, col='blue'); density(y, col='red')
D. plot(density(x), col='blue'); lines(density(y), col='red')

Solution

  1. Step 1: Plot first density with plot()

    Use plot(density(x), col='blue') to draw the first density curve in blue.
  2. Step 2: Add second density with lines()

    Use lines(density(y), col='red') to overlay the second density curve in red.
  3. Final Answer:

    plot(density(x), col='blue'); lines(density(y), col='red') -> Option D
  4. Quick Check:

    plot() then lines() overlays densities with colors [OK]
Hint: plot() first density, lines() add second [OK]
Common Mistakes:
  • Using hist() which creates separate plots
  • Passing col to density() instead of plot() or lines()
  • Trying to plot both densities in one plot() call
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard