Change values of X-axis to Integer

To change real axis values to integer we can use scale_x_continuous() or scale_y_continuous() functions to fix x-axis and y-axis respectively. By passing appropriate interval values to break argument the job can be done.

Syntax: scale_x_continuous(breaks =c(b1,b2,b3….bn))

Parameters:

breaks: this specifies at what intervals the value should be displayed

b1,b2,b3…bn: these values specifies each intervals 

Example: Changing real values on x-axis to integer

R




library(dplyr)
library(ggplot2)
  
set.seed(10)
df <- data.frame(X = rnorm(15,mean=1.199, sd=0.7622377), 
                 Y =(rnorm(15,mean=3.057, sd= 0.4358663)))
  
df %>% 
  ggplot(aes(x = X, y = Y)) +
  geom_point()+
  scale_x_continuous(breaks =c(0,1,2))


Output:

Similarly, both values can be changed if both the functions are called together. 

Syntax: 

scale_y_continuous(breaks =c(b1,b2,b3,…bn))+ scale_x_continuous(breaks =c(b1,b2,b3,…bn))

Example: Changing values for both axes

R




library(dplyr)
library(ggplot2)
  
set.seed(10)
df <- data.frame(X = rnorm(15,mean=1.199, sd=0.7622377), 
                 Y =(rnorm(15,mean=3.057, sd= 0.4358663)))
  
df %>% 
  ggplot(aes(x = X, y = Y)) +
  geom_point()+
  scale_x_continuous(breaks =c(0,1,2))+
  scale_y_continuous(breaks =c(3))


Output:

How to Change X and Y Axis Values from Real to Integers in ggplot2 in R

In this article, we will discuss how to change X and Y-Axes values from Real to Integers using ggplot2 in the R programming language. Let us first create a plot that contains Real values on axes and then change them to integers so that the difference will be apparent.

Example: Initial plot

R




library(dplyr)
library(ggplot2)
  
# dummy dataset
set.seed(10)
df <- data.frame(X = rnorm(15,mean=1.199, sd=0.7622377), 
                 Y =(rnorm(15,mean=3.057, sd= 0.4358663)))
  
df %>% 
  ggplot(aes(x = X, y = Y)) +
  geom_point()


Output:

As you can see in the above code, the values of the X and Y axes are Real. Now let us look at various methods by which these can be changed to integer values.

Similar Reads

Method 1: Change values of X-axis to Integer

...

Method 2: Using a custom function

To change real axis values to integer we can use scale_x_continuous() or scale_y_continuous() functions to fix x-axis and y-axis respectively. By passing appropriate interval values to break argument the job can be done....