- 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 color scatterplot points based on a threshold using ggplot2 in R?
To color scatterplot points based on a threshold using ggplot2, we first need to define a column with the threshold value and then we can use that column inside aes for coloring. The column with threshold can be created by using cut function.
Check out the example given below to understand how it can be done.
Example
Following snippet creates a sample data frame −
x<-rpois(20,5) y<-rpois(20,2) df<-data.frame(x,y) df
Output
The following dataframe is created −
x y 1 6 0 2 5 5 3 7 2 4 4 0 5 4 1 6 5 0 7 9 3 8 4 1 9 7 1 10 5 2 11 4 3 12 8 3 13 4 0 14 9 0 15 5 1 16 3 3 17 7 1 18 2 2 19 5 3 20 5 1
To create a threshold column based on values in column y, add the following code to the above snippet −
x<-rpois(20,5) y<-rpois(20,2) df<-data.frame(x,y) df$Threshold<-cut(df$y,breaks=c(-Inf,2,Inf),labels=c("<=2",">2")) df
Output
If you execute all the above given snippets as a single program, it generates the following Output −
x y Threshold 1 6 0 <=2 2 5 5 >2 3 7 2 <=2 4 4 0 <=2 5 4 1 <=2 6 5 0 <=2 7 9 3 >2 8 4 1 <=2 9 7 1 <=2 10 5 2 <=2 11 4 3 >2 12 8 3 >2 13 4 0 <=2 14 9 0 <=2 15 5 1 <=2 16 3 3 >2 17 7 1 <=2 18 2 2 <=2 19 5 3 >2 20 5 1 <=2
To load ggplot2 package and create scatterplot between x and y with different color of points based on threshold column, add the following code to the above snippet −
library(ggplot2) ggplot(df,aes(x,y,color=Threshold))+geom_point()
Output
If you execute all the above given snippets as a single program, it generates the following Output −
- Related Articles
- 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 change the color of points for ggplot2 scatterplot using color brewer in R?
- How to increase the size of points on a scatterplot if the points are drawn based on another sequence using ggplot2 in R?
- How to create a scatterplot with dark points using ggplot2 in R?
- How to change the border color of points in a scatterplot created with ggplot2 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 scatterplot for categories with grey color palette using ggplot2 in R?
- How to change the default type of points for scatterplot using ggplot2 in R?
- How to display NA group values in scatterplot created with ggplot2 using color brewer in R?
- How to create a scatterplot with two legends using ggplot2 in R?
- How to create scatterplot using ggplot2 without grey background in R?
- How to join points on a scatterplot with smooth lines in R using plot function?
