0
0
R Programmingprogramming~10 mins

Legend control in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Legend control
Create plot object
Add data layers
Add legend control
Customize legend appearance
Render plot with legend
This flow shows how to build a plot step-by-step, add a legend control, customize it, and then display the final plot.
Execution Sample
R Programming
library(ggplot2)

p <- ggplot(mtcars, aes(x=wt, y=mpg, color=factor(cyl))) +
  geom_point() +
  labs(color='Cylinders')

print(p)
This code creates a scatter plot of car weight vs. mileage, colors points by cylinder count, adds a legend title, and displays the plot.
Execution Table
StepActionEvaluationResult
1Load ggplot2 librarylibrary(ggplot2)ggplot2 functions available
2Create ggplot object with mtcars dataggplot(mtcars, aes(x=wt, y=mpg, color=factor(cyl)))Plot object with aesthetics set
3Add points layergeom_point()Points layer added to plot
4Add legend label for colorlabs(color='Cylinders')Legend title set to 'Cylinders'
5Print plotprint(p)Plot rendered with legend showing cylinder groups
6EndNo more commandsExecution stops
💡 All plot components added and plot displayed with legend control
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
pNULLggplot object with aestheticsggplot + points layerggplot + points + legend labelFinal plot object ready to render
Key Moments - 3 Insights
Why does the legend appear automatically without extra code?
Because ggplot2 automatically creates legends for mapped aesthetics like color, as shown in execution_table step 2 and 3.
How do we change the legend title?
By using labs(color='Cylinders') as in step 4, which sets the legend label for the color aesthetic.
What happens if we don't call print(p)?
The plot won't display in some environments; print(p) in step 5 triggers rendering.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of variable 'p' after step 3?
APlot object with legend label set
BPlot object with points layer added
CNULL
DPlot rendered on screen
💡 Hint
Check variable_tracker column 'After Step 3' for 'p'
At which step is the legend title set to 'Cylinders'?
AStep 2
BStep 5
CStep 4
DStep 3
💡 Hint
Look at execution_table row with action 'Add legend label for color'
If we skip step 5 (print(p)), what happens?
APlot object is created but not displayed
BError occurs
CPlot is rendered anyway
DLegend disappears
💡 Hint
Refer to key_moments answer about print(p) necessity
Concept Snapshot
Legend control in R ggplot2:
- Map aesthetics like color to data for automatic legends
- Use labs(color='Title') to set legend title
- Add layers (geom_point) to build plot
- Call print() to render plot with legend
- Legend updates automatically with mapped aesthetics
Full Transcript
This example shows how to control legends in R using ggplot2. First, we load the library. Then, we create a plot object mapping weight and mileage, coloring points by cylinder count. Adding geom_point adds the data points. Using labs(color='Cylinders') sets the legend title. Finally, print(p) renders the plot with the legend visible. The legend appears automatically because ggplot2 creates legends for mapped aesthetics. The legend title is customized with labs(). Without print(), the plot won't display in some environments. This step-by-step trace helps understand how legend control works in R plotting.