- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 two vertical lines on a plot with shaded area in-between using R?
Sometimes we want to place a vertical rectangle on a plot that has different color as compared to the rest of the plot area. This vertical rectangle is created based on the conditional values of x axis and represent the pivot area or unimportant area depending on the characteristics of the data. These values of x variable are placed as vertical lines on the plot and the area between these lines is shaded. It can be done by using geom_rect function.
Consider the below data frame −
> x<-rpois(10,2) > y<-rpois(10,5) > df<-data.frame(x,y) > df
x y 1 2 9 2 3 7 3 1 3 4 2 2 5 1 4 6 5 8 7 2 8 8 1 4 9 1 7 10 2 5
Creating a scatterplot between x and y −
> library(ggplot2) > ggplot(df,aes(x,y))+geom_point()
Adding vertical lines at x=2 and x=3 with shaded area in-between −
> ggplot(df,aes(x,y))+geom_point()+geom_rect(aes(xmin=2,xmax=3,ymin=0,ymax=Inf,fill =x))
- How to create dotted vertical lines in a plot using ggplot2 in R?
- How to create a circle with vertical lines in R?
- How to create a stacked bar plot with vertical bars in R using ggplot2?
- How to create two lines using ggplot2 based on a categorical column in R?
- How to join points on a scatterplot with smooth lines in R using plot function?
- How to create quantile regression plot with larger width of lines using ggplot2 in R?
- How to create a plot in R with gridlines using plot function?
- How to create a scatterplot in R with legend position inside the plot area using ggplot2?
- How to create the stacked bar plot using ggplot2 in R with labels on the plot?
- How to create two plots using ggplot2 arranged in a vertical manner in R?
- How to create a bar plot using ggplot2 with percentage on Y-axis in R?
- Create multiple regression lines in a single plot using ggplot2 in R.
- How to create a plot in base R with tick marks but excluding axes lines?
- How to create a plot in R with a different plot window size using plot function?
- Fill between two vertical lines in matplotlib