Open In App

How to Add a vega axis specification to a ggvis plot?

Last Updated : 05 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how to add a vega axis specification to ggvis plot in R programming language.

ggvis package

The data visualization is accomplished using the R ggvis tool. It is used to create visual interactive graphics for data plotting and representation. The package can be installed into the working space using the following command :

install.packages("ggvis")

To launch the ggvis graphical window, use the ggvis() method found in the ggvis package. The ggvis method has the following syntax :

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

Arguments :

data – The dataset to plot.

mp1, mp2,.. – The map variables to plot.

Vega axis specification

The vega axis specification to a ggvis plot can be added using the add_axis method. The add axis method can be used to modify the default values of the plot values. The add_axis method has the following syntax : 

Syntax: add_axis( vis, type, scale = NULL, orient = NULL, title = NULL, ticks = NULL)

Arguments : 

  • vis – A ggvis object
  • type – Specifying to take whether “x” or “y” axis respectively
  • orient – Specify the orientation of the axis. 
  • title – Specifying the title of the axis
  • ticks – The number of ticks

Note: The orientation of the axes can also be specified as arguments to the add_axis method in R. 

Plotting a graph

Firstly, the data.frame() method is used to generate a data frame. The data frame’s data points are created. Using the pipe operator, the ggvis procedures are applied to the data frame and plot the graph using ggvis.

R




# Import ggvis package
library("ggvis")
  
# Declaring a data frame
data_frame <- data.frame(col1 = c(1:5),
              col2 = c(20,32,12,57,33))
  
# Printing the data frame
print("Data Frame")
print(data_frame)
  
# Plotting data on the ggvis plot
# adding x and y axes respectively 
data_frame %>% ggvis(x = ~col1, y = ~col2) %>%
  layer_points() %>%
  add_axis("x", title = "Roll Number", orient = "top") %>%
  add_axis("y", title = "Marks", orient = "left")


Output:

[1] "Data Frame" 
col1 col2 
1    1   20 
2    2   32 
3    3   12 
4    4   57 
5    5   33
How to Add a vega axis specification to a ggvis plot

 

Ticks can be assigned to the axes where the bins are created as shown in the output of the below code.

R




# Importing ggvis package
library("ggvis")
  
# Declaring a data frame
data_frame <- data.frame(col1 = c(1:5),
             col2 = c(20,32,12,57,33))
  
# Printing the data frame
print("Data Frame")
print(data_frame)
  
# Plotting data on the ggvis plot
# adding x and y axes respectively 
data_frame %>% ggvis(x = ~col1, y = ~col2) %>%
  layer_lines() %>%
  add_axis("x", title = "Roll Number",
           orient = "top", ticks = 20) %>%
  add_axis("y", title = "Marks",
           orient = "left", ticks = 40)


Output:

[1] "Data Frame" 
col1 col2 
1    1   20 
2    2   32 
3    3   12 
4    4   57 
5    5   33
How to Add a vega axis specification to a ggvis plot

 



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

Similar Reads