Use different colors/shapes for scatterplot with two groups in R
Last Updated :
25 Aug, 2022
In this article, we will be looking to the different approaches to colors/shapes for scatterplots with two groups in R programming language.
The plot in R can be used for visual analysis of the data. The ggplot2 library in R is used to create data visualizations. The package can be downloaded and installed into the working space using the following command :
install.packages("ggplot2")
Data frame can be used to contain organized tabular data arranged in rows and columns. The column data may be grouped. The data points can then be classified into argument segments based on the values contained in these groups.
The ggplot object can be used to create a plot object. It also takes as data frame columns to plot and arguments the aesthetic mappings where in the data frame columns to plot attributes like color, size, and shape.
The method has the following syntax :
ggplot(data , aes = )
Arguments :
- data – The data to plot
- aes – The aesthetic mappings to use
The geom_point() method can be added to the ggplot object to plot the data in the form of points representing the data values.
Using geom_point() to color points corresponding to different groups
The color parameter can be added to the aesthetic mappings of the ggplot object to provide colors to the data point values. The color parameter can be assigned to the grouped column of the data frame, which, by default, assigns different colors to the values belonging to different groups.
ggplot(data, aes (...,colour = ))
R
library ( "ggplot2" )
data_frame <- data.frame (
col1 = c ( "g1" , "g2" , "g1" , "g1" ,
"g2" , "g1" , "g2" , "g2" ),
col2 = 1:8,
col3 = LETTERS [1:8]
)
print ( "Data Frame" )
print (data_frame)
ggplot (data_frame, aes (col1,col2,colour=col1))+ geom_point ()
|
Output
[1] "Data Frame"
> print(data_frame)
col1 col2 col3
1 g1 1 A
2 g2 2 B
3 g1 3 C
4 g1 4 D
5 g2 5 E
6 g1 6 F
7 g2 7 G
8 g2 8 H
Changing the colors of the plotted points
In order to provide customized colors to the data points, a method of the ggplot2 package can be used, scale_color_manual. The method can be used to take both hexadecimal as well string vector of color values. The vector is then assigned to the values parameter of this method. The length of the vector should be equivalent to the number of groups. The method then assigns different colors according to the grouped column of the data frame.
scale_color_manual(values = col-vec)
Arguments :
- col-vec – The color vector
R
library ( "ggplot2" )
data_frame <- data.frame (
col1 = c ( "g1" , "g2" , "g1" , "g1" ,
"g2" , "g1" , "g2" , "g2" ),
col2 = 1:8,
col3 = LETTERS [1:8]
)
print ( "Data Frame" )
print (data_frame)
cols <- c ( "blue" , "orange" )
ggplot (data_frame, aes (col1,col2,colour=col1))+ geom_point ()+
scale_color_manual (values = cols)
|
Output
[1] "Data Frame"
> print(data_frame)
col1 col2 col3
1 g1 1 A
2 g2 2 B
3 g1 3 C
4 g1 4 D
5 g2 5 E
6 g1 6 F
7 g2 7 G
8 g2 8 H
Changing the shape of the plotted points
Different shapes can be assigned to the data points in the plot. By default, circles are plotted to the specified points. If we wish to customize the shape, the shape number value can be specified in the geom_point() method as the shape argument. For instance, shape number 15 is used to construct squares, and shape number 17 constructs triangles.
geom_point(shape = )
R
library ( "ggplot2" )
data_frame <- data.frame (
col1 = c ( "g1" , "g2" , "g1" , "g1" ,
"g2" , "g1" , "g2" , "g2" ),
col2 = 1:8,
col3 = LETTERS [1:8]
)
print ( "Data Frame" )
print (data_frame)
cols <- c ( "blue" , "orange" )
ggplot (data_frame, aes (col1,col2,colour=col1))+ geom_point (shape = 15)
|
Output
[1] "Data Frame"
> print(data_frame)
col1 col2 col3
1 g1 1 A
2 g2 2 B
3 g1 3 C
4 g1 4 D
5 g2 5 E
6 g1 6 F
7 g2 7 G
8 g2 8 H
Assigning shapes based on the groups of plotted points
If we wish to plot the groups in different shapes as well as colors, we can specify and use both the parameters in the aesthetic mappings of the ggplot object. The shape and color can be assigned to the grouped column of the data frame. The below code snippet assigns orange-colored circles to group “g1” and blue-colored triangles to group “g2”
R
library ( "ggplot2" )
data_frame <- data.frame (
col1 = c ( "g1" , "g2" , "g1" , "g1" ,
"g2" , "g1" , "g2" , "g2" ),
col2 = 1:8,
col3 = LETTERS [1:8]
)
print ( "Data Frame" )
print (data_frame)
cols <- c ( "blue" , "orange" )
ggplot (data_frame, aes (col1,col2,colour=col1,shape=col1))+
geom_point ()
|
Output
[1] "Data Frame"
> print(data_frame)
col1 col2 col3
1 g1 1 A
2 g2 2 B
3 g1 3 C
4 g1 4 D
5 g2 5 E
6 g1 6 F
7 g2 7 G
8 g2 8 H
However, if we don’t assign the color parameter to points belonging to different groups, then only different shapes are assigned in black to the different data points.
R
library ( "ggplot2" )
data_frame <- data.frame (
col1 = c ( "g1" , "g2" , "g1" , "g1" ,
"g2" , "g1" , "g2" , "g2" ),
col2 = 1:8,
col3 = LETTERS [1:8]
)
print ( "Data Frame" )
print (data_frame)
cols <- c ( "blue" , "orange" )
ggplot (data_frame, aes (col1,col2,shape=col1))+
geom_point ()
|
Output
[1] "Data Frame"
> print(data_frame)
col1 col2 col3
1 g1 1 A
2 g2 2 B
3 g1 3 C
4 g1 4 D
5 g2 5 E
6 g1 6 F
7 g2 7 G
8 g2 8 H

Similar Reads
How to Use Different Shapes for Every Point in ggplot
Data visualization is crucial for clearly conveying insights derived from data. In R Programming Language ggplot2 is a widely used tool for crafting visually engaging and informative plots. While ggplot2 offers various ways to personalize plots, like adjusting colors and line styles, one feature tha
3 min read
How to Use the Jitter Function in R for Scatterplots?
In this article, we will discuss how to use the jitter function in the R programming Language for Scatterplots. Scatterplots is a visualization plot that uses cartesian coordinates to display values for typically two variables for a set of data by having them at the x-axis and the y-axis. This is ve
3 min read
Create Boxplot with respect to two factors using ggplot2 in R
Boxplots are an effective way to visualize the distribution of data, especially when comparing multiple variables. The ggplot2 package in R allows us to easily create grouped boxplots, which are helpful when we have multiple subgroups within a variable. The function used for creating boxplots in ggp
3 min read
How to Change the Color of Points for ggplot2 Scatterplot Using ColorBrewer in R
When visualizing data using scatter plots, coloring the points can help distinguish between categories or highlight certain patterns in the data. In this article, we will explore how to change the color of points in a ggplot2 scatterplot using RColorBrewer in R. Introduction to ColorBrewerRColorBrew
4 min read
How to make histogram bars to have different colors in Plotly in R?
In this article, we are going to discuss making histogram bars to have different colors in Plotly in R Language. The Histogram is defined as the bar graph representation of data along the x-axis. The plotly package contains plot_ly() function which is used to visualize different plots in R like scat
1 min read
Draw ggplot2 plot with two Y-axes on each side and different scales in R
There are many data available that have more than one unit like Temperature, Pressure, Height of a person, etc. We want to represent these data using more than one unit in a basic plot because other users may not be familiarized with the unit you have provided in the plot. It becomes difficult for t
3 min read
Create a Scatter Plot with Multiple Groups using ggplot2 in R
In this article, we will discuss how to create a scatter plot with multiple groups in R Programming Language. Geoms can be added to the plot to compute various graphical representations of the data in the plot (points, lines, bars). The geom_point() method is used to create scatter plots in R. The g
2 min read
Add Correlation Coefficients with P-values to a Scatter Plot in R
In this article, we will discuss how to add correlation coefficients with P-value to a scatter plot in the R Programming Language. To add correlation coefficient with P-value to a scatter plot, we use the stat_cor() function of the ggpubr package in the R Language. The ggpubr package provides some e
2 min read
How to Create a Scatterplot in R with Multiple Variables?
In this article, we will be looking at the way to create a scatter plot with multiple variables in the R programming language. Using Plot() And Points() Function In Base R: In this approach to create a scatter plot with multiple variables, the user needs to call the plot() function Plot() function:
3 min read
How to Create a Scatterplot with a Regression Line in R?
A scatter plot uses dots to represent values for two different numeric variables. Scatter plots are used to observe relationships between variables. A linear regression is a straight line representation of relationship between an independent and dependent variable. In this article, we will discuss h
3 min read