
- 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 increase the space between horizontal legends using ggplot2 in R?
Generally, the space between two legend entries is not large enough and it becomes difficult to read the legend names if the names are long. In this case, we need to increase the margin between the legend entries/names but this would be required when the legends are horizontally aligned as vertical legends can be read as it is. For this purpose, we can use legend.text argument inside theme function of ggplot2 package.
Example
Consider the below data frame −
> x<-c("Male","Female") > y<-c(501,520) > df<-data.frame(x,y) > df
Output
x y 1 Male 501 2 Female 520
Loading ggplot2 package and creating a horizontal bar graph −
Example
> library(ggplot2) > ggplot(df,aes(x,y,fill=x))+geom_bar(stat="identity")+coord_flip()+theme(legend.position="bottom")+guides(fill=guide_legend(title=NULL))
Output
Creating the plot with a larger space between legend entries −
Example
> ggplot(df,aes(x,y,fill=x))+geom_bar(stat="identity")+coord_flip()+theme(legend.position="bottom",legend.text=element_text(margin=margin(r=1,unit="inch")))+guides(fill=guide_legend(title=NULL))
Output
- Related Questions & Answers
- How to increase the space between bars of a bar plot using ggplot2 in R?
- Increase the space between facets in a facetted plot created using ggplot2 in R.
- How to increase the distance between boxplots using ggplot2 in R?
- How to increase the width of axes using ggplot2 in R?
- How to increase the axes tick width using ggplot2 in R?
- How to create horizontal legend using ggplot2 in R?
- How to set the legends using ggplot2 on top-right side in R?
- How to create a scatterplot with two legends using ggplot2 in R?
- How to reduce the space between Y-axis value and ticks using ggplot2 in R?
- How to increase the X-axis labels font size using ggplot2 in R?
- How to change the Y-axis title to horizontal using ggplot2 in R?
- How to create a horizontal bar graph using ggplot2 in R?
- How to increase the width of the median line in boxplot using ggplot2 in R?
- How to increase the width of the lines in the boxplot created by using ggplot2 in R?
- How to create facetted plot with facets in horizontal direction using ggplot2 in R?
Advertisements