Change Spacing Between Horizontal Legend Items of ggplot2 Plot in R
In this article, we are going to see how to change the space between horizontal legend items using the ggplot2 bar plot in R programming language.
Here we are using a bar graph the same can be done to any other plot. For creating a simple bar plot we will use the function geom_bar( ).
geom_bar(stat, fill, color, width)
- stat : Set the stat parameter to identify the mode.
- fill : Represents color inside the bars.
- color : Represents color of outlines of the bars.
- width : Represents width of the bars.
Data in use:
Let us first create a regular plot so that the difference is apparent.
To change the space between the horizontal legends we have to use the command below:
legend.x = unit( unit_value, ‘cm’)
The command has to be written inside the theme( ) function where we have already assigned the position of the legend before.