Open In App

Transparent Scatterplot Points in Base R and ggplot2

Last Updated : 16 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will explore how to create transparent scatterplot points in R. We will use the alpha parameter within the plotting function to control the transparency of the points.

The alpha value ranges from 0 to 1, where a value closer to 0 makes the points more transparent, and a value closer to 1 makes them more opaque. By adjusting this parameter, we can enhance the visual clarity of overlapping data points in the scatterplot.

Method 1: Using Base R Programming

In this method for creating transparent scatterplot points, we need to install and load the scales package in R. This package allows us to adjust the alpha (transparency) levels of the data points. To apply transparency, we call the plot() function and use the alpha argument.

To install and import the scales package:

install.packages(“scales”)
library(“scales”)

Example:

In this example, we will plot a scatter plot of the given data and set the alpha value to 0.2 to create a transparent effect in the scatterplot using the R.

R
library("scales") 

gfg <- data.frame(x = c(1, 2.2, 2, 2, 3, 3,
                        4.2, 4.1, 5.2, 5.1),  
                   y = c(2, 2, 2.5, 2.1, 3.4,
                         4.1, 4, 4, 5, 5),
                group = as.factor(1:2))

plot(gfg$x, gfg$y, pch = 18, cex = 6,
     col = gfg$group)

Output:

Using alpha to create a transparent plot: 

R
library("scales") 

gfg <- data.frame(x = c(1, 2.2, 2, 2, 3, 3,
                        4.2, 4.1, 5.2, 5.1),  
                   y = c(2, 2, 2.5, 2.1, 3.4,
                         4.1, 4, 4, 5, 5),
                group = as.factor(1:2))

plot(gfg$x,gfg$y, pch = 18, cex = 6,
     col = alpha(gfg$group, 0.2))

Output:

Method 2: Using ggplot2 Package

To create transparent scatter plot points using the geom_point() function, we need to install and load the ggplot2 package in R. This package allows us to create scatter plots and other visualizations. To control transparency, we use the alpha argument within geom_point(), where lower values make points more transparent and values closer to 1 make them more opaque.

To install and import the ggplot2 package:

install.packages(“ggplot2”)
library(“ggplot2”)

Example:

In this example, we will plot the given data and set the value of the alpha argument to 0.2 to achieve a transparent effect in the scatterplot using the ggplot2 package in R.

R
library("ggplot2") 

gfg <- data.frame(x = c(1, 2.2, 2, 2, 3, 3,
                        4.2, 4.1, 5.2, 5.1),  
                   y = c(2, 2, 2.5, 2.1, 3.4,
                         4.1, 4, 4, 5, 5),
                group = as.factor(1:2))

ggplot(gfg, aes(x, y, col = group)) + 
geom_point(pch = 18,size = 12)

Output:

Using alpha to create a transparent plot:

R
library("ggplot2") 

gfg <- data.frame(x = c(1, 2.2, 2, 2, 3, 3, 4.2,
                        4.1, 5.2, 5.1),  
                   y = c(2, 2, 2.5, 2.1, 3.4, 4.1,
                         4, 4, 5, 5),
                group = as.factor(1:2))
ggplot(gfg, aes(x, y, col = group)) + 
geom_point(pch = 18,size = 12, alpha = 0.2)

Output:



Next Article
Article Tags :

Similar Reads