- 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 change plot area margins using ggplot2 in R?
While creating plots using ggplot2, the plot area is of square shape but we can change our plot area by setting plot.margin in theme function. This is helpful when we want to decrease the plot area and also when the data points are less.
Example
Consider the below data frame −
> set.seed(1) > x<-rnorm(20,0.2) > y<-rnorm(20,0.5) > df<-data.frame(x,y)
Loading ggplot2 package −
> library(ggplot2)
Creating the scatterplot without changing the plot area margins −
> ggplot(df,aes(x,y))+ + geom_point()
> ggplot(df,aes(x,y))+ + geom_point()+ + theme(plot.margin = unit(c(1,1,1,1), "cm"))
> ggplot(df,aes(x,y))+ + geom_point()+ + theme(plot.margin = unit(c(2,2,2,2), "cm"))
- Related Articles
- How to change the partial plot background using ggplot2 in R?
- How to change the border style of a plot using ggplot2 in R?
- How to change legend values in a bar plot created by using ggplot2 in R?
- How to change the Y-axis values in a bar plot using ggplot2 in R?
- How to change the color of bars of a bar plot using ggplot2 in R?
- How to change the angle of annotated text in plot created by using ggplot2 in R?
- How to write plot description outside plot in facetted plot using ggplot2 in R?
- How to create a scatterplot in R with legend position inside the plot area using ggplot2?
- How to plot multiple time series using ggplot2 in R?
- How to create a dot plot using ggplot2 in R?
- How to create an empty plot using ggplot2 in R?
- How to plot means inside boxplot using ggplot2 in R?
- How to create transparent bar plot using ggplot2 in R?
- How to write text outside plot using ggplot2 in R?
- How to change the aspect ratio of a plot in ggplot2 in R?

Advertisements