0
0
R Programmingprogramming~10 mins

Box plots and violin 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 basic box plot of the variable 'mpg' from the 'mtcars' dataset.

R Programming
boxplot(mtcars$[1])
Drag options to blanks, or click blank then click option'
Ampg
Bcyl
Cgear
Dhp
Attempts:
3 left
💡 Hint
Common Mistakes
Using a categorical variable like 'cyl' instead of a numeric variable.
Forgetting to use the $ operator to access the variable.
2fill in blank
medium

Complete the code to create a violin plot of 'mpg' grouped by 'cyl' using ggplot2.

R Programming
library(ggplot2)
ggplot(mtcars, aes(x = factor([1]), y = mpg)) + geom_violin()
Drag options to blanks, or click blank then click option'
Ahp
Bcarb
Ccyl
Dgear
Attempts:
3 left
💡 Hint
Common Mistakes
Using a numeric variable like 'gear' without converting to factor.
Not converting the grouping variable to a factor, causing continuous x-axis.
3fill in blank
hard

Fix the error in the code to add a box plot layer to a ggplot object 'p' that plots 'mpg' by 'cyl'.

R Programming
p + geom_boxplot(aes([1] = factor(cyl), y = mpg))
Drag options to blanks, or click blank then click option'
Ax
By
Cfill
Dcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Using y instead of x for grouping.
Using fill or color inside aes() without specifying x or y.
4fill in blank
hard

Fill both blanks to create a combined violin and box plot of 'mpg' grouped by 'cyl' with ggplot2.

R Programming
ggplot(mtcars, aes(x = factor([1]), y = mpg)) + geom_violin() + geom_[2]()
Drag options to blanks, or click blank then click option'
Acyl
Bboxplot
Dpoint
Attempts:
3 left
💡 Hint
Common Mistakes
Using geom_point() instead of geom_boxplot().
Not converting 'cyl' to a factor for grouping.
5fill in blank
hard

Fill all three blanks to create a violin plot with customized fill color by 'cyl' and add box plot overlay.

R Programming
ggplot(mtcars, aes(x = factor([1]), y = mpg, fill = factor([2]))) + geom_violin() + geom_[3](width = 0.1)
Drag options to blanks, or click blank then click option'
Acyl
Cboxplot
Dpoint
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for x and fill causing inconsistent grouping.
Using geom_point() instead of geom_boxplot() for overlay.