Set Origin of ggplot2 Plot Axes to Zero in R
In this article, we will be looking at the approach to set the origin of the gplot2 plot axes to zero with the help of some functions of R programming language.
In this approach to set the origin of the ggpot2 plot axes to zero, the user first needs to install and import the ggplot2 package in the working R console, and then call the scale_x_continuous() and scale_y_continuous() functions together with the plot, with the required parameters and this will add zeroes to the origin of both the ggplot2 plot axes in the R programming language,
Scale_x_continuous() and scale_y_continuous() functions functions are used to set the continuous position scales (x & y).
scale_x_continuous(…, expand = waiver())
scale_y_continuous(…, expand = waiver())
- …: common continuous scale parameters: name, breaks, labels, na.value, limits, and trans.
- Expand: a numeric vector of length two giving multiplicative and additive expansion constants.
Let us first look at initial plot, so that the difference is apparent.
Example: initial plot
Now, let’s add zeroes to origin. using the approach described above.
Example: final plot