0
0
R Programmingprogramming~20 mins

Legend control in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Legend Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this legend control code?
Consider the following R code that creates a plot with a legend. What will be the color of the legend text?
R Programming
plot(1:5, col = c("red", "blue", "green", "purple", "orange"), pch = 19, cex = 2)
legend("topright", legend = c("A", "B", "C", "D", "E"), col = c("red", "blue", "green", "purple", "orange"), pch = 19, text.col = "blue")
AThe legend text will be blue.
BThe legend text will be red.
CThe legend text will be black (default).
DThe legend text will be green.
Attempts:
2 left
💡 Hint
Look at the 'text.col' parameter in the legend function.
Predict Output
intermediate
2:00remaining
What happens if you set 'bty = "n"' in legend()?
Given this R code snippet, what will be the effect on the legend box?
R Programming
plot(1:3, col = c("red", "green", "blue"), pch = 19, cex = 2)
legend("topright", legend = c("Red", "Green", "Blue"), col = c("red", "green", "blue"), pch = 19, bty = "n")
AThe legend box will not be drawn (no border).
BThe legend box will have a thick black border.
CThe legend box will be drawn with dashed border.
DThe legend box will be filled with color.
Attempts:
2 left
💡 Hint
Check the meaning of the 'bty' parameter in legend().
Predict Output
advanced
2:00remaining
What is the output of this legend with multiple line labels?
What will be the appearance of the legend text in this R code?
R Programming
plot(1:4, col = c("red", "blue", "green", "purple"), pch = 19, cex = 2)
legend("bottomleft", legend = c("First line\nSecond line", "Blue", "Green", "Purple"), col = c("red", "blue", "green", "purple"), pch = 19)
AThe first legend label will show two lines: 'First line' and 'Second line'.
BThe legend will show only 'First line' and ignore the rest.
CThe legend will show '\n' literally in the first label.
DThe legend will cause an error due to '\n' in the label.
Attempts:
2 left
💡 Hint
Check how legend() handles newline characters in labels.
Predict Output
advanced
2:00remaining
What is the effect of 'inset' parameter in legend()?
Given this R code, where will the legend appear relative to the plot area?
R Programming
plot(1:5, col = "black", pch = 19, cex = 2)
legend("topright", legend = "Point", pch = 19, inset = 0.1)
AThe legend will be inset 10 pixels from the top right corner.
BThe legend will be outside the plot area at the top right.
CThe legend will be exactly at the top right corner with no inset.
DThe legend will be inset 10% inside from the top right corner.
Attempts:
2 left
💡 Hint
The 'inset' parameter moves the legend inside the plot margins by a fraction.
Predict Output
expert
2:00remaining
What is the output of this legend with 'fill' and 'border' parameters?
Analyze the following R code. What will be the color of the legend boxes and their borders?
R Programming
plot(1:3, pch = 22, cex = 3)
legend("bottomright", legend = c("Red", "Green", "Blue"), fill = c("red", "green", "blue"), border = c("black", "yellow", "white"))
ALegend boxes will be empty with colored borders black, yellow, white.
BLegend boxes will be filled with red, green, blue and borders black, yellow, white respectively.
CLegend boxes will be filled with red, green, blue but all borders will be black.
DLegend boxes will be filled with red, green, blue but borders will not be visible.
Attempts:
2 left
💡 Hint
Check how 'fill' and 'border' parameters work together in legend().