- 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 reduce the space between two plots that are joined with grid.arrange in R?
When we join or combine plots using grid.arrange the scales of the first plot comes in between as X-axis even if the independent variable in both of the plots is same.
Therefore, we might want to remove the space between the plots while joining to get only one X-axis. This can be done by using theme function.
Example
Consider the below data frame −
set.seed(123) x<-rnorm(10,1) y<-rnorm(10,2) df<-data.frame(x,y) df
Output
x y 1 0.4395244 3.22408180 2 0.7698225 2.35981383 3 2.5587083 2.40077145 4 1.0705084 2.11068272 5 1.1292877 1.44415887 6 2.7150650 3.78691314 7 1.4609162 2.49785048 8 -0.2650612 0.03338284 9 0.3131471 2.70135590 10 0.5543380 1.52720859
Loading ggplot2 and gridExtra package −
library(ggplot2) library(gridExtra)
Creating scatterplot and line chart between x and y with plot.margin to reduce the space between the plot when joined with grid.arrange −
Example
Plot1<-ggplot(df,aes(x,y))+geom_point()+theme(plot.margin=unit(c(1,1,-0.5,1),"cm")) Plot1
Output
Plot2<-ggplot(df,aes(x,y))+geom_line()+theme(plot.margin=unit(c(-0.5,1,1,1),"cm")) Plot2
Output
Joining the two plots −
Example
grid.arrange(Plot1,Plot2)
Output
- Related Articles
- How to display a list of plots with the help of grid.arrange in R?
- How to change the size of plots arranged using grid.arrange in R?
- How to separate strings in R that are joined with special characters?
- How to reduce the space between Y-axis value and ticks using ggplot2 in R?
- How to replace space between two words with underscore in an R data frame column?
- How to put space between words that start with uppercase letter in string vector in R?
- How to create two 3d plots at a time in R?
- How to arrange a list of scatterplots in R using grid.arrange?
- How to change the space between bars in a barplot in R?
- How to increase the space between horizontal legends using ggplot2 in R?
- How to show multiple ggplot2 plots with Plotly using R?
- How to create sample space of throwing two dices in R?
- How to create two plots using ggplot2 arranged in a vertical manner in R?
- How to create the plots arranged in a list that were generated using ggplot2 in R?
- Drawing lines between two plots in Matplotlib

Advertisements