Skip to content
Related Articles

Related Articles

How to change the domain range of an axis in R

View Discussion
Improve Article
Save Article
  • Last Updated : 02 Aug, 2022
View Discussion
Improve Article
Save Article

In this article, we are going to see how to change the domain range of an Axis in R Programming Language.

The layer_points() method can plot the coordinates using points specified in the data frame. The method has the following syntax : 

Syntax: layer_points(vis)

Arguments :

  • vis – The ggvis object

The following code snippet indicates that a data frame is created for a pair of values (x , x^2) for a specified range of values. These coordinates are then plotted on the ggvis plot. 

R




# installing the required libraries 
library(ggplot2)
library(ggvis)
  
# creating the data frame by
# defining the x and y coordinates
# respectively
data_frame <- data.frame(
  x_pos = 1:10,
  y_pos <- x_pos^2
)
print("Data Frame")
print(data_frame)
  
# plotting the points
data_frame %>% ggvis(~x_pos, ~y_pos) 
%>% layer_points()

Output

[1] "Data Frame"
> print(data_frame)
  x_pos y_pos....x_pos.2
1      1                1
2      2                4
3      3                9
4      4               16
5      5               25
6      6               36
7      7               49
8      8               64
9      9               81
10    10              100

 

The scale of the axis can be changed by specifying the domain as an argument of the method scale_numeric in the ggvis package. The upper and lower limits are specified in the domain parameter. The method has the following syntax : 

scale_numeric (vis, axis, domain)

Arguments :

  • vis – The ggvis object
  • axis – The axis to scale the range
  • domain – The new interval to plot against

The below code indicates the x axis range to be modified from the interval 0 to 100. 

R




# installing the required libraries 
library(ggplot2)
library(ggvis)
  
# creating the data frame by defining
# the x and y coordinates respectively
data_frame <- data.frame(
  x_pos = 1:10,
  y_pos <- x_pos^2
)
print("Data Frame")
print(data_frame)
  
# plotting the points
data_frame %>% ggvis(~x_pos, ~y_pos) %>% layer_points() %>% 
  
# specifying the range of x axis
scale_numeric("x", domain = c(0, 100))

Output

[1] "Data Frame"
> print(data_frame)
 x_pos y_pos....x_pos.2
1      1                1
2      2                4
3      3                9
4      4               16
5      5               25
6      6               36
7      7               49
8      8               64
9      9               81
10    10              100

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!