- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a scatterplot with colors of the group of points in R?
A scatterplot is the plot that has one dependent variable plotted on Y-axis and one independent variable plotted on X-axis. Sometimes the pair of dependent and independent variable are grouped with some characteristics, thus, we might want to create the scatterplot with different colors of the group based on characteristics. For this purpose, we can use colour argument in ggplot function.
Example
Consider the below data frame −
set.seed(123) x <-rpois(10,2) y <-rpois(10,5) group <-c("A","B","C","A","A","A","C","B","B","C") df <-data.frame(x,y,group) df
Output
x y group 1 1 9 A 2 3 5 B 3 2 6 C 4 4 5 A 5 4 2 A 6 0 8 A 7 2 3 C 8 4 2 B 9 2 4 B 10 2 9 C
Example
Creating a simple scatterplot −
library(ggplot2) ggplot(df,aes(x,y))+geom_point()
Output
Creating a scatterplot with color of points based on groups −
ggplot(df,aes(x,y,colour=group))+geom_point()
Output
- Related Articles
- How to create a scatterplot with colors as group in R?
- How to create a scatterplot in R using ggplot2 with transparency of points?
- How to create a scatterplot in R using ggplot2 with different designs of points?
- How to create a scatterplot with low intensity of points using ggplot2 in R?
- How to create a scatterplot with dark points using ggplot2 in R?
- How to create a scatterplot in base R with points depending on categorical column?
- How to create a scatterplot with log10 of dependent variable in R?
- How to change the border color of points in a scatterplot created with ggplot2 in R?
- How to create a scatterplot using ggplot2 with different shape and color of points based on a variable in R?
- How to change the color of points in a scatterplot using ggplot2 in R?
- How to create a scatterplot with two legends using ggplot2 in R?
- How to create a scatterplot with larger distance between facets in R?
- How to change the default type of points for scatterplot using ggplot2 in R?
- How to join points on a scatterplot with smooth lines in R using plot function?
- How to create plot in R with different shape of points?

Advertisements