📖 Scenario: You are creating a simple scatter plot to compare two groups of data points. You want to add a legend to clearly show which color represents which group.
🎯 Goal: Build a scatter plot with two groups of points and add a legend that correctly labels each group.
📋 What You'll Learn
Create two numeric vectors called group1 and group2 with the exact values specified.
Create a vector called labels with the exact group names.
Plot the points for both groups with different colors.
Add a legend that uses the labels vector and matches the colors used in the plot.
💡 Why This Matters
🌍 Real World
Legends help people understand what different colors or symbols mean in charts, like showing sales by region or comparing groups in a study.
💼 Career
Data analysts and scientists often create plots with legends to communicate insights clearly to others.
Progress0 / 4 steps
1
DATA SETUP: Create data vectors
Create two numeric vectors called group1 with values c(2, 4, 6, 8) and group2 with values c(1, 3, 5, 7).
R Programming
Hint
Use the c() function to create numeric vectors with the exact values.
2
CONFIGURATION: Create labels vector
Create a character vector called labels with the exact values c("Group A", "Group B").
R Programming
Hint
Use c() to create a character vector with the exact group names.
3
CORE LOGIC: Plot points with colors
Use plot() to plot group1 points in red with pch=16. Then add group2 points in blue using points() with pch=17.
R Programming
Hint
Use plot() for the first group and points() to add the second group with different colors and symbols.
4
OUTPUT: Add legend to the plot
Add a legend to the top right of the plot using legend() with labels, colors c("red", "blue"), and point characters c(16, 17).
R Programming
Hint
Use legend() with the exact parameters to match colors and symbols to the labels.
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
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
The legend() 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 A
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
Step 1: Recall legend() function syntax
The first argument is the position as a string, then legend= specifies labels.
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
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 A
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))