Jul 09, 2026 — Digital Edition
George Ideas
Independent Journalism & Insight
Feature

Mastering R: A Step-by-Step Guide to Coloring Graphs

Embellishing your graphs with color can significantly enhance their readability and aesthetic appeal. R, a powerful programming language for statistical computing and graphics, offers a wide range of color customization options. Let's delve into how you can color your graphs in R.

the favorite color graph with pencils and crayons on it for kids to draw
the favorite color graph with pencils and crayons on it for kids to draw

Before we dive into the specifics, it's crucial to understand that R uses the 'ggplot2' package for data visualization, which is based on the grammar of graphics. This package provides a flexible and efficient way to create and customize plots.

the color wheel with different shades to choose from, which one do not shade with?
the color wheel with different shades to choose from, which one do not shade with?

Understanding Colors in R

R uses a color palette system to define colors. These palettes are collections of colors that can be used to fill or outline shapes in your plots. Some of the most commonly used palettes include 'viridis', 'plasma', and 'magma'.

Sort & Graph Colorful Rainbow Bears: Preschool Math Activity
Sort & Graph Colorful Rainbow Bears: Preschool Math Activity

You can also specify colors using their hexadecimal codes, RGB values, or even by name. For instance, 'red' is equivalent to '#FF0000', and 'blue' is equivalent to '#0000FF'.

Filling Plots with Color

Stata graphs: Define your own color schemes
Stata graphs: Define your own color schemes

To fill a plot with color, you can use the 'fill' aesthetic in 'ggplot2'. This aesthetic maps a variable to the color of the fill. Here's a simple example:

ggplot(data, aes(x, y, fill = z)) + geom_bar(stat = 'identity')

In this example, 'z' is the variable that determines the color of the bars in the bar plot.

an info sheet with different types of graphs and diagrams on it, including data points
an info sheet with different types of graphs and diagrams on it, including data points

Changing the Color Palette

If you want to use a different color palette, you can do so by specifying it in the 'scale_fill_brewer()' function. Here's how you can use the 'viridis' palette:

ggplot(data, aes(x, y, fill = z)) + geom_bar(stat = 'identity') + scale_fill_viridis(discrete = TRUE)

Graphing Favorite Colors
Graphing Favorite Colors

The 'discrete = TRUE' argument is used when the fill variable is categorical. If it's continuous, you can set it to 'FALSE'.

Customizing Color Scales

an image of a poster with different colored dots on it's back side and the words, risograph color chart
an image of a poster with different colored dots on it's back side and the words, risograph color chart
the color scheme for an art project is shown in this diagram, with different colors
the color scheme for an art project is shown in this diagram, with different colors
Client Challenge
Client Challenge
GGPlot Colors Best Tricks You Will Love - Datanovia
GGPlot Colors Best Tricks You Will Love - Datanovia
Extended Dumbbell Plot in R with ggplot2
Extended Dumbbell Plot in R with ggplot2
Trendy Gradients in Web Design
Trendy Gradients in Web Design
the color chart for pindot's press - out chart is shown in red, yellow, blue, and green
the color chart for pindot's press - out chart is shown in red, yellow, blue, and green
ILLUSTRATOR TUTORIAL: How To Create Colored Graph (Illustrator Graphics Design Tutorial)
ILLUSTRATOR TUTORIAL: How To Create Colored Graph (Illustrator Graphics Design Tutorial)
a poster with different colors and numbers on it
a poster with different colors and numbers on it
The R Graph Gallery – Help and inspiration for R charts
The R Graph Gallery – Help and inspiration for R charts
Favorite Color Graph with Print & Cut Printable
Favorite Color Graph with Print & Cut Printable
graph art is an easy activity for kids to learn how to draw and color
graph art is an easy activity for kids to learn how to draw and color
How to create a hexagonal heatmap in R
How to create a hexagonal heatmap in R
20 Graphing Activities For Kids That Really Raise the Bar
20 Graphing Activities For Kids That Really Raise the Bar
a poster with different colored circles and the words, create a riso effect in illustrator
a poster with different colored circles and the words, create a riso effect in illustrator
an image of a cat that is made out of different types of pixels and colors
an image of a cat that is made out of different types of pixels and colors
Animal Grid Coloring Pages | Woo! Jr. Kids Activities : Children's Publishing
Animal Grid Coloring Pages | Woo! Jr. Kids Activities : Children's Publishing
the instructions for how to make mosaic tiles in adobe and photoshopped with text
the instructions for how to make mosaic tiles in adobe and photoshopped with text
知識ゼロから Illustrator をしっかり学ぶ!デザインの作り方、チュートリアルまとめ | PhotoshopVIP
知識ゼロから Illustrator をしっかり学ぶ!デザインの作り方、チュートリアルまとめ | PhotoshopVIP
Graph paper art with acyrlic pens
Graph paper art with acyrlic pens

Sometimes, you might want to customize the color scale to better suit your data or preferences. R provides several functions to do this, such as 'scale_fill_gradient()' and 'scale_fill_hue()'.

For instance, to create a gradient of colors from blue to red, you can use:

ggplot(data, aes(x, y, fill = z)) + geom_bar(stat = 'identity') + scale_fill_gradient(low = 'blue', high = 'red')

Using Color Blends

R also allows you to use color blends, which are combinations of two or more colors. You can create a color blend using the 'blend2()' function from the 'colorspace' package. Here's an example:

library(colorspace)

my_color <- blend2(c('blue', 'red'), c(0.2, 0.8))

In this example, 'my_color' is a blend of blue and red, with red being 80% of the final color.

Changing the Color of Plot Elements

Besides filling plots with color, you can also change the color of other plot elements, such as the outline of shapes or the color of text. This can be done using the 'color' aesthetic in 'ggplot2'.

Here's an example of how to change the color of the outline of bars in a bar plot:

ggplot(data, aes(x, y)) + geom_bar(stat = 'identity', color = 'black')

In conclusion, R provides a wide range of options for coloring your graphs. Whether you're filling plots with color, changing the color palette, or customizing color scales, R has a function for it. So go ahead, experiment with colors, and make your graphs not just informative, but also visually appealing.