How to Color Scatter Plot Points in R ?
Last Updated :
26 Dec, 2022
A scatter plot is a set of dotted points to represent individual pieces of data in the horizontal and vertical axis. But by default, the color of these points is black and sometimes there might be a need to change the color of these points.
In this article, we will discuss how to change the color of points in scatterplot in the R Programming Language.
Method 1: Using plot()
The simple scatterplot is created using the plot() function.
Syntax: plot(x, y, main, xlab, ylab, xlim, ylim, axes)
Let us first create a scatterplot without any color so that the difference is apparent.
Example:
R
df<-read.csv("bestsellers.csv")
plot(df$Reviews,df$Price,pch=16)
Output:

Now to change the colors of a scatterplot using plot(), simply select the column on basis of which different colors should be assigned to various points. Pass the column that will help differentiate between points to "col" attribute.
Example:
R
df<-read.csv("bestsellers.csv")
plot(df$Reviews,df$Price,pch=16,col=df$Genre)
Output:

Method 2: Using ggplot
ggplot2 module supports geom_point() function that can help plot scatterplot. Let us first see how a scatterplot will appear without providing any mechanism for changing colors.
Example:
R
library("ggplot2")
df<-read.csv("bestsellers.csv")
ggplot(df,aes(x=Reviews,y=Price))+geom_point()
Output:

By using, ggplot there are various ways of adding colors to a scatterplot. Let us first discuss how colors are changed by default. For this simply pass the differentiating column to col attribute.
Example:
R
library("ggplot2")
df<-read.csv("bestsellers.csv")
ggplot(df,aes(x=Reviews,y=Price,col=Genre))+geom_point()
Output:

Another way of producing the same result is to provide the grouping column to group attribute and again to color in geom_point()
Example:
R
library("ggplot2")
df<-read.csv("bestsellers.csv")
ggplot(df,aes(x=Reviews,y=Price,group=Genre))+
geom_point(aes(color=Genre))
Output:

We can also add custom colors by using scale_color_manual() function with the list of colors to choose from.
Example
R
library("ggplot2")
df<-read.csv("bestsellers.csv")
ggplot(df,aes(x=Reviews,y=Price,group=Genre))+
geom_point(aes(color=Genre))+
scale_color_manual(values=c('Yellow','Green'))
Output:

A scatterplot can also display colors only from grayscale, for this use scale_color_grey() function.
Example:
R
library("ggplot2")
df<-read.csv("bestsellers.csv")
ggplot(df,aes(x=Reviews,y=Price,group=Genre))+
geom_point(aes(color=Genre))+
scale_color_grey()
Output:

scale_color_brewer() function is also a method to add colors to a scatterplot. This function takes the name of the palette to pick colors from.
Example:
R
library("ggplot2")
df<-read.csv("bestsellers.csv")
ggplot(df,aes(x=Reviews,y=Price,group=Genre))+
geom_point(aes(color=Genre))+
scale_color_brewer(palette="Accent")
Output:

Similar Reads
How to Make a Scatter Plot Matrix in R A scatterplot matrix is ââa grid of scatterplots that allows us to see how different pairs of variables are related to each other. We can easily generate a scatterplot matrix using the pairs() function in R programming. In this article, we will walk through the process of creating a scatterplot matr
6 min read
How to Plot 3D Scatter Diagram Using ggplot in R The ggplot2 package in R is one of the most popular tools for creating complex and aesthetically pleasing plots. However, ggplot2 is primarily designed for 2D plotting, which presents a challenge when it comes to creating 3D scatter plots. While ggplot2 does not natively support 3D plotting, it can
4 min read
Joining Points on Scatter plot using Smooth Lines in R A smooth line, also known as a smoothed line, is a line that is drawn through a set of data points in such a way that it represents the overall trend of the data while minimizing the effects of random fluctuations or noise. In other words, it is a way to represent a general pattern or trend in a dat
8 min read
What Is a Scatter Plot in Python? Scatter plots are a fundamental tool in data visualization, providing a visual representation of the relationship between two variables. In Python, scatter plots are commonly created using libraries such as Matplotlib and Seaborn. This article will delve into the concept of scatter plots, their appl
6 min read
Create Scatter plot from CSV in R In R Programming Language we use plot() function to display scatterplot. It takes eight parameters. Syntax: plot(x, y, main, xlab, ylab, xlim, ylim, axes) Parameters: x: sets variable to be used for horizontal coordinates.y: sets variable to be used for vertical coordinates.xlab: label for horizonta
3 min read
Control the Size of the Points in a Scatterplot in R In this article, we are going to see how to control the size of the points in a scatterplot in R Programming language. We will Control the size of the points in a scatterplot using cex argument of the plot function. In this approach to control the size of the points in a scatterplot, the user needs
2 min read
How to create a scatter plot using lattice package in R? In this article, we will discuss how to create the scatter plots using lattice package in R programming language. In R programming, the Lattice package is a data visualization library that consists of various functions to plot different kinds of plots. Using the lattice library we can able to plot v
2 min read
How to Create an X-Y Scatter Plot in Excel? Excel is powerful data visualization and data management tool which can be used to store, analyze, and create reports on large data. It can be used to visualize and compare data using a graph plot. In excel we can plot different kinds of graphs like line graphs, bar graphs, etc. to visualize or anal
2 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 can I explicitly assign unique colors to every point in an R Plotly scatterplot? Creating visually appealing and informative scatterplots is a key aspect of data visualization. In R, Plotly provides a powerful toolset for building interactive plots, including the ability to customize the color of each data point individually. This article explores how to explicitly assign unique
4 min read