Bird
Raised Fist0
R Programmingprogramming~20 mins

Legend control in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 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().

Practice

(1/5)
1. What is the main purpose of the legend() function in R plotting?
easy
A. To explain the meaning of colors or symbols used in the plot
B. To change the plot background color
C. To add titles to the plot axes
D. To save the plot as an image file

Solution

  1. Step 1: Understand the role of legend in plots

    A legend helps viewers understand what different colors or symbols represent in a plot.
  2. Step 2: Identify the function purpose

    The legend() function in R adds this explanatory box to the plot.
  3. Final Answer:

    To explain the meaning of colors or symbols used in the plot -> Option A
  4. Quick Check:

    legend() explains plot symbols/colors [OK]
Hint: Legend explains plot symbols and colors clearly [OK]
Common Mistakes:
  • Confusing legend() with title() or axis labeling functions
  • Thinking legend() changes plot colors
  • Assuming legend() saves plots
2. Which of the following is the correct syntax to add a legend at the top right corner with labels "A" and "B" in R?
easy
A. legend(topright, labels = c("A", "B"))
B. legend("topright", legend = c("A", "B"))
C. legend(position = "topright", c("A", "B"))
D. legend(c("A", "B"), position = "topright")

Solution

  1. Step 1: Recall legend() function syntax

    The first argument is the position as a string, then legend= specifies labels.
  2. Step 2: Check each option

    legend("topright", legend = c("A", "B")) uses correct syntax: legend("topright", legend = c("A", "B")). Others misuse argument names or order.
  3. Final Answer:

    legend("topright", legend = c("A", "B")) -> Option B
  4. Quick Check:

    Correct legend() syntax = legend("topright", legend = c("A", "B")) [OK]
Hint: First argument is position string, use legend= for labels [OK]
Common Mistakes:
  • Omitting quotes around position
  • Using wrong argument names like labels instead of legend
  • Passing labels before position
3. What will be the output of the following R code?
plot(1:3, col = c("red", "green", "blue"), pch = 16)
legend("bottomleft", legend = c("Red", "Green", "Blue"), col = c("red", "green", "blue"), pch = 16)
medium
A. A plot with colored points but legend shows empty boxes
B. A plot with default black points and no legend
C. A plot with three points colored red, green, blue and a legend at bottom left showing these colors with filled circles
D. Syntax error due to missing parameters

Solution

  1. Step 1: Understand plot() parameters

    Points 1,2,3 are plotted with colors red, green, blue and filled circle symbol (pch=16).
  2. Step 2: Understand legend() parameters

    Legend placed at bottom left with matching labels, colors, and symbols (filled circles).
  3. Final Answer:

    A plot with three points colored red, green, blue and a legend at bottom left showing these colors with filled circles -> Option C
  4. Quick Check:

    plot colors and legend match correctly [OK]
Hint: Legend colors and symbols must match plot points [OK]
Common Mistakes:
  • Assuming legend uses default black color
  • Forgetting to set pch in legend
  • Confusing legend position strings
4. Identify the error in this R code snippet that tries to add a legend:
plot(1:5, col = 1:5, pch = 15)
legend("top", labels = c("One", "Two", "Three", "Four", "Five"), col = 1:5, pch = 15)
medium
A. The argument 'labels' should be 'legend' in the legend() function
B. The position 'top' is invalid in legend()
C. The colors 1:5 are not valid color codes
D. pch = 15 is not a valid plotting character

Solution

  1. Step 1: Check legend() argument names

    The correct argument for labels is 'legend', not 'labels'. Using 'labels' causes no legend to appear.
  2. Step 2: Verify position and other parameters

    Position 'top' is valid, colors 1:5 are valid R colors, pch=15 is a valid filled square symbol.
  3. Final Answer:

    The argument 'labels' should be 'legend' in the legend() function -> Option A
  4. Quick Check:

    Use legend=, not labels= in legend() [OK]
Hint: Use legend= for labels inside legend() [OK]
Common Mistakes:
  • Using 'labels' instead of 'legend' argument
  • Thinking 'top' is invalid position
  • Confusing color codes with integers
5. You want to create a plot with two groups of points: group A in red circles and group B in blue triangles. How do you add a legend that clearly shows these groups with correct symbols and colors?
hard
A. legend("topright", legend = c("Group A", "Group B"), col = c(1, 2), pch = c(16, 17))
B. legend("topright", labels = c("Group A", "Group B"), colors = c("red", "blue"), symbols = c(1, 2))
C. legend("topright", legend = c("Group A", "Group B"), color = c("red", "blue"), pch = c("circle", "triangle"))
D. legend("topright", legend = c("Group A", "Group B"), col = c("red", "blue"), pch = c(1, 2))

Solution

  1. Step 1: Identify correct legend arguments

    Use 'legend' for labels, 'col' for colors, and 'pch' for symbols (numeric codes).
  2. Step 2: Match symbols and colors to groups

    Red circles use pch=1, blue triangles use pch=2, colors as "red" and "blue" strings.
  3. Step 3: Check options

    legend("topright", legend = c("Group A", "Group B"), col = c("red", "blue"), pch = c(1, 2)) uses correct argument names and values. legend("topright", labels = c("Group A", "Group B"), colors = c("red", "blue"), symbols = c(1, 2)) uses wrong argument names. legend("topright", legend = c("Group A", "Group B"), color = c("red", "blue"), pch = c("circle", "triangle")) uses invalid pch values. legend("topright", legend = c("Group A", "Group B"), col = c(1, 2), pch = c(16, 17)) uses numeric colors 1 and 2 which are black and red, not blue.
  4. Final Answer:

    legend("topright", legend = c("Group A", "Group B"), col = c("red", "blue"), pch = c(1, 2)) -> Option D
  5. Quick Check:

    Correct legend args and matching colors/symbols [OK]
Hint: Use legend=, col= colors, pch= numeric symbols [OK]
Common Mistakes:
  • Using wrong argument names like labels or colors
  • Using string names for pch instead of numbers
  • Using numeric colors that don't match intended colors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard