Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a legend to the plot.
R Programming
plot(x, y, type = 'l') legend('topright', legend = [1], col = 'blue', lty = 1)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an unquoted string causes an error.
Using square brackets is not valid in R for vectors.
✗ Incorrect
The legend function expects the legend argument as a character vector, so c('Data Line') is correct.
2fill in blank
mediumComplete the code to set the legend title.
R Programming
plot(x, y, type = 'l') legend('topright', legend = c('Data'), title = [1], col = 'red', lty = 1)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes causes an error.
Using c() creates a vector, but title expects a single string.
✗ Incorrect
The title argument expects a string, so it must be in quotes like 'Data Title'.
3fill in blank
hardFix the error in the legend position argument.
R Programming
plot(x, y, type = 'l') legend([1], legend = c('Line'), col = 'green', lty = 1)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted position names causes errors.
Using invalid position names causes the legend not to appear.
✗ Incorrect
The position argument must be a string in quotes, so 'topright' is correct.
4fill in blank
hardFill both blanks to create a legend with two items and different colors.
R Programming
plot(x, y, type = 'l') legend('bottomleft', legend = [1], col = [2], lty = 1)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one label or one color causes mismatch errors.
Not using c() creates invalid arguments.
✗ Incorrect
The legend needs two labels and two colors, so c('Line1', 'Line2') and c('red', 'blue') are correct.
5fill in blank
hardFill all three blanks to customize the legend with line types and a title.
R Programming
plot(x, y, type = 'l') legend('top', legend = [1], col = [2], lty = [3], title = 'Legend')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatched vector lengths cause errors.
Using color names not recognized by R causes warnings.
✗ Incorrect
The legend labels are c('A', 'B'), colors c('black', 'orange'), and line types c(1, 2) for solid and dashed lines.