- 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 line chart using ggplot2 that touches the edge in R?
To create a line chart using ggplot2 that touches the edge we can follow the below steps −
- First of all, creating data frame.
- Then loading ggplot2 package and creating the line chart in default manner.
- After that creating the line chart with coord_cartesian function.
Create the data frame
Let's create a data frame as shown below −
x<-rpois(5,2) y<-rpois(5,5) df<-data.frame(x,y) df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x y 1 2 5 2 1 4 3 3 8 4 1 3 5 2 5
Creating the line chart in default manner
Loading ggplot2 package and creating the line chart in default manner −
library(ggplot2) x<-rpois(5,2) y<-rpois(5,5) df<-data.frame(x,y) ggplot(df,aes(x,y))+geom_line()
Output
Creating the line chart that touches the edges
Use coord_cartesian function of ggplot2 package to create the line chart where line touches the edges −
library(ggplot2) x<-rpois(5,2) y<-rpois(5,5) df<-data.frame(x,y) ggplot(df,aes(x,y))+geom_line()+coord_cartesian(xlim=c(1.09,2.91))
Output
- Related Articles
- How to create a line chart using ggplot2 with a vertical line in R?
- How to create a line chart using ggplot2 with larger width in R?
- How to create line chart using ggplot2 in R with 3-sigma limits?
- How to create a line chart with mean and standard deviation using ggplot2 in R?
- How to create a line chart for a subset of a data frame using ggplot2 in R?
- How to create line chart for categories with grey color palette using ggplot2 in R?
- How to create bar chart using ggplot2 with chart sub-title in R?
- How to create a bar chart using ggplot2 with dots drawn at the center of top edge of the bars in R?
- How to change the color of lines for a line chart using ggplot2 in R?
- How to create a dotted vertical line using ggplot2 in R?
- How to create a point chart with empty points using ggplot2 in R?
- How to create a point chart for cumulative sums using ggplot2 in R?
- How to create a bar chart for single vector using ggplot2 in R?
- How to create the bar chart with ggplot2 using color brewer in R?
- How to create grouped line chart using ggplotly in R?

Advertisements