0
0
R Programmingprogramming~10 mins

Histogram and density plots in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a histogram of the variable 'x' using ggplot2.

R Programming
library(ggplot2)
data <- data.frame(x = rnorm(100))
ggplot(data, aes(x = x)) + geom_[1]()
Drag options to blanks, or click blank then click option'
Ahist
Bpoint
Chistogram
Dbar
Attempts:
3 left
💡 Hint
Common Mistakes
Using geom_bar() instead of geom_histogram()
Using geom_point() which creates scatter plots
2fill in blank
medium

Complete the code to add a density plot layer on top of the histogram.

R Programming
ggplot(data, aes(x = x)) + geom_histogram(binwidth = 0.5, fill = 'blue', alpha = 0.5) + geom_[1](color = 'red')
Drag options to blanks, or click blank then click option'
Adensity
Bhist
Cline
Dsmooth
Attempts:
3 left
💡 Hint
Common Mistakes
Using geom_hist() again instead of geom_density()
Using geom_line() which requires y aesthetic
3fill in blank
hard

Fix the error in the code to correctly plot a histogram with density on y-axis.

R Programming
ggplot(data, aes(x = x, y = ..[1]..)) + geom_histogram(binwidth = 0.3)
Drag options to blanks, or click blank then click option'
Avalue
Bdensity
Cfrequency
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using y = ..count.. which shows counts instead of density
Using y = ..value.. which is invalid
4fill in blank
hard

Fill both blanks to create a histogram with density overlay and set transparency.

R Programming
ggplot(data, aes(x = x)) + geom_histogram(binwidth = 0.4, fill = 'green', alpha = [1]) + geom_[2](color = 'black', size = 1)
Drag options to blanks, or click blank then click option'
A0.3
B0.7
Cdensity
Dline
Attempts:
3 left
💡 Hint
Common Mistakes
Using alpha greater than 1 or less than 0
Using geom_line() instead of geom_density()
5fill in blank
hard

Fill all three blanks to create a histogram with density overlay, set binwidth, and add title.

R Programming
ggplot(data, aes(x = x)) + geom_histogram(binwidth = [1], fill = 'purple', alpha = 0.6) + geom_density(color = [2], size = 1) + ggtitle([3])
Drag options to blanks, or click blank then click option'
A0.2
B'red'
C"Histogram with Density"
D0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the title string
Using invalid binwidth values