

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 the aspect ratio of a plot in ggplot2 in R?
The aspect ratio of a chart can be changed in ggplot2 and this will be useful if we want a smaller image of the chart. Sometimes, we don’t have large space where the chart will be pasted therefore this functionality becomes useful. Mostly, in research reports we see charts that are of small size, hence R becomes helpful to create charts that can be pasted in the desired space. This can be done with the help of theme function.
Example
Consider the below data frame −
> set.seed(100) > x<-rpois(30,2) > df<-data.frame(x)
Loading the ggplot2 package −
> library(ggplot2)
Creating the plot with aspect ratio 4/3 −
> ggplot(df,aes(x))+ + geom_bar()+ + theme(aspect.ratio=4/3)
Output
Creating the plot with aspect ratio 16/9 −
> ggplot(df,aes(x))+ + geom_bar()+ + theme(aspect.ratio=16/9)
Output
Creating the plot with aspect ratio 1, it gives us a square form −
> ggplot(df,aes(x))+ + geom_bar()+ + theme(aspect.ratio=1)
Output
- Related Questions & Answers
- Setting the aspect ratio of a 3D plot in Matplotlib
- How to change the aspect ratio of an image in JavaFX?
- How to change the plot border color of a ggplot2 graph in R?
- How to change the border style of a plot using ggplot2 in R?
- How to change the partial plot background using ggplot2 in R?
- How to change the color of bars of a bar plot using ggplot2 in R?
- How to change plot area margins using ggplot2 in R?
- How to change the Y-axis values in a bar plot using ggplot2 in R?
- How to change the automatic sorting of X-axis of a bar plot using ggplot2 in R?
- How to maintain the aspect ratio of an element with CSS?
- How to change the angle of annotated text in plot created by using ggplot2 in R?
- Change the starting point of bars in bar plot for a ggplot2 graph in R.
- How to change the resolution of a plot in base R?
- How to change the color of gridlines of a ggplot2 graph in R?
- How to change legend values in a bar plot created by using ggplot2 in R?
Advertisements