Using Colors to Create Engaging Visualisations in R
Last Updated :
26 Apr, 2025
Colors are an effective medium for communicating information. The color display of data plays a critical role in visualization and exploratory data analysis. The exact use of color for data display allows for known interrelationships and patterns within data. The careless use of color will create uncertain patterns so, it becomes difficult to understand. The main part of data analysis is storytelling, using colors in storytelling helps stack holders understand the dashboard easily. Appropriate use of colors is used to make effective data-driven decisions.
Use colors to create engaging visuals in R
We have to use colors to create good quality visuals so, that make sense to the viewer. It helps viewers and stack holders to understand the information faster and more effectively. Color plays a significant role in data visualization. Viewers need to understand the report easily so if we highlight certain pieces of information and promote information recall it will be easy for viewers to understand the report created by analysts. Using colors strategically can aid pattern recognition and attract attention to important information and this is exactly what we will be looking at in this article using R.
Some rules to follow while using colors
- Use a single color to represent continuous data.
- Use contrasting colors to represent a comparison between columns.
- Use colors to make important data stand out.
- Use limited colors. Too many colors in a single dashboard lead to ambiguity.
- Use the correct graph style and coloring for your data.
To demonstrate the color usage in plots we will be using the Palmer Penguins dataset which has 8 attributes and 344 observations throughout this article. Penguins' data set gives complete information about penguins' bodies.
R
# install packages
install.packages("dplyr")
install.packages("palmerpenguins")
# load packages
library(dplyr)
library(palmerpenguins)
# summary of penguins dataset
summary(penguins)
Output:
Summary of the penguin datasetUsing default Colors with ggplot
When we draw some visualization using ggplot2 package the default colors are provided by the library to distinguish between different categories present in the data provided for visualization.
R
# load packages
install.packages("ggplot2")
library(ggplot2)
# data layer + Geometric layer + Aesthetics layer
ggplot(data = penguins) + geom_bar(mapping =
aes(x=bill_length_mm, fill = species))
Output:
Plot with default colors
If we use a bar chart for each penguin of different bill_length and bill_depth we cannot get clear data so, using the right graph is important along with colors below is an example of good usage of a suitable graph and colors. Also, one thing that we can observe in the below graph is that the colors used are the same as that present in the previous visualization this makes an analysis report boring and less comprehensive.
R
# load packages
library(ggplot2)
# data layer + Geometric layer + Aesthetics layer
ggplot(data = penguins) + geom_point(mapping =
aes(x=bill_length_mm, y=bill_depth_mm, col = species))
Output:
The different plot formed using ggplot with the same colors
We generally use col aesthetic to use colors in our visuals in the case of a bar graph col aesthetic does not give color variation it just outlines the bar in a bar chart so, for this problem we use the fill attribute to replace of cool aesthetic.
R
#install and load packages if you are using new R script
age <- c(19,34,56,23,45,18,34,32,17,15,16,9)
gender <- c("f","f","m","f","m","m","m","f","m","m","f","m")
height_in_cm <- c(183.98,178.56,167.984,156.78,178.35,145.78,190.23,167.23,156.278,178.89,189.56,130.90)
people <- data.frame(age, gender, height_in_cm)
ggplot(data = people) + geom_line(mapping = aes(x = height_in_cm, y = age, col = gender))
Output:
Wes Anderson package to Create Engaging Visualizations in R
There are also some inbuilt color palettes in R language these palettes are available in the Wes Anderson package. Install Wes Anderson package in R script to access available color palettes in R language.
Syntax:
wes_palette(name, n, type = c("discrete", "continuous"))
Parameters:
- name - Name of the color Palette
- n - Number of colors desired
- type
Some of the color palettes available in the Wes Anderson are as shown below:
- Royal1
- Cavalcanti1
- GrandBudapest1
- Moonrise1
- FantasticFox1
Let's start with the installation of this package.
R
# install package wesanderson
install.packages("wesanderson")
library(wesanderson)
Now we will draw some palettes and use them further to make visualizations. The first palette that we will be looking at is Royal1.
R
Output:
Royal1 color palette in Wes Anderson package
Now let's make a visualization using this Royal1 color palette.
R
monochromatic <- ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point(stat = "identity")
# Royal1 is a available palette name
monochromatic +
scale_color_manual(values = wes_palette(n=3,
name = "Royal1"))
Output:
Plot using Royal1 Color Palette
Now let's look at the Cavalcanti1 color palettes.
R
wes_palette("Cavalcanti1")
Output:
Cavalcanti1 Color Palette
Now let's make a visualization using this Cavalcanti1 color palette.
R
cavalcanti <- ggplot(penguins, aes(x = species,
y = body_mass_g,
fill = species)) + geom_bar(stat = "identity")
# cavalcanti is a available palette name
cavalcanti +
scale_color_manual(values = wes_palette(n=3,
name = "Cavalcanti1"))
Output:
Plot using Cavalcanti1 Color PaletteRColorBrewer package to Create Engaging Visualizations in R
Another package we can use for creating good visualizations in R is using RColorBrewer. This package creates good-looking color palettes for visualization. To know more about the RColorBrewer package. This article also covers different types of color palettes like Sequential, Diverging, and Qualitative color palettes.
R
# install and load RColorBrewer
install.packages("RColorBrewer")
library(RColorBrewer)
We can list all the colors with their key information using brewer.pal.info.
R
Output:
Different palettes available in RColorBrewer PackageFunctions in RColorBrewer
Below are some of the built-in functions which come in very handy while using the RColorBrewer package to fill colors in the visualizations we make in the process of analysis.
Function
| Description
|
brewer.pal(n, name) | makes the color palettes from ColorBrewer available as R color palettes |
display.the brewer.pal(n, name) | display the selected palettes in the R window |
display.brewer.all(n, type, colorblindFriendly) | displays the few palettes simultaneously in an R window |
brewer.pal.info | returns information about all available palettes as a data frame. brewer.pal.info is not a function it is a variable |
scale_fill_brewer() | Used to color for bar plot, box plot, violin plot, dot plot |
scale_color_brewer() | User to color for lines and points |
Arguments for Functions in RColorBrewer
To obtain different results with the same functions we can pass different types of arguments as well which can help us to obtain results as per our requirement.
Arguments
| Description
|
n | Number of different colors in the palette, minimum 3, maximum depending on the palette |
name | A palette name from the available palettes |
type | used to specify the type of palette using the string "div", "qual", "seq", or "all" |
select | A list of names of existing palettes |
exact.n | If TRUE, only display palettes with a color number given by n |
colorblind-friendly | If TRUE, display only colorblind-friendly palettes |
To get info about any particular color palette we can use the display.brewer.pal function to display a grid with strips of different colors that are included in that particular palette.
R
# display specific R color brewer palette
display.brewer.pal(n = 8, name = 'Dark2')
Output:
Dark2 color palette in RColorBrewer Package
We can also print the hexadecimal codes for the respective color palettes.
R
# return hexadecimal code of color palette
brewer.pal(n = 8, name = 'RdBu')
Output:
"#B2182B" "#D6604D" "#F4A582" "#FDDBC7" "#D1E5F0" "#92C5DE" "#4393C3" "#2166AC"
Now let's see some of the color palettes in action. First, we will try the Dark2 color palette in the below visualization.
R
temp <- ggplot(data=penguins) + geom_point(mapping = aes(x = bill_length_mm,
y = bill_depth_mm,
col = species))
temp + scale_color_brewer(palette = 'Dark2')
Output:
Exemplary plot with Dark2 Color Palette
Now, we will try the Greens color palette in the below visualization.
R
temp <- ggplot(data=penguins) + geom_point(mapping = aes(x = bill_length_mm,
y = bill_depth_mm,
col = species))
temp + scale_color_brewer(palette = 'Greens')
Output:
Exemplary plot with Greens Color Palette
Now, we will try the RdGy color palette in the below visualization.
R
temp <- ggplot(data=penguins) + geom_point(mapping = aes(x = bill_length_mm,
y = bill_depth_mm,
col = species))
temp + scale_color_brewer(palette = 'RdGy')
Output:
Exemplary plot with RdGy Color Palette
Similar Reads
Data visualization with R and ggplot2
The ggplot2 ( Grammar of Graphics ) is a free, open-source visualization package widely used in R Programming Language. It includes several layers on which it is governed. The layers are as follows:Layers with the grammar of graphicsData: The element is the data set itself.Aesthetics: The data is to
7 min read
Working with External Data
Basic Plotting with ggplot2
Plot Only One Variable in ggplot2 Plot in R
In this article, we will be looking at the two different methods to plot only one variable in the ggplot2 plot in the R programming language. Draw ggplot2 Plot Based On Only One Variable Using ggplot & nrow Functions In this approach to drawing a ggplot2 plot based on the only one variable, firs
5 min read
How to create a plot using ggplot2 with Multiple Lines in R ?
In this article, we will discuss how to create a plot using ggplot2 with multiple lines in the R programming language. Method 1: Using geom_line() function In this approach to create a ggplot with multiple lines, the user need to first install and import the ggplot2 package in the R console and then
3 min read
Plot Lines from a List of DataFrames using ggplot2 in R
For data visualization, the ggplot2 package is frequently used because it allows us to create a wide range of plots. To effectively display trends or patterns, we can combine multiple data frames to create a combined plot.Syntax: ggplot(data = NULL, mapping = aes(), colour())Parameters:data - Defaul
3 min read
How to plot a subset of a dataframe using ggplot2 in R ?
In this article, we will discuss plotting a subset of a data frame using ggplot2 in the R programming language. Dataframe in use: Â AgeScoreEnrollNo117700521880103177915419752051885256199630717903581971409188345 To get a complete picture, let us first draw a complete data frame. Example: R # Load ggp
9 min read
Change Theme Color in ggplot2 Plot in R
A theme in ggplot2 is a collection of settings that control the non-data elements of the plot. These settings include things like background colors, grid lines, axis labels, and text sizes. we can use various theme-related functions to customize the appearance of your plots, including changing theme
4 min read
Modify axis, legend, and plot labels using ggplot2 in R
In this article, we are going to see how to modify the axis labels, legend, and plot labels using ggplot2 bar plot in R programming language. For creating a simple bar plot we will use the function geom_bar( ). Syntax: geom_bar(stat, fill, color, width) Parameters :Â Â stat : Set the stat parameter to
5 min read
Common Geometric Objects (Geoms)
Comprehensive Guide to Scatter Plot using ggplot2 in R
Scatter plot uses dots to represent values for two different numeric variables and is used to observe relationships between those variables. To plot the Scatter plot we will use we will be using the geom_point() function. This function is available in ggplot2 package which is a free and open-source
7 min read
Line Plot using ggplot2 in R
In a line graph, we have the horizontal axis value through which the line will be ordered and connected using the vertical axis values. We are going to use the R package ggplot2 which has several layers in it. First, you need to install the ggplot2 package if it is not previously installed in R Stu
6 min read
R - Bar Charts
Bar charts provide an easy method of representing categorical data in the form of bars. The length or height of each bar represents the value of the category it represents. In R, bar charts are created using the function barplot(), and it can be applied both for vertical and horizontal charts.Syntax
4 min read
Histogram in R using ggplot2
A histogram is an approximate representation of the distribution of numerical data. In a histogram, each bar groups numbers into ranges. Taller bars show that more data falls in that range. It is used to display the shape and spread of continuous sample data.Plotting Histogram using ggplot2 in RWe c
5 min read
Box plot in R using ggplot2
A box plot is a graphical display of a data set which indicates its distribution and highlights potential outliers It displays the range of the data, the median, and the quartiles, making it easy to observe the spread and skewness of the data.In ggplot2, the geom_boxplot() function is used to create
5 min read
geom_area plot with areas and outlines in ggplot2 in R
An Area Plot helps us to visualize the variation in quantitative quantity with respect to some other quantity. It is simply a line chart where the area under the plot is colored/shaded. It is best used to study the trends of variation over a period of time, where we want to analyze the value of one
3 min read
Advanced Data Visualization Techniques
Combine two ggplot2 plots from different DataFrame in R
In this article, we are going to learn how to Combine two ggplot2 plots from different DataFrame in R Programming Language. Here in this article we are using a scatter plot, but it can be applied to any other plot. Let us first individually draw two ggplot2 Scatter Plots by different DataFrames then
2 min read
Annotating text on individual facet in ggplot2 in R
In this article, we will discuss how to annotate a text on the Individual facet in ggplot2 in R Programming Language. To plot facet in R programming language, we use the facet_grid() function from the ggplot2 library. The facet_grid() is used to form a matrix of panels defined by row and column face
5 min read
How to annotate a plot in ggplot2 in R ?
In this article, we will discuss how to annotate functions in R Programming Language in ggplot2 and also read the use cases of annotate. What is annotate?An annotate function in R can help the readability of a plot. It allows adding text to a plot or highlighting a specific portion of the curve. Th
4 min read
Annotate Text Outside of ggplot2 Plot in R
Ggplot2 is based on the grammar of graphics, the idea that you can build every graph from the same few components: a data set, a set of geomsâvisual marks that represent data points, and a coordinate system. There are many scenarios where we need to annotate outside the plot area or specific area as
2 min read
How to put text on different lines to ggplot2 plot in R?
ggplot2 is a plotting package in R programming language that is used to create complex plots from data specified in a data frame. It provides a more programmatic interface for specifying which variables to plot onto the graphical device, how they are displayed, and general visual properties. In thi
3 min read
How to Connect Paired Points with Lines in Scatterplot in ggplot2 in R?
In this article, we will discuss how to connect paired points in scatter plot in ggplot2 in R Programming Language. Scatter plots help us to visualize the change in two more categorical clusters of data. Sometimes, we need to work with paired quantitative variables and try to visualize their relatio
2 min read
How to highlight text inside a plot created by ggplot2 using a box in R?
In this article, we will discuss how to highlight text inside a plot created by ggplot2 using a box in R programming language. There are many ways to do this, but we will be focusing on one of the ways. We will be using the geom_label function present in the ggplot2 package in R. This function allo
3 min read
Adding labels, titles, and legends in r
Working with Legends in R using ggplot2
A legend in a plot helps us to understand which groups belong to each bar, line, or box based on its type, color, etc. We can add a legend box in R using the legend() function. These work as guides. The keys can be determined by scale breaks. In this article, we will be working with legends and asso
6 min read
How to Add Labels Directly in ggplot2 in R
Labels are textual entities that have information about the data point they are attached to which helps in determining the context of those data points. In this article, we will discuss how to directly add labels to ggplot2 in R programming language. To put labels directly in the ggplot2 plot we add
5 min read
How to change legend title in ggplot2 in R?
In this article, we will see how to change the legend title using ggplot2 in R Programming. We will use ScatterPlot. For the Data of Scatter Plot, we will pick some 20 random values for the X and Y axis both using rnorm() function which can generate random normal values, and here we have one more p
3 min read
How to change legend title in R using ggplot ?
A legend helps understand what the different plots on the same graph indicate. They basically provide labels or names for useful data depicted by graphs. In this article, we will discuss how legend names can be changed in R Programming Language. Let us first see what legend title appears by default.
2 min read
Customizing Visual Appearance
Handling Data Subsets: Faceting
How to create a faceted line-graph using ggplot2 in R ?
A potent visualization tool that enables us to investigate the relationship between two variables at various levels of a third-category variable is the faceted line graph. The ggplot2 tool in R offers a simple and versatile method for making faceted line graphs. This visual depiction improves our co
6 min read
How to Combine Multiple ggplot2 Plots in R?
In this article, we will discuss how to combine multiple ggplot2 plots in the R programming language. Combining multiple ggplot2 plots using '+' sign to the final plot In this method to combine multiple plots, here the user can add different types of plots or plots with different data to a single p
2 min read
Change Labels of GGPLOT2 Facet Plot in R
In this article, we will see How To Change Labels of ggplot2 Facet Plot in R Programming language. To create a ggplot2 plot, we have to load ggplot2 package. library() function is used for that. Then either create or load dataframe. Create a regular plot with facets. The labels are added by default
3 min read
Change Font Size of ggplot2 Facet Grid Labels in R
In this article, we will see how to change font size of ggplot2 Facet Grid Labels in R Programming Language. Let us first draw a regular plot without any changes so that the difference is apparent. Example: R library("ggplot2") DF <- data.frame(X = rnorm(20), Y = rnorm(20), group = c("Label 1",
2 min read
Remove Labels from ggplot2 Facet Plot in R
In this article, we will discuss how to remove the labels from the facet plot in ggplot2 in the R Programming language. Facet plots, where one subsets the data based on a categorical variable and makes a series of similar plots with the same scale. We can easily plot a facetted plot using the facet_
2 min read
Grouping Data: Dodge and Position Adjustments