Mapping dates to the viridis colour scale in ggplot2
Last Updated :
04 Oct, 2024
In data visualization, color plays a crucial role in distinguishing data points and revealing patterns. When dealing with continuous variables like dates, the choice of color scales can greatly affect the clarity of the visualization. One of the best color scales for continuous data is the Viridis color scale in R Programming Language.
Introduction to the Viridis Color Scale
The Viridis color scale was developed to ensure perceptual uniformity, meaning that the color changes correspond to changes in data values consistently across the scale. It is also designed to be accessible to those with color vision deficiencies. Viridis comes in four variants:
- viridis: A perceptually uniform color scale that transitions from dark blue to green and then yellow.
- magma: A perceptually uniform color scale that transitions from dark purple to orange and yellow.
- plasma: A perceptually uniform color scale that transitions from purple to yellow and orange.
- inferno: A perceptually uniform color scale that transitions from dark purple to yellow via red and orange.
Why Use Viridis?
Here is the main reasons why we use viridis colour scale:
- Perceptual Uniformity: It is easy to perceive changes across the range.
- Colorblind-Friendly: Works well for those with color vision deficiencies.
- Print-Friendly: It looks good in grayscale and works well in publications.
- Matplotlib Compatibility: Widely used in Python and easily adaptable in R.
Mapping Dates in ggplot2
In ggplot2, we can map dates directly to a color scale. ggplot2
handles date-type data well, and it can use color scales to represent them on a plot. When visualizing time-based data, such as events over a calendar year or time points in a time series, mapping dates to color can make trends easier to identify.
Step 1: Loading the required Packages
First we will load the package along with ggplot2
:
R
library(ggplot2)
library(viridis)
Step 2: Create dataset
Let's start by creating an example dataset with dates:
R
# Create a sample dataset with dates
set.seed(123)
df <- data.frame(
date = seq(as.Date("2023-01-01"), as.Date("2023-12-31"), by = "month"),
value = rnorm(12, 20, 5)
)
head(df)
Output:
date value
1 2023-01-01 17.19762
2 2023-02-01 18.84911
3 2023-03-01 27.79354
4 2023-04-01 20.35254
5 2023-05-01 20.64644
6 2023-06-01 28.57532
This dataset has dates ranging from January 2023 to December 2023 and some random values associated with each date.
Step 3: Create one basic plot
Now we will create one basic plot.
R
# Create a scatter plot and map date to the viridis color scale
ggplot(df, aes(x = date, y = value, color = date)) +
geom_point(size = 4) +
theme_minimal() +
labs(title = "Mapping Dates to Viridis Color Scale",
x = "Date",
y = "Value",
color = "Date")
Output:
Create one basic plotStep 4: Customizing the Viridis Color Scale
To map dates to the Viridis color scale in a ggplot visualization, we will use the scale_color_viridis_c()
function for continuous data, or scale_fill_viridis_c()
if filling areas.
R
# Create a scatter plot and map date to the viridis color scale
ggplot(df, aes(x = date, y = value, color = date)) +
geom_point(size = 4) +
scale_color_viridis_c(option = "viridis") +
theme_minimal() +
labs(title = "Mapping Dates to Viridis Color Scale",
x = "Date",
y = "Value",
color = "Date")
Output:
Customizing the Viridis Color Scale- The x-axis represents the date.
- The y-axis represents the value.
- The color aesthetic is mapped to the date, and we apply the Viridis color scale to it using
scale_color_viridis_c()
.
scale_color_viridis_c(option = "viridis")
: Applies the Viridis color palette to the continuous variable (dates). You can change "viridis"
to "magma"
, "plasma"
, or "inferno"
for different color variants.
Step 5: Customizing the Viridis Color Scale for line plot
Now we will dealing with time series data, a line plot is a more appropriate visualization.
R
# Create a line plot and map date to viridis color scale
ggplot(df, aes(x = date, y = value, color = date)) +
geom_line(size = 1.2) +
geom_point(size = 3) +
scale_color_viridis_c(option = "magma") +
theme_minimal() +
labs(title = "Line Plot with Viridis Color Scale",
x = "Date",
y = "Value",
color = "Date")
Output:
Customizing the Viridis Color Scale for line plot- The line and points are colored based on the date.
- The
magma
color scale is used here for variety.
Let’s use real-world time-series data to show the practical application of mapping dates to the Viridis color scale. We'll use the built-in airquality
dataset, which contains daily air quality measurements in New York.
R
# Load the airquality dataset
data("airquality")
# Convert Month and Day to a Date format
airquality$date <- as.Date(with(airquality, paste(1973, Month, Day, sep = "-")), "%Y-%m-%d")
# Plot with the Viridis color scale
ggplot(airquality, aes(x = date, y = Temp, color = date)) +
geom_line(size = 1) +
scale_color_viridis_c(option = "inferno") +
theme_minimal() +
labs(title = "Air Temperature Over Time (1973)",
x = "Date",
y = "Temperature (F)",
color = "Date")
Output:
Mapping dates to the viridis colour scale in ggplot2This plot visualizes the temperature changes over time, with dates mapped to the Inferno color scale. As time progresses, the color transitions, making it easier to track trends over the months.
Conclusion
Mapping dates to the Viridis color scale in ggplot2
allows you to create perceptually uniform and visually appealing visualizations. It enhances your data interpretation by effectively utilizing color to represent the passage of time, making trends and patterns more apparent. You can experiment with different Viridis color variants (viridis, magma, plasma, inferno) to suit your data and the message you want to convey.