Open In App

Create a secondary y axis for lines graph with ggvis R

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be looking into the problem of adding a secondary y-axis for a line graph created using the ggvis package. R supports a large variety of v R data visualization and graph creation packages that can be invoked into the working R environment. The ggvis package is used to create visual interactive graphics tools for data plotting and representation. The package can be installed into the working space using the following command:

install.packages("ggvis")

A ggvis graphical window can be initiated using this package. It is used to create dynamic plots basis the data points provided to it. A large number of formatting and styling options are also available along with it to increase user interactions multifold. The ggvis() and add_axis() method has the following syntax as shown below:

Syntax of ggvis method in R Language

Syntax: ggvis( data , mp1, mp2.,)

Parameter:

  • data – The dataset to plot
  • mp1, mp2,.. – The map variables to plot

Syntax of add_axis function in R Language

Syntax: add_axis( vis, axes , orient , title )

Parameter:

  • vis – The ggvis object to use for plotting
  • axes – The axes to be used for plotting
  • orient – The orientation to be used for axis 
  • title – The title for the axis 

Example 1

Initially, a graphical window is initiated in the working environment using the ggvis() method which takes parameters for x-axis representation. The add_axis method is then added to derive the labeling for the axes of the plotted graph. It can be used to override the default value for the axes.

R




# installing the required libraries
library(dplyr)
library(ggvis)
# creating a data frame of three columns 
data_frame = data.frame(col_x = c(1,2,3),
                        col_y = c(3,6,9),
                        col_z = c(-40,-80,-120))
# taking a common x-axis
data_frame  %>% ggvis(x = ~col_x) %>% 
  # plotting a line for the x and z columns respectively
  layer_lines(y = ~col_z, stroke := "green") %>% 
  # providing label for left y-axis
  add_axis("y", orient = "left", title = "col_z" ) %>%  
  # using the y numeric scale for data plotting
  scale_numeric("y", nice = FALSE) %>%  
  # providing label for right y-axis
  add_axis("y", 'ympg' , orient = "right", title= "col_y" ) %>% 
  # plotting a line for the x and y columns respectively
  layer_lines( prop('y' , ~col_y,  scale='ympg') ) %>% 
  # adding the label for x-axis respectively
  add_axis("x", title = "col_x"  )


Output:

[1] "Data Frame"
> print(data_frame)
 col_x col_y col_z
1     1     3   -40
2     2     6   -80
3     3     9  -120
Create a secondary y axis for lines graph with ggvis R

 

Example 2

In the following example, a new column in the data frame is created by taking the log of the second column. The lines are then plotted to take into account the scales of the x-y and x-z axes respectively. 

R




# installing the required libraries
library(dplyr)
library(ggvis)
# creating a data frame of three columns 
data_frame = data.frame(col_x = c(10,20,30),
                        col_y = c(300,600,900),
                        )
# printing the data frame
print("Data Frame")
print(data_frame)
# taking a common x-axis
data_frame  %>% 
  mutate(col_z=log(col_y)) %>% 
  ggvis(x = ~col_x) %>% 
  # plotting a line for the x and z columns respectively
  layer_lines(y = ~col_z, stroke := "red") %>% 
  # providing label for left y-axis
  add_axis("y", orient = "left", title = "col_z" ) %>%  
  # using the y numeric scale for data plotting
  scale_numeric("y", nice = FALSE) %>%  
  # providing label for right y-axis
  add_axis("y", 'ympg' , orient = "right", title= "col_y" ) %>% 
  # plotting a line for the x and y columns respectively
  layer_lines( prop('y' , ~col_y,  scale='ympg') ) %>%  
  # adding the label for x-axis respectively
  add_axis("x", title = "col_x"  )


Output:

[1] "Data Frame" 
> print(data_frame)   
col_x col_y 
1    10   300 
2    20   600 
3    30   900
Create a secondary y axis for lines graph with ggvis R

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads