A legend helps explain what different colors or symbols mean in a plot. It makes charts easier to understand.
Legend control in R Programming
Introduction
Syntax
R Programming
legend(x, y = NULL, legend, fill = NULL, col = NULL, pch = NULL, title = NULL, ...)
x and y specify the position of the legend on the plot.
legend is a character vector with labels for the legend.
Examples
R Programming
legend("topright", legend = c("Group A", "Group B"), fill = c("red", "blue"))
R Programming
legend(1, 1, legend = c("Cats", "Dogs"), col = c("black", "brown"), pch = c(1, 2))
Sample Program
This program plots two data series with different colors and symbols. The legend explains which color and symbol belong to each series.
R Programming
x <- 1:5 y1 <- c(2, 3, 5, 7, 11) y2 <- c(1, 4, 6, 8, 10) plot(x, y1, type = "b", col = "red", pch = 16, ylim = c(0, 12), ylab = "Value", xlab = "X") lines(x, y2, type = "b", col = "blue", pch = 17) legend("topright", legend = c("Series 1", "Series 2"), col = c("red", "blue"), pch = c(16, 17), title = "Legend")
Important Notes
You can change the legend position by using keywords like "topright", "bottomleft", or coordinates.
Use pch to set point symbols and col to set colors in the legend.
Adding a title to the legend helps clarify what the legend is about.
Summary
A legend explains colors or symbols in a plot.
Use legend() to add and customize legends in R plots.
Position, labels, colors, and symbols can all be controlled for clarity.
Practice
1. What is the main purpose of the
legend() function in R plotting?easy
Solution
Step 1: Understand the role of legend in plots
A legend helps viewers understand what different colors or symbols represent in a plot.Step 2: Identify the function purpose
Thelegend()function in R adds this explanatory box to the plot.Final Answer:
To explain the meaning of colors or symbols used in the plot -> Option AQuick 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
Solution
Step 1: Recall legend() function syntax
The first argument is the position as a string, then legend= specifies labels.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.Final Answer:
legend("topright", legend = c("A", "B")) -> Option BQuick 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
Solution
Step 1: Understand plot() parameters
Points 1,2,3 are plotted with colors red, green, blue and filled circle symbol (pch=16).Step 2: Understand legend() parameters
Legend placed at bottom left with matching labels, colors, and symbols (filled circles).Final Answer:
A plot with three points colored red, green, blue and a legend at bottom left showing these colors with filled circles -> Option CQuick 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
Solution
Step 1: Check legend() argument names
The correct argument for labels is 'legend', not 'labels'. Using 'labels' causes no legend to appear.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.Final Answer:
The argument 'labels' should be 'legend' in the legend() function -> Option AQuick 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
Solution
Step 1: Identify correct legend arguments
Use 'legend' for labels, 'col' for colors, and 'pch' for symbols (numeric codes).Step 2: Match symbols and colors to groups
Red circles use pch=1, blue triangles use pch=2, colors as "red" and "blue" strings.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.Final Answer:
legend("topright", legend = c("Group A", "Group B"), col = c("red", "blue"), pch = c(1, 2)) -> Option DQuick 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
10 questions ~5 mineasy hard
