Skip to content
Related Articles

Related Articles

Use different colors/shapes for scatterplot with two groups in R

View Discussion
Improve Article
Save Article
  • Last Updated : 25 Aug, 2022
View Discussion
Improve Article
Save Article

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




# installing the reqd library
library("ggplot2")
 
# creating a data frame
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)
 
# plotting the col1 and col2 of data frame
# and assigning colors based on col1
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




# installing the reqd library
library("ggplot2")
 
# creating a data frame
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)
 
# assigning colors to different grouped
# points
cols <- c("blue","orange")
 
# plotting the col1 and col2 of data frame
# and assigning colors based on col1
ggplot(data_frame,aes(col1,col2,colour=col1))+geom_point()+
 
  # assigning customised colours to the plot
  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




# installing the reqd library
library("ggplot2")
 
# creating a data frame
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)
 
# assigning colors to different grouped
# points
cols <- c("blue","orange")
 
# plotting the col1 and col2 of data frame
# and assigning square based shape
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




# installing the reqd library
library("ggplot2")
 
# creating a data frame
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)
 
# assigning colors to different grouped points
cols <- c("blue","orange")
 
# plotting the col1 and col2 of data frame and
# assigning square based shape
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




# installing the reqd library
library("ggplot2")
 
# creating a data frame
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)
 
# assigning colors to different grouped
# points
cols <- c("blue","orange")
 
# plotting the col1 and col2 of data frame
# and assigning square based shape
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

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!