Complete the code to create a histogram of the variable 'x' using ggplot2.
library(ggplot2) data <- data.frame(x = rnorm(100)) ggplot(data, aes(x = x)) + geom_[1]()
The correct function to create a histogram in ggplot2 is geom_histogram().
Complete the code to add a density plot layer on top of the histogram.
ggplot(data, aes(x = x)) + geom_histogram(binwidth = 0.5, fill = 'blue', alpha = 0.5) + geom_[1](color = 'red')
The function geom_density() adds a density plot layer.
Fix the error in the code to correctly plot a histogram with density on y-axis.
ggplot(data, aes(x = x, y = ..[1]..)) + geom_histogram(binwidth = 0.3)
Using y = ..density.. scales the histogram to show density instead of counts.
Fill both blanks to create a histogram with density overlay and set transparency.
ggplot(data, aes(x = x)) + geom_histogram(binwidth = 0.4, fill = 'green', alpha = [1]) + geom_[2](color = 'black', size = 1)
Alpha controls transparency; 0.7 is a good choice. geom_density() adds the density line.
Fill all three blanks to create a histogram with density overlay, set binwidth, and add title.
ggplot(data, aes(x = x)) + geom_histogram(binwidth = [1], fill = 'purple', alpha = 0.6) + geom_density(color = [2], size = 1) + ggtitle([3])
Binwidth 0.5 is reasonable. Color 'red' for density line. Title must be a string in quotes.