Create a secondary y axis for lines graph with ggvis R
Last Updated :
23 Jan, 2023
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
library (dplyr)
library (ggvis)
data_frame = data.frame (col_x = c (1,2,3),
col_y = c (3,6,9),
col_z = c (-40,-80,-120))
data_frame %>% ggvis (x = ~col_x) %>%
layer_lines (y = ~col_z, stroke := "green" ) %>%
add_axis ( "y" , orient = "left" , title = "col_z" ) %>%
scale_numeric ( "y" , nice = FALSE ) %>%
add_axis ( "y" , 'ympg' , orient = "right" , title= "col_y" ) %>%
layer_lines ( prop ( 'y' , ~col_y, scale= 'ympg' ) ) %>%
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
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
library (dplyr)
library (ggvis)
data_frame = data.frame (col_x = c (10,20,30),
col_y = c (300,600,900),
)
print ( "Data Frame" )
print (data_frame)
data_frame %>%
mutate (col_z= log (col_y)) %>%
ggvis (x = ~col_x) %>%
layer_lines (y = ~col_z, stroke := "red" ) %>%
add_axis ( "y" , orient = "left" , title = "col_z" ) %>%
scale_numeric ( "y" , nice = FALSE ) %>%
add_axis ( "y" , 'ympg' , orient = "right" , title= "col_y" ) %>%
layer_lines ( prop ( 'y' , ~col_y, scale= 'ympg' ) ) %>%
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...