- 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 line chart using ggplot2 with a vertical line in R?
In general, the line chart is drawn to view the trend of something and we might also have some threshold point for that trend, for example, if blood pressure is plotted then we might want to show 60 mm Hg as well because this is the lowest acceptable value for blood pressure recommended by doctors. Therefore, it can be plotted as a vertical line if we want to plot blood pressures of a person. Similarly, there can be many situations where we can use a vertical line to visualize the threshold value. This can be achieved in ggplot2 with the help of geom_vline function.
Example
Consider the below data frame −
set.seed(10) x<-c(5,10,15,20,25) frequency<-c(1,12,8,16,22) df<-data.frame(x,frequency) df
Output
x frequency 1 5 1 2 10 12 3 15 8 4 20 16 5 25 22 library(ggplot2)
Creating a simple line chart −
ggplot(df,aes(x,frequency,group=1))+geom_line()
Output
Creating the chart with a vertical line on it −
ggplot(df,aes(x,frequency,group=1))+geom_line()+geom_vline(xintercept=8)
Output
- Related Articles
- How to create a dotted vertical line using ggplot2 in R?
- How to create a line chart using ggplot2 with larger width in R?
- How to create a wide vertical line using ggplot2 with different color in R?
- How to create a line chart with mean and standard deviation using ggplot2 in R?
- How to create line chart using ggplot2 in R with 3-sigma limits?
- How to create a line chart using ggplot2 that touches the edge in R?
- How to create line chart for categories with grey color palette using ggplot2 in R?
- How to create a line chart for a subset of a data frame using ggplot2 in R?
- How to create a line chart in R using plot function with larger width?
- How to create grouped line chart using ggplotly in R?
- How to change the color of lines for a line chart using ggplot2 in R?
- How to create a line chart using JavaFX?
- How to create a line chart using Matplotlib?
- How to create a vertical line with CSS?
- How to create a point chart with empty points using ggplot2 in R?

Advertisements