- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 plot using ggplot2 by including values greater than a certain value in R?
To create a plot using ggplot2 by excluding values greater than a certain value, we can use subsetting with single square brackets and which function. For example, if we have a data frame called df that contains two columns say x and y, then the point chart by including values of x that are greater than 0 can be created by using the command −
ggplot(df[which(df$x>0),],aes(x,y))+geom_point()
Example
Consider the below data frame −
> x<-rnorm(20) > y<-rnorm(20) > df<-data.frame(x,y) > df
Output
x y 1 -0.62160328 0.38477515 2 0.68287365 -1.56169067 3 0.75259774 1.28849990 4 0.56688920 -0.17014225 5 1.22351113 -0.32446764 6 -1.54210099 0.29001967 7 0.08800284 1.34342269 8 1.77498480 -0.75239348 9 -0.31916824 0.24433868 10 0.09802049 -0.91107863 11 -1.63060088 0.05336120 12 0.01328284 -2.36494891 13 -1.69921881 -1.29001305 14 -0.02819300 -0.06126524 15 0.77405426 0.25468262 16 -0.36423968 0.79130216 17 0.26224330 0.10437648 18 0.31894879 -0.50317250 19 0.37739488 0.62952910 20 0.26141716 -0.97143860
Loading ggplot2 package and creating scatterplot between x and y −
> library(ggplot2) > ggplot(df,aes(x,y))+geom_point()
Output
Creating scatterplot between x and y by including only x values that are greater than 0 −
> ggplot(df[which(df$x>0),],aes(x,y))+geom_point()
Output
- Related Articles
- How to create a subset of matrix in R using greater than or less than a certain value of a column?
- How to find the frequency of values greater than or equal to a certain value in R?
- How to create a dot plot using ggplot2 in R?
- How to create horizontal line for a range of values in a plot created by using ggplot2 in R?
- How to create bar plot with log values using ggplot2 in R?
- How to check whether a column value is less than or greater than a certain value in R?
- How to change legend values in a bar plot created by using ggplot2 in R?
- How to check if a list element is greater than a certain value in R?
- Replace all values in an R data frame if they are greater than a certain value.
- How to create a bar plot with ggplot2 using stat_summary in R?
- How to subset rows of an R data frame if any columns have values greater than a certain value?
- How to subset rows of an R data frame if all columns have values greater than a certain value
- How to create dotted vertical lines in a plot using ggplot2 in R?
- How to calculate the number of elements greater than a certain value in a vector in R?
- How to create an empty plot using ggplot2 in R?

Advertisements