Find Transparent Equivalent of Color in R (2 Examples)

 

This page shows how to determine the transparent version of a hex color code in the R programming language.

The post contains these content blocks:

Here’s how to do it:

 

Example Color & Add-On Packages

The first step is to create an exemplifying color object.

my_col <- "#1B98E0FF"                               # Define hex color code

We also need to install and load the scales package, in order to display our colors in R:

install.packages("scales")                          # Install & load scales
library("scales")

We can now use the show_col function to show our example color in RStudio:

show_col(my_col)                                    # Show color

 

r graph figure 1 find transparent equivalent color r

 

As shown in Figure 1, the previously shown code has managed to create a plot displaying our example color in R.

 

Example 1: Convert Color Code into Transparent Version

In Example 1, I’ll illustrate how to determine the equivalent hex color code of our example color with a decreased alpha opacity of 0.2.

Have a look at the following R syntax:

my_col_alpha <- adjustcolor(my_col, alpha.f = 0.2)  # Create transparent color code
my_col_alpha                                        # Print new color code
# [1] "#1B98E033"

As you can see based on the previous output of the RStudio console, the transparent equivalent of our example color is the hex color code #1B98E033.

Let’s show this color in comparison to the original input color:

show_col(c(my_col, my_col_alpha))                   # Show original & transparent color

 

r graph figure 2 find transparent equivalent color r

 

Figure 2 shows the output of the previous code: As you can see the new version of our color has a low opacity.

 

Example 2: Convert Color Code into Vector of Multiple Transparent Versions

Example 2 illustrates how to create a sequence of color codes with decreasing opacity.

For this, we can use a for-loop as shown below:

my_col_alpha_all <- character()                     # Create vector of color codes
for(i in 1:20) {
  my_col_alpha_all[i] <- adjustcolor(my_col, alpha.f = i / 20)
}

Let’s have a look at the output of the previous for-loop:

my_col_alpha_all                                    # Print vector of color codes
#  [1] "#1B98E00D" "#1B98E01A" "#1B98E026" "#1B98E033" "#1B98E040" "#1B98E04D" "#1B98E059" "#1B98E066" "#1B98E073"
# [10] "#1B98E080" "#1B98E08C" "#1B98E099" "#1B98E0A6" "#1B98E0B3" "#1B98E0BF" "#1B98E0CC" "#1B98E0D9" "#1B98E0E6"
# [19] "#1B98E0F2" "#1B98E0FF"

As you can see, we have created a vector containing 20 different color codes.

Let’s visualize these colors in R:

show_col(my_col_alpha_all)                          # Show all colors

 

r graph figure 3 find transparent equivalent color r

 

As shown in Figure 3, the previous R code has created a graphic showing 20 different alpha versions of our input color and the corresponding hex color codes.

 

Video, Further Resources & Summary

Have a look at the following video that I have published on my YouTube channel. I show the R code of this tutorial in the video.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you might read the related articles on my website. I have published several articles already:

 

Summary: This tutorial has shown how to get the color code of a given color with less alpha in R programming. If you have additional questions, don’t hesitate to let me know in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


2 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top