0
0
R Programmingprogramming~10 mins

Color scales and palettes in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Color scales and palettes
Define colors or palette
Create color scale function
Apply scale to data values
Generate colors for visualization
Use colors in plot or output
This flow shows how you start with colors or palettes, create a scale, apply it to data, and then use the colors in a plot.
Execution Sample
R Programming
library(scales)
vals <- c(0, 0.5, 1)
col_fun <- col_numeric(palette = c("blue", "white", "red"), domain = c(0,1))
colors <- col_fun(vals)
print(colors)
This code creates a numeric color scale from blue to white to red, applies it to values 0, 0.5, and 1, then prints the resulting colors.
Execution Table
StepActionInput/ConditionResult/Output
1Load scales librarylibrary(scales)scales functions available
2Define valuesvals = c(0, 0.5, 1)vals = 0, 0.5, 1
3Create color functionpalette = blue, white, red; domain = 0 to 1col_fun created
4Apply col_fun to valsvals = 0, 0.5, 1colors = "#0000FF", "#FFFFFF", "#FF0000"
5Print colorscolors["#0000FF", "#FFFFFF", "#FF0000"]
💡 All values processed; colors generated for each input value.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
valsundefinedc(0, 0.5, 1)c(0, 0.5, 1)c(0, 0.5, 1)c(0, 0.5, 1)
col_funundefinedundefinedfunction (numeric -> color)function (numeric -> color)function (numeric -> color)
colorsundefinedundefinedundefinedc("#0000FF", "#FFFFFF", "#FF0000")c("#0000FF", "#FFFFFF", "#FF0000")
Key Moments - 2 Insights
Why does the color for 0.5 become white?
Because the palette is defined as blue to white to red, 0.5 is the middle of the domain (0 to 1), so it maps to the middle color white, as shown in execution_table step 4.
What happens if a value is outside the domain 0 to 1?
Values outside the domain will be clamped to the nearest color in the palette. For example, a value less than 0 gets blue, greater than 1 gets red. This is standard behavior of col_numeric.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 4, what color corresponds to the value 0?
A"#0000FF" (blue)
B"#FFFFFF" (white)
C"#FF0000" (red)
D"#00FF00" (green)
💡 Hint
Check the colors generated in step 4 for vals = 0.
According to variable_tracker, what is the value of 'colors' after step 4?
Ac("#FFFFFF", "#0000FF", "#FF0000")
Bc("#FF0000", "#FFFFFF", "#0000FF")
Cc("#0000FF", "#FFFFFF", "#FF0000")
Dc("#000000", "#FFFFFF", "#FF0000")
💡 Hint
Look at the 'colors' row in variable_tracker after step 4.
If we add a value 1.5 to vals, what color will col_fun assign?
A"#0000FF" (blue)
B"#FF0000" (red)
C"#FFFFFF" (white)
DAn error occurs
💡 Hint
Recall that values outside domain are clamped to nearest palette color.
Concept Snapshot
Color scales map numeric values to colors.
Use col_numeric() with a palette and domain.
Apply the function to values to get colors.
Values outside domain clamp to ends.
Useful for coloring plots by data.
Full Transcript
This visual execution shows how to create and use color scales in R using the scales package. We start by loading the package, then define numeric values. Next, we create a color scale function with a palette from blue to white to red over the domain 0 to 1. Applying this function to the values 0, 0.5, and 1 produces colors blue, white, and red respectively. The variable tracker shows how variables change step by step. Key moments clarify why 0.5 maps to white and how values outside the domain behave. The quiz tests understanding of color mapping and clamping behavior. This helps beginners see how numeric data can be visually represented with colors in R.