0
0
R Programmingprogramming~30 mins

Legend control in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Legend control
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use legend() with the exact parameters to match colors and symbols to the labels.