Method 4:Using scale_x_continuous() and scale_y_continuous() functions with ggplot2
In this method to change the axis interval, the user needs to install and import the ggplot2 package in the working R console, this package will be responsible for the plotting of the plot and for using some of the functionality. Then the user needs to call the scale_x_continous() /scale_x_continous() function with the plotted ggplot2 plot with the required parameters to change the axis intervals to a log scale in the R programming language.
scale_x_continuous() / scale_y_continuous() functions are used to for continuous position scales (x & y).
Syntax:
scale_x_continuous(…, expand = waiver())
scale_y_continuous(…, expand = waiver())
Parameters:
- …: 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.
Example: Initial plot
R
library (ggplot2) gfg<- data.frame (x= c (8,9,6,5,8,5,1,7,3,5), y= c (9,6,5,4,2,5,6,7,4,1)) ggplot (data=gfg, aes (x=x, y=y)) + geom_point () |
Output:
Example: Change axis intervals
R
library (ggplot2) gfg<- data.frame (x= c (8,9,6,5,8,5,1,7,3,5), y= c (9,6,5,4,2,5,6,7,4,1)) ggplot (data=gfg, aes (x=x, y=y)) + geom_point ()+ scale_y_continuous (trans = 'log10' ) |
Output:
How to Change Axis Intervals in R Plots?
In this article, we will be looking at the different approaches to change axis intervals in the R programming language.